Firefox extension now use the default loading indicator
This commit is contained in:
		
							parent
							
								
									6ae80b5e78
								
							
						
					
					
						commit
						556727f2f5
					
				
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							@ -165,9 +165,9 @@ PDF_WEB_FILES = \
 | 
			
		||||
extension:
 | 
			
		||||
	# Copy a standalone version of pdf.js inside the content directory
 | 
			
		||||
	@rm -Rf $(EXTENSION_SRC)/$(CONTENT_DIR)/
 | 
			
		||||
	@mkdir $(EXTENSION_SRC)/$(CONTENT_DIR)/
 | 
			
		||||
	@mkdir -p $(EXTENSION_SRC)/$(CONTENT_DIR)/web
 | 
			
		||||
	@cp $(PDF_JS_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
 | 
			
		||||
	@cp -r $(PDF_WEB_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
 | 
			
		||||
	@cp -r $(PDF_WEB_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/web/
 | 
			
		||||
 | 
			
		||||
	# Create the xpi
 | 
			
		||||
	@cd $(EXTENSION_SRC); zip -r $(EXTENSION_NAME) *
 | 
			
		||||
 | 
			
		||||
@ -8,9 +8,6 @@ const PDF_CONTENT_TYPE = "application/pdf";
 | 
			
		||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 | 
			
		||||
Cu.import("resource://gre/modules/Services.jsm");
 | 
			
		||||
 | 
			
		||||
// TODO
 | 
			
		||||
// Add some download progress event
 | 
			
		||||
 | 
			
		||||
function log(aMsg) {
 | 
			
		||||
  let msg = "pdfContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
 | 
			
		||||
  Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
 | 
			
		||||
@ -18,33 +15,45 @@ function log(aMsg) {
 | 
			
		||||
  dump(msg + "\n");
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function fireEventTo(aName, aData, aWindow) {
 | 
			
		||||
  let window = aWindow.wrappedJSObject;
 | 
			
		||||
  let evt = window.document.createEvent("CustomEvent");
 | 
			
		||||
  evt.initCustomEvent("pdf" + aName, false, false, aData);
 | 
			
		||||
  window.document.dispatchEvent(evt);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function loadDocument(aWindow, aDocumentUrl) {
 | 
			
		||||
  let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
 | 
			
		||||
              .createInstance(Ci.nsIXMLHttpRequest);
 | 
			
		||||
  xhr.open("GET", aDocumentUrl);
 | 
			
		||||
  xhr.mozResponseType = xhr.responseType = "arraybuffer";
 | 
			
		||||
  xhr.onreadystatechange = function() {
 | 
			
		||||
    if (xhr.readyState == 4 && xhr.status == 200) {
 | 
			
		||||
      let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 | 
			
		||||
                  xhr.responseArrayBuffer || xhr.response);
 | 
			
		||||
      try {
 | 
			
		||||
        var view = new Uint8Array(data);
 | 
			
		||||
 | 
			
		||||
        // I think accessing aWindow.wrappedJSObject returns a 
 | 
			
		||||
        // XPCSafeJSObjectWrapper and so it is safe but mrbkap can confirm that
 | 
			
		||||
        let window = aWindow.wrappedJSObject;
 | 
			
		||||
        var arrayBuffer = new window.ArrayBuffer(data.byteLength);
 | 
			
		||||
        var view2 = new window.Uint8Array(arrayBuffer);
 | 
			
		||||
        view2.set(view);
 | 
			
		||||
  xhr.onprogress = function updateProgress(evt) {
 | 
			
		||||
    if (evt.lengthComputable)
 | 
			
		||||
      fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
        let evt = window.document.createEvent("CustomEvent");
 | 
			
		||||
        evt.initCustomEvent("pdfloaded", false, false, arrayBuffer);
 | 
			
		||||
        window.document.dispatchEvent(evt);
 | 
			
		||||
      } catch(e) {
 | 
			
		||||
        log("Error - " + e);
 | 
			
		||||
      }
 | 
			
		||||
  xhr.onerror = function error(evt) {
 | 
			
		||||
    fireEventTo(evt.type, false, aWindow);
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  xhr.onload = function load(evt) {
 | 
			
		||||
    let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 | 
			
		||||
                xhr.responseArrayBuffer || xhr.response);
 | 
			
		||||
    try {
 | 
			
		||||
      let view = new Uint8Array(data);
 | 
			
		||||
 | 
			
		||||
      let window = aWindow.wrappedJSObject;
 | 
			
		||||
      let arrayBuffer = new window.ArrayBuffer(data.byteLength);
 | 
			
		||||
      let view2 = new window.Uint8Array(arrayBuffer);
 | 
			
		||||
      view2.set(view);
 | 
			
		||||
 | 
			
		||||
      fireEventTo(evt.type, arrayBuffer, aWindow);
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
      log("Error - " + e);
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  xhr.open("GET", aDocumentUrl);
 | 
			
		||||
  xhr.responseType = "arraybuffer";
 | 
			
		||||
  xhr.send(null);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -131,7 +140,7 @@ pdfContentHandler.prototype = {
 | 
			
		||||
      url = url.replace("%s", uri.spec);
 | 
			
		||||
      window.location = url;
 | 
			
		||||
    } catch(e) {
 | 
			
		||||
      log("Error - " + e);
 | 
			
		||||
      log("Error retrieving the pdf.js base url - " + e);
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -111,14 +111,17 @@ var PDFView = {
 | 
			
		||||
    xhr.open('GET', url);
 | 
			
		||||
    xhr.mozResponseType = xhr.responseType = 'arraybuffer';
 | 
			
		||||
    xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
 | 
			
		||||
    xhr.onprogress = PDFView.progressLevel;
 | 
			
		||||
    xhr.onprogress = function updateProgress(evt) {
 | 
			
		||||
      if (evt.lengthComputable)
 | 
			
		||||
        PDFView.progress(evt.loaded / evt.total);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    xhr.onerror = PDFView.error;
 | 
			
		||||
 | 
			
		||||
    xhr.onreadystatechange = function() {
 | 
			
		||||
      if (xhr.readyState === 4 && xhr.status === xhr.expected) {
 | 
			
		||||
        var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
 | 
			
		||||
                    xhr.responseArrayBuffer || xhr.response);
 | 
			
		||||
 | 
			
		||||
        document.getElementById('loading').style.display = 'none';
 | 
			
		||||
        PDFView.load(data, scale);
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
@ -126,11 +129,6 @@ var PDFView = {
 | 
			
		||||
    xhr.send(null);
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  progressLevel: function(evt) {
 | 
			
		||||
    var p = Math.round((evt.loaded / evt.total) * 100);
 | 
			
		||||
    document.getElementById('loading').innerHTML = 'Loading... ' + p + '%';
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  navigateTo: function(dest) {
 | 
			
		||||
    if (typeof dest === 'string')
 | 
			
		||||
      dest = this.destinations[dest];
 | 
			
		||||
@ -147,7 +145,21 @@ var PDFView = {
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  error: function() {
 | 
			
		||||
    var loadingIndicator = document.getElementById('loading');
 | 
			
		||||
    loadingIndicator.innerHTML = 'Error';
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  progress: function(level) {
 | 
			
		||||
    var percent = Math.round(level * 100);
 | 
			
		||||
    var loadingIndicator = document.getElementById('loading');
 | 
			
		||||
    loadingIndicator.innerHTML = 'Loading... ' + percent + '%';
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  load: function(data, scale) {
 | 
			
		||||
    var loadingIndicator = document.getElementById('loading');
 | 
			
		||||
    loadingIndicator.style.display = 'none';
 | 
			
		||||
 | 
			
		||||
    var sidebar = document.getElementById('sidebarView');
 | 
			
		||||
    sidebar.parentNode.scrollTop = 0;
 | 
			
		||||
 | 
			
		||||
@ -495,10 +507,18 @@ window.addEventListener('load', function(evt) {
 | 
			
		||||
    document.getElementById('fileInput').value = null;
 | 
			
		||||
}, true);
 | 
			
		||||
 | 
			
		||||
window.addEventListener('pdfloaded', function(evt) {
 | 
			
		||||
window.addEventListener('pdfload', function(evt) {
 | 
			
		||||
  PDFView.load(evt.detail);
 | 
			
		||||
}, true);
 | 
			
		||||
 | 
			
		||||
window.addEventListener('pdfprogress', function(evt) {
 | 
			
		||||
  PDFView.progress(evt.detail);
 | 
			
		||||
}, true);
 | 
			
		||||
 | 
			
		||||
window.addEventListener('pdferror', function(evt) {
 | 
			
		||||
  PDFView.error();
 | 
			
		||||
}, true);
 | 
			
		||||
 | 
			
		||||
function updateViewarea() {
 | 
			
		||||
  var visiblePages = PDFView.getVisiblePages();
 | 
			
		||||
  for (var i = 0; i < visiblePages.length; i++) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user