From de932573ddc36ae5525f195f5425941fc4dfd087 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 27 Aug 2020 15:50:55 +0200 Subject: [PATCH 1/3] Revert "Use a link, rather than `window.open`, when opening PDF attachments in Firefox (bug 1661259)" This reverts commit 1e5d4b6a8025477c3b15b3a320b1cc41ccd7c2bc, since it unfortunately doesn't work in all situations. Please note that I did *successfully* test the patch in a local Firefox build, obviously with an ad blocker installed. However, I've now tested the *latest* Nightly-build with my default profile, and unfortunately I can still reproduce the bug there!? --- web/pdf_attachment_viewer.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index a6406a053..0543cd6c5 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -118,22 +118,7 @@ class PDFAttachmentViewer extends BaseTreeViewer { encodeURIComponent(blobUrl + "#" + filename); } try { - if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { - window.open(viewerUrl); - } else { - // Since we have a full URL in the MOZCENTRAL-build, use a link rather - // than `window.open` since e.g. ad blockers may otherwise force-close - // the newly opened window and thus break viewing of PDF attachments - // (fixes bug 1661259). - const a = document.createElement("a"); - a.hidden = true; - a.href = viewerUrl; - a.target = "_blank"; - // must be in the document, otherwise `a.click()` is ignored. - (document.body || document.documentElement).appendChild(a); - a.click(); - a.remove(); - } + window.open(viewerUrl); } catch (ex) { console.error(`_bindPdfLink: ${ex}`); // Release the `blobUrl`, since opening it failed... From a23079c2dd0ea039ee9b2d80de8ab52ca204821d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 27 Aug 2020 16:00:12 +0200 Subject: [PATCH 2/3] Ensure that `PDFAttachmentViewer._bindLink` assigns the correct contentType when downloading PDF attachments This should provide better filetype detection when downloading PDF attachments in the viewer. Also, to avoid creating the "is PDF file" regular expression more than once it's extracted into a global constant instead. --- web/pdf_attachment_viewer.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index 0543cd6c5..6646830cd 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -17,6 +17,8 @@ import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib"; import { BaseTreeViewer } from "./base_tree_viewer.js"; import { viewerCompatibilityParams } from "./viewer_compatibility.js"; +const PdfFileRegExp = /\.pdf$/i; + /** * @typedef {Object} PDFAttachmentViewerOptions * @property {HTMLDivElement} container - The viewer element. @@ -136,7 +138,8 @@ class PDFAttachmentViewer extends BaseTreeViewer { */ _bindLink(element, { content, filename }) { element.onclick = () => { - this.downloadManager.downloadData(content, filename, ""); + const contentType = PdfFileRegExp.test(filename) ? "application/pdf" : ""; + this.downloadManager.downloadData(content, filename, contentType); return false; }; } @@ -169,7 +172,7 @@ class PDFAttachmentViewer extends BaseTreeViewer { const element = document.createElement("a"); if ( - /\.pdf$/i.test(filename) && + PdfFileRegExp.test(filename) && !viewerCompatibilityParams.disableCreateObjectURL ) { this._bindPdfLink(element, { content: item.content, filename }); From 2a0de0b66b41c345cd357cbb828b3ca6f49fd395 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 27 Aug 2020 16:01:45 +0200 Subject: [PATCH 3/3] Download, rather than opening, PDF attachments in Firefox (bug 1661259, PR 12286 follow-up) Unfortunately the work-around implemented in PR 12286 didn't actually work in all cases, please refer to the previous commit messages. To prevent opening of PDF attachments from being completely broken for some users, we'll simply force-download them for now in MOZCENTRAL-builds to unbreak things. (Given that the "Open with" dialog now features a "Open with Firefox"-option, this is less bad than it previously would've been.) --- web/pdf_attachment_viewer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/web/pdf_attachment_viewer.js b/web/pdf_attachment_viewer.js index 6646830cd..079b083ca 100644 --- a/web/pdf_attachment_viewer.js +++ b/web/pdf_attachment_viewer.js @@ -172,6 +172,7 @@ class PDFAttachmentViewer extends BaseTreeViewer { const element = document.createElement("a"); if ( + (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) && PdfFileRegExp.test(filename) && !viewerCompatibilityParams.disableCreateObjectURL ) {