Browse Source

Merge pull request #8116 from Snuffleupagus/download_manager-preprocessor

Get rid of a couple `eslint-disable` statements in `web/download_manager.js` by instead relying on the preprocessor dead-code removal added in PR 7942
Tim van der Meij 8 years ago committed by GitHub
parent
commit
522281c01d
  1. 141
      web/download_manager.js

141
web/download_manager.js

@ -25,86 +25,87 @@
factory((root.pdfjsWebDownloadManager = {}), root.pdfjsWebPDFJS); factory((root.pdfjsWebDownloadManager = {}), root.pdfjsWebPDFJS);
} }
}(this, function (exports, pdfjsLib) { }(this, function (exports, pdfjsLib) {
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC || CHROME')) { if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
// eslint-disable-next-line no-inner-declarations return;
function download(blobUrl, filename) { }
var a = document.createElement('a');
if (a.click) { function download(blobUrl, filename) {
// Use a.click() if available. Otherwise, Chrome might show var a = document.createElement('a');
// "Unsafe JavaScript attempt to initiate a navigation change if (a.click) {
// for frame with URL" and not open the PDF at all. // Use a.click() if available. Otherwise, Chrome might show
// Supported by (not mentioned = untested): // "Unsafe JavaScript attempt to initiate a navigation change
// - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click) // for frame with URL" and not open the PDF at all.
// - Chrome 19 - 26 (18- does not support a.click) // Supported by (not mentioned = untested):
// - Opera 9 - 12.15 // - Firefox 6 - 19 (4- does not support a.click, 5 ignores a.click)
// - Internet Explorer 6 - 10 // - Chrome 19 - 26 (18- does not support a.click)
// - Safari 6 (5.1- does not support a.click) // - Opera 9 - 12.15
a.href = blobUrl; // - Internet Explorer 6 - 10
a.target = '_parent'; // - Safari 6 (5.1- does not support a.click)
// Use a.download if available. This increases the likelihood that a.href = blobUrl;
// the file is downloaded instead of opened by another PDF plugin. a.target = '_parent';
if ('download' in a) { // Use a.download if available. This increases the likelihood that
a.download = filename; // the file is downloaded instead of opened by another PDF plugin.
} if ('download' in a) {
// <a> must be in the document for IE and recent Firefox versions. a.download = filename;
// (otherwise .click() is ignored)
(document.body || document.documentElement).appendChild(a);
a.click();
a.parentNode.removeChild(a);
} else {
if (window.top === window &&
blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
// If _parent == self, then opening an identical URL with different
// location hash will only cause a navigation, not a download.
var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
}
window.open(blobUrl, '_parent');
} }
// <a> must be in the document for IE and recent Firefox versions.
// (otherwise .click() is ignored)
(document.body || document.documentElement).appendChild(a);
a.click();
a.parentNode.removeChild(a);
} else {
if (window.top === window &&
blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
// If _parent == self, then opening an identical URL with different
// location hash will only cause a navigation, not a download.
var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
}
window.open(blobUrl, '_parent');
} }
}
function DownloadManager() {} // eslint-disable-line no-inner-declarations function DownloadManager() {}
DownloadManager.prototype = {
downloadUrl: function DownloadManager_downloadUrl(url, filename) {
if (!pdfjsLib.createValidAbsoluteUrl(url, 'http://example.com')) {
return; // restricted/invalid URL
}
download(url + '#pdfjs.action=download', filename);
},
downloadData: function DownloadManager_downloadData(data, filename, DownloadManager.prototype = {
contentType) { downloadUrl: function DownloadManager_downloadUrl(url, filename) {
if (navigator.msSaveBlob) { // IE10 and above if (!pdfjsLib.createValidAbsoluteUrl(url, 'http://example.com')) {
return navigator.msSaveBlob(new Blob([data], { type: contentType }), return; // restricted/invalid URL
filename); }
} download(url + '#pdfjs.action=download', filename);
},
var blobUrl = pdfjsLib.createObjectURL(data, contentType, downloadData: function DownloadManager_downloadData(data, filename,
pdfjsLib.PDFJS.disableCreateObjectURL); contentType) {
download(blobUrl, filename); if (navigator.msSaveBlob) { // IE10 and above
}, return navigator.msSaveBlob(new Blob([data], { type: contentType }),
filename);
}
download: function DownloadManager_download(blob, url, filename) { var blobUrl = pdfjsLib.createObjectURL(data, contentType,
if (navigator.msSaveBlob) { pdfjsLib.PDFJS.disableCreateObjectURL);
// IE10 / IE11 download(blobUrl, filename);
if (!navigator.msSaveBlob(blob, filename)) { },
this.downloadUrl(url, filename);
}
return;
}
if (pdfjsLib.PDFJS.disableCreateObjectURL) { download: function DownloadManager_download(blob, url, filename) {
// URL.createObjectURL is not supported if (navigator.msSaveBlob) {
// IE10 / IE11
if (!navigator.msSaveBlob(blob, filename)) {
this.downloadUrl(url, filename); this.downloadUrl(url, filename);
return;
} }
return;
}
var blobUrl = URL.createObjectURL(blob); if (pdfjsLib.PDFJS.disableCreateObjectURL) {
download(blobUrl, filename); // URL.createObjectURL is not supported
this.downloadUrl(url, filename);
return;
} }
};
exports.DownloadManager = DownloadManager; var blobUrl = URL.createObjectURL(blob);
} download(blobUrl, filename);
}
};
exports.DownloadManager = DownloadManager;
})); }));

Loading…
Cancel
Save