From fd9f3d7d5eb3928dea692b2ee757750335f9e53d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 24 Apr 2020 13:17:04 +0200 Subject: [PATCH] Let `PDFAttachmentViewer._bindPdfLink` fallback to downloading the PDF file, when opening the `blobUrl` fails This is a simple work-around for https://bugzilla.mozilla.org/show_bug.cgi?id=1632644 which was caused by platform changes in Firefox. Ideally the Firefox bug should still be fixed, but these PDF.js changes seem generally useful to prevent both current and future issues here. --- web/pdf_attachment_viewer.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index a8c2dbefa..de1a63df6 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -83,7 +83,7 @@ class PDFAttachmentViewer { ); } let blobUrl; - button.onclick = function () { + button.onclick = () => { if (!blobUrl) { blobUrl = URL.createObjectURL( new Blob([content], { type: "application/pdf" }) @@ -93,6 +93,9 @@ class PDFAttachmentViewer { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // The current URL is the viewer, let's use it and append the file. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename); + } else if (PDFJSDev.test("MOZCENTRAL")) { + // Let Firefox's content handler catch the URL and display the PDF. + viewerUrl = blobUrl + "?" + encodeURIComponent(filename); } else if (PDFJSDev.test("CHROME")) { // In the Chrome extension, the URL is rewritten using the history API // in viewer.js, so an absolute URL must be generated. @@ -101,11 +104,17 @@ class PDFAttachmentViewer { chrome.runtime.getURL("/content/web/viewer.html") + "?file=" + encodeURIComponent(blobUrl + "#" + filename); - } else if (PDFJSDev.test("MOZCENTRAL")) { - // Let Firefox's content handler catch the URL and display the PDF. - viewerUrl = blobUrl + "?" + encodeURIComponent(filename); } - window.open(viewerUrl); + try { + window.open(viewerUrl); + } catch (ex) { + console.error(`_bindPdfLink: ${ex}`); + // Release the `blobUrl`, since opening it failed... + URL.revokeObjectURL(blobUrl); + blobUrl = null; + // ... and fallback to downloading the PDF file. + this.downloadManager.downloadData(content, filename, "application/pdf"); + } return false; }; }