From 6d2c5414bf12b90c92b359cee627fe9e8caa714e Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Wed, 19 Oct 2011 03:20:50 +0200 Subject: [PATCH 1/3] Add a download button in case the extension is not enough --- .../firefox/components/pdfContentHandler.js | 33 +- web/images/download.svg | 619 ++++++++++++++++++ web/viewer.html | 5 + web/viewer.js | 7 +- 4 files changed, 651 insertions(+), 13 deletions(-) create mode 100644 web/images/download.svg diff --git a/extensions/firefox/components/pdfContentHandler.js b/extensions/firefox/components/pdfContentHandler.js index eda0ec92c..a04b717a2 100644 --- a/extensions/firefox/components/pdfContentHandler.js +++ b/extensions/firefox/components/pdfContentHandler.js @@ -20,16 +20,17 @@ function log(aMsg) { dump(msg + '\n'); } +const NS_ERROR_WONT_HANDLE_CONTENT = 0x805d0001; function pdfContentHandler() { } pdfContentHandler.prototype = { handleContent: function handleContent(aMimetype, aContext, aRequest) { if (aMimetype != PDF_CONTENT_TYPE) - throw Cr.NS_ERROR_WONT_HANDLE_CONTENT; + throw NS_ERROR_WONT_HANDLE_CONTENT; if (!(aRequest instanceof Ci.nsIChannel)) - throw Cr.NS_ERROR_WONT_HANDLE_CONTENT; + throw NS_ERROR_WONT_HANDLE_CONTENT; let window = null; let callbacks = aRequest.notificationCallbacks || @@ -37,18 +38,26 @@ pdfContentHandler.prototype = { if (!callbacks) return; + window = callbacks.getInterface(Ci.nsIDOMWindow); + + let url = null; + try { + url = Services.prefs.getCharPref('extensions.pdf.js.url'); + } catch(e) { + log('Error retrieving the pdf.js base url - ' + e); + throw NS_ERROR_WONT_HANDLE_CONTENT; + } + + // To allow a Download feature we need to ensure pdf.js + // is not opened again if the request for opening an + // application/pdf document has been done by itself. + let location = window.location.toString(); + if (location.indexOf(url.replace('%s', '')) == 0) + throw NS_ERROR_WONT_HANDLE_CONTENT; + aRequest.cancel(Cr.NS_BINDING_ABORTED); let uri = aRequest.URI; - - try { - let url = Services.prefs.getCharPref('extensions.pdf.js.url'); - url = url.replace('%s', uri.spec); - - window = callbacks.getInterface(Ci.nsIDOMWindow); - window.location = url; - } catch (e) { - log('Error retrieving the pdf.js base url - ' + e); - } + window.location = url.replace('%s', uri.spec); }, classID: Components.ID('{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}'), diff --git a/web/images/download.svg b/web/images/download.svg new file mode 100644 index 000000000..2922c4331 --- /dev/null +++ b/web/images/download.svg @@ -0,0 +1,619 @@ + + + diff --git a/web/viewer.html b/web/viewer.html index 4175d29fc..f348b6c58 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -64,6 +64,11 @@ Print + +
diff --git a/web/viewer.js b/web/viewer.js index e40697a26..27211ce1c 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -85,6 +85,10 @@ var PDFView = { this.setScale(newScale, true); }, + download: function pdfViewDownload() { + window.open(document.title, '_parent', 'pdf=yes'); + }, + set page(val) { var pages = this.pages; var input = document.getElementById('pageNumber'); @@ -661,8 +665,9 @@ window.addEventListener('change', function webViewerChange(evt) { document.title = file.name; - // URL does not reflect proper document location - hiding bookmark icon. + // URL does not reflect proper document location - hiding some icons. document.getElementById('viewBookmark').setAttribute('hidden', 'true'); + document.getElementById('download').setAttribute('hidden', 'true'); }, true); window.addEventListener('transitionend', function webViewerTransitionend(evt) { From f3dc4206ee29c28badceccffb3f4c4cfd709687c Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Wed, 19 Oct 2011 06:29:14 +0200 Subject: [PATCH 2/3] Make the 'download' button behavior more robust --- extensions/firefox/components/pdfContentHandler.js | 10 +++------- web/viewer.js | 10 +++++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/extensions/firefox/components/pdfContentHandler.js b/extensions/firefox/components/pdfContentHandler.js index a04b717a2..d5f466e3f 100644 --- a/extensions/firefox/components/pdfContentHandler.js +++ b/extensions/firefox/components/pdfContentHandler.js @@ -48,16 +48,12 @@ pdfContentHandler.prototype = { throw NS_ERROR_WONT_HANDLE_CONTENT; } - // To allow a Download feature we need to ensure pdf.js - // is not opened again if the request for opening an - // application/pdf document has been done by itself. - let location = window.location.toString(); - if (location.indexOf(url.replace('%s', '')) == 0) + let targetUrl = aRequest.URI.spec; + if (targetUrl.indexOf('?pdfjs.action=download') >= 0) throw NS_ERROR_WONT_HANDLE_CONTENT; aRequest.cancel(Cr.NS_BINDING_ABORTED); - let uri = aRequest.URI; - window.location = url.replace('%s', uri.spec); + window.location = url.replace('%s', targetUrl); }, classID: Components.ID('{2278dfd0-b75c-11e0-8257-1ba3d93c9f1a}'), diff --git a/web/viewer.js b/web/viewer.js index 27211ce1c..12d0a3f37 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -85,10 +85,6 @@ var PDFView = { this.setScale(newScale, true); }, - download: function pdfViewDownload() { - window.open(document.title, '_parent', 'pdf=yes'); - }, - set page(val) { var pages = this.pages; var input = document.getElementById('pageNumber'); @@ -119,7 +115,7 @@ var PDFView = { }, open: function pdfViewOpen(url, scale) { - document.title = url; + document.title = this.url = url; getPdf( { @@ -135,6 +131,10 @@ var PDFView = { }); }, + download: function pdfViewDownload() { + window.open(this.url + '?pdfjs.action=download', '_parent'); + }, + navigateTo: function pdfViewNavigateTo(dest) { if (typeof dest === 'string') dest = this.destinations[dest]; From 63099565a3981c33bcd09aa4280eeee94fe2efc8 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Wed, 19 Oct 2011 16:17:02 +0200 Subject: [PATCH 3/3] fix spacing issue (lint) --- extensions/firefox/components/pdfContentHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/firefox/components/pdfContentHandler.js b/extensions/firefox/components/pdfContentHandler.js index d5f466e3f..7746e41b6 100644 --- a/extensions/firefox/components/pdfContentHandler.js +++ b/extensions/firefox/components/pdfContentHandler.js @@ -43,7 +43,7 @@ pdfContentHandler.prototype = { let url = null; try { url = Services.prefs.getCharPref('extensions.pdf.js.url'); - } catch(e) { + } catch (e) { log('Error retrieving the pdf.js base url - ' + e); throw NS_ERROR_WONT_HANDLE_CONTENT; }