Merge pull request #11845 from Snuffleupagus/less-createObjectURL
Use the native `URL.createObjectURL` method more in the `web/` folder
This commit is contained in:
commit
7363308b97
@ -14,8 +14,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import "../extensions/firefox/tools/l10n.js";
|
import "../extensions/firefox/tools/l10n.js";
|
||||||
import { createObjectURL, PDFDataRangeTransport, shadow } from "pdfjs-lib";
|
|
||||||
import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
|
import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
|
||||||
|
import { PDFDataRangeTransport, shadow } from "pdfjs-lib";
|
||||||
import { BasePreferences } from "./preferences.js";
|
import { BasePreferences } from "./preferences.js";
|
||||||
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
|
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
|
||||||
|
|
||||||
@ -100,14 +100,23 @@ class DownloadManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
downloadData(data, filename, contentType) {
|
downloadData(data, filename, contentType) {
|
||||||
const blobUrl = createObjectURL(data, contentType);
|
const blobUrl = URL.createObjectURL(
|
||||||
|
new Blob([data], { type: contentType })
|
||||||
|
);
|
||||||
|
const onResponse = err => {
|
||||||
|
URL.revokeObjectURL(blobUrl);
|
||||||
|
};
|
||||||
|
|
||||||
FirefoxCom.request("download", {
|
FirefoxCom.request(
|
||||||
blobUrl,
|
"download",
|
||||||
originalUrl: blobUrl,
|
{
|
||||||
filename,
|
blobUrl,
|
||||||
isAttachment: true,
|
originalUrl: blobUrl,
|
||||||
});
|
filename,
|
||||||
|
isAttachment: true,
|
||||||
|
},
|
||||||
|
onResponse
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
download(blob, url, filename) {
|
download(blob, url, filename) {
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createObjectURL,
|
|
||||||
createPromiseCapability,
|
createPromiseCapability,
|
||||||
getFilenameFromUrl,
|
getFilenameFromUrl,
|
||||||
removeNullCharacters,
|
removeNullCharacters,
|
||||||
@ -84,14 +83,19 @@ class PDFAttachmentViewer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let blobUrl;
|
let blobUrl;
|
||||||
button.onclick = function () {
|
button.onclick = () => {
|
||||||
if (!blobUrl) {
|
if (!blobUrl) {
|
||||||
blobUrl = createObjectURL(content, "application/pdf");
|
blobUrl = URL.createObjectURL(
|
||||||
|
new Blob([content], { type: "application/pdf" })
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let viewerUrl;
|
let viewerUrl;
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||||
// The current URL is the viewer, let's use it and append the file.
|
// The current URL is the viewer, let's use it and append the file.
|
||||||
viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
|
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")) {
|
} else if (PDFJSDev.test("CHROME")) {
|
||||||
// In the Chrome extension, the URL is rewritten using the history API
|
// In the Chrome extension, the URL is rewritten using the history API
|
||||||
// in viewer.js, so an absolute URL must be generated.
|
// in viewer.js, so an absolute URL must be generated.
|
||||||
@ -100,11 +104,17 @@ class PDFAttachmentViewer {
|
|||||||
chrome.runtime.getURL("/content/web/viewer.html") +
|
chrome.runtime.getURL("/content/web/viewer.html") +
|
||||||
"?file=" +
|
"?file=" +
|
||||||
encodeURIComponent(blobUrl + "#" + filename);
|
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;
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user