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; }; }