Merge pull request #548 from vingtetun/master
Let the firefox extension use the loading indicator mechanism
This commit is contained in:
commit
67652a3efd
4
Makefile
4
Makefile
@ -166,9 +166,9 @@ PDF_WEB_FILES = \
|
|||||||
extension:
|
extension:
|
||||||
# Copy a standalone version of pdf.js inside the content directory
|
# Copy a standalone version of pdf.js inside the content directory
|
||||||
@rm -Rf $(EXTENSION_SRC)/$(CONTENT_DIR)/
|
@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 $(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
|
# Create the xpi
|
||||||
@cd $(EXTENSION_SRC); zip -r $(EXTENSION_NAME) *
|
@cd $(EXTENSION_SRC); zip -r $(EXTENSION_NAME) *
|
||||||
|
@ -13,9 +13,6 @@ const PDF_CONTENT_TYPE = 'application/pdf';
|
|||||||
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
||||||
Cu.import('resource://gre/modules/Services.jsm');
|
Cu.import('resource://gre/modules/Services.jsm');
|
||||||
|
|
||||||
// TODO
|
|
||||||
// Add some download progress event
|
|
||||||
|
|
||||||
function log(aMsg) {
|
function log(aMsg) {
|
||||||
let msg = 'pdfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
|
let msg = 'pdfContentHandler.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
|
||||||
Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
|
Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService)
|
||||||
@ -23,33 +20,44 @@ function log(aMsg) {
|
|||||||
dump(msg + '\n');
|
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) {
|
function loadDocument(aWindow, aDocumentUrl) {
|
||||||
let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
|
let xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
|
||||||
.createInstance(Ci.nsIXMLHttpRequest);
|
.createInstance(Ci.nsIXMLHttpRequest);
|
||||||
xhr.open('GET', aDocumentUrl);
|
xhr.onprogress = function updateProgress(evt) {
|
||||||
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
if (evt.lengthComputable)
|
||||||
xhr.onreadystatechange = function() {
|
fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
|
||||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
};
|
||||||
|
|
||||||
|
xhr.onerror = function error(evt) {
|
||||||
|
fireEventTo(evt.type, false, aWindow);
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.onload = function load(evt) {
|
||||||
let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
||||||
xhr.responseArrayBuffer || xhr.response);
|
xhr.responseArrayBuffer || xhr.response);
|
||||||
try {
|
try {
|
||||||
var view = new Uint8Array(data);
|
let 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;
|
let window = aWindow.wrappedJSObject;
|
||||||
var arrayBuffer = new window.ArrayBuffer(data.byteLength);
|
let arrayBuffer = new window.ArrayBuffer(data.byteLength);
|
||||||
var view2 = new window.Uint8Array(arrayBuffer);
|
let view2 = new window.Uint8Array(arrayBuffer);
|
||||||
view2.set(view);
|
view2.set(view);
|
||||||
|
|
||||||
let evt = window.document.createEvent('CustomEvent');
|
fireEventTo(evt.type, arrayBuffer, aWindow);
|
||||||
evt.initCustomEvent('pdfloaded', false, false, arrayBuffer);
|
|
||||||
window.document.dispatchEvent(evt);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('Error - ' + e);
|
log('Error - ' + e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
xhr.open('GET', aDocumentUrl);
|
||||||
|
xhr.responseType = 'arraybuffer';
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +148,7 @@ pdfContentHandler.prototype = {
|
|||||||
url = url.replace('%s', uri.spec);
|
url = url.replace('%s', uri.spec);
|
||||||
window.location = url;
|
window.location = url;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('Error - ' + e);
|
log('Error retrieving the pdf.js base url - ' + e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
19
pdf.js
19
pdf.js
@ -116,24 +116,31 @@ function stringToPDFString(str) {
|
|||||||
// getPdf()
|
// getPdf()
|
||||||
// Convenience function to perform binary Ajax GET
|
// Convenience function to perform binary Ajax GET
|
||||||
// Usage: getPdf('http://...', callback)
|
// Usage: getPdf('http://...', callback)
|
||||||
// getPdf({url:String [,progress:Function]}, callback)
|
// getPdf({
|
||||||
|
// url:String ,
|
||||||
|
// [,progress:Function, error:Function]
|
||||||
|
// },
|
||||||
|
// callback)
|
||||||
//
|
//
|
||||||
function getPdf(arg, callback) {
|
function getPdf(arg, callback) {
|
||||||
var params = arg;
|
var params = arg;
|
||||||
if (typeof arg === 'string') {
|
if (typeof arg === 'string')
|
||||||
params = { url: arg };
|
params = { url: arg };
|
||||||
}
|
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open('GET', params.url);
|
xhr.open('GET', params.url);
|
||||||
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
||||||
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
|
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
|
||||||
xhr.onprogress = params.progress || undefined;
|
|
||||||
|
if ('progress' in params)
|
||||||
|
xhr.onprogrss = params.progress || undefined;
|
||||||
|
|
||||||
|
if ('error' in params)
|
||||||
|
xhr.onerror = params.error || undefined;
|
||||||
|
|
||||||
xhr.onreadystatechange = function getPdfOnreadystatechange() {
|
xhr.onreadystatechange = function getPdfOnreadystatechange() {
|
||||||
var data;
|
|
||||||
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
|
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
|
||||||
data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
||||||
xhr.responseArrayBuffer || xhr.response);
|
xhr.responseArrayBuffer || xhr.response);
|
||||||
callback(data);
|
callback(data);
|
||||||
}
|
}
|
||||||
|
@ -107,17 +107,20 @@ var PDFView = {
|
|||||||
|
|
||||||
document.title = url;
|
document.title = url;
|
||||||
|
|
||||||
getPdf({url: url, progress: PDFView.progressLevel}, function(data) {
|
getPdf(
|
||||||
document.getElementById('loading').style.display = 'none';
|
{
|
||||||
|
url: url,
|
||||||
|
progress: function getPdfProgress(evt) {
|
||||||
|
if (evt.lengthComputable)
|
||||||
|
PDFView.progress(evt.loaded / evt.total);
|
||||||
|
},
|
||||||
|
error: PDFView.error
|
||||||
|
},
|
||||||
|
function getPdfLoad(data) {
|
||||||
PDFView.load(data, scale);
|
PDFView.load(data, scale);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
progressLevel: function(evt) {
|
|
||||||
var p = Math.round((evt.loaded / evt.total) * 100);
|
|
||||||
document.getElementById('loading').innerHTML = 'Loading... ' + p + '%';
|
|
||||||
},
|
|
||||||
|
|
||||||
navigateTo: function(dest) {
|
navigateTo: function(dest) {
|
||||||
if (typeof dest === 'string')
|
if (typeof dest === 'string')
|
||||||
dest = this.destinations[dest];
|
dest = this.destinations[dest];
|
||||||
@ -134,7 +137,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) {
|
load: function(data, scale) {
|
||||||
|
var loadingIndicator = document.getElementById('loading');
|
||||||
|
loadingIndicator.style.display = 'none';
|
||||||
|
|
||||||
var sidebar = document.getElementById('sidebarView');
|
var sidebar = document.getElementById('sidebarView');
|
||||||
sidebar.parentNode.scrollTop = 0;
|
sidebar.parentNode.scrollTop = 0;
|
||||||
|
|
||||||
@ -482,10 +499,18 @@ window.addEventListener('load', function(evt) {
|
|||||||
document.getElementById('fileInput').value = null;
|
document.getElementById('fileInput').value = null;
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
window.addEventListener('pdfloaded', function(evt) {
|
window.addEventListener('pdfload', function(evt) {
|
||||||
PDFView.load(evt.detail);
|
PDFView.load(evt.detail);
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
|
window.addEventListener('pdfprogress', function(evt) {
|
||||||
|
PDFView.progress(evt.detail);
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
window.addEventListener('pdferror', function(evt) {
|
||||||
|
PDFView.error();
|
||||||
|
}, true);
|
||||||
|
|
||||||
function updateViewarea() {
|
function updateViewarea() {
|
||||||
var visiblePages = PDFView.getVisiblePages();
|
var visiblePages = PDFView.getVisiblePages();
|
||||||
for (var i = 0; i < visiblePages.length; i++) {
|
for (var i = 0; i < visiblePages.length; i++) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user