Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download, rather than opening, PDF attachments in Firefox (bug 1661259, PR 12286 follow-up) #12293

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions web/pdf_attachment_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -118,22 +120,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";
// <a> 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...
Expand All @@ -151,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;
};
}
Expand Down Expand Up @@ -184,7 +172,8 @@ class PDFAttachmentViewer extends BaseTreeViewer {

const element = document.createElement("a");
if (
/\.pdf$/i.test(filename) &&
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) &&
PdfFileRegExp.test(filename) &&
!viewerCompatibilityParams.disableCreateObjectURL
) {
this._bindPdfLink(element, { content: item.content, filename });
Expand Down