ES6-ify the code in web/firefoxcom.js
These changes consists mainly of replacing `var` with `let`/`const`, and finally converting the `DownloadManager` to a proper class.
This commit is contained in:
parent
320779e6ed
commit
aaff3385ee
@ -24,7 +24,7 @@ if (typeof PDFJSDev === 'undefined' ||
|
||||
'FIREFOX and MOZCENTRAL builds.');
|
||||
}
|
||||
|
||||
var FirefoxCom = (function FirefoxComClosure() {
|
||||
let FirefoxCom = (function FirefoxComClosure() {
|
||||
return {
|
||||
/**
|
||||
* Creates an event that the extension is listening for and will
|
||||
@ -36,17 +36,18 @@ var FirefoxCom = (function FirefoxComClosure() {
|
||||
* @return {*} The response.
|
||||
*/
|
||||
requestSync(action, data) {
|
||||
var request = document.createTextNode('');
|
||||
let request = document.createTextNode('');
|
||||
document.documentElement.appendChild(request);
|
||||
|
||||
var sender = document.createEvent('CustomEvent');
|
||||
let sender = document.createEvent('CustomEvent');
|
||||
sender.initCustomEvent('pdf.js.message', true, false,
|
||||
{ action, data, sync: true, });
|
||||
request.dispatchEvent(sender);
|
||||
var response = sender.detail.response;
|
||||
let response = sender.detail.response;
|
||||
document.documentElement.removeChild(request);
|
||||
return response;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates an event that the extension is listening for and will
|
||||
* asynchronously respond by calling the callback.
|
||||
@ -56,11 +57,11 @@ var FirefoxCom = (function FirefoxComClosure() {
|
||||
* with one data argument.
|
||||
*/
|
||||
request(action, data, callback) {
|
||||
var request = document.createTextNode('');
|
||||
let request = document.createTextNode('');
|
||||
if (callback) {
|
||||
document.addEventListener('pdf.js.response', function listener(event) {
|
||||
var node = event.target;
|
||||
var response = event.detail.response;
|
||||
let node = event.target;
|
||||
let response = event.detail.response;
|
||||
|
||||
document.documentElement.removeChild(node);
|
||||
|
||||
@ -70,7 +71,7 @@ var FirefoxCom = (function FirefoxComClosure() {
|
||||
}
|
||||
document.documentElement.appendChild(request);
|
||||
|
||||
var sender = document.createEvent('CustomEvent');
|
||||
let sender = document.createEvent('CustomEvent');
|
||||
sender.initCustomEvent('pdf.js.message', true, false, {
|
||||
action,
|
||||
data,
|
||||
@ -82,48 +83,41 @@ var FirefoxCom = (function FirefoxComClosure() {
|
||||
};
|
||||
})();
|
||||
|
||||
var DownloadManager = (function DownloadManagerClosure() {
|
||||
function DownloadManager() {}
|
||||
class DownloadManager {
|
||||
downloadUrl(url, filename) {
|
||||
FirefoxCom.request('download', {
|
||||
originalUrl: url,
|
||||
filename,
|
||||
});
|
||||
}
|
||||
|
||||
DownloadManager.prototype = {
|
||||
downloadUrl: function DownloadManager_downloadUrl(url, filename) {
|
||||
FirefoxCom.request('download', {
|
||||
originalUrl: url,
|
||||
filename,
|
||||
});
|
||||
},
|
||||
downloadData(data, filename, contentType) {
|
||||
let blobUrl = createObjectURL(data, contentType, false);
|
||||
|
||||
downloadData: function DownloadManager_downloadData(data, filename,
|
||||
contentType) {
|
||||
var blobUrl = createObjectURL(data, contentType, false);
|
||||
FirefoxCom.request('download', {
|
||||
blobUrl,
|
||||
originalUrl: blobUrl,
|
||||
filename,
|
||||
isAttachment: true,
|
||||
});
|
||||
}
|
||||
|
||||
FirefoxCom.request('download', {
|
||||
blobUrl,
|
||||
originalUrl: blobUrl,
|
||||
filename,
|
||||
isAttachment: true,
|
||||
});
|
||||
},
|
||||
download(blob, url, filename) {
|
||||
let blobUrl = URL.createObjectURL(blob);
|
||||
let onResponse = (err) => {
|
||||
if (err && this.onerror) {
|
||||
this.onerror(err);
|
||||
}
|
||||
URL.revokeObjectURL(blobUrl);
|
||||
};
|
||||
|
||||
download: function DownloadManager_download(blob, url, filename) {
|
||||
let blobUrl = window.URL.createObjectURL(blob);
|
||||
let onResponse = (err) => {
|
||||
if (err && this.onerror) {
|
||||
this.onerror(err);
|
||||
}
|
||||
window.URL.revokeObjectURL(blobUrl);
|
||||
};
|
||||
|
||||
FirefoxCom.request('download', {
|
||||
blobUrl,
|
||||
originalUrl: url,
|
||||
filename,
|
||||
}, onResponse);
|
||||
},
|
||||
};
|
||||
|
||||
return DownloadManager;
|
||||
})();
|
||||
FirefoxCom.request('download', {
|
||||
blobUrl,
|
||||
originalUrl: url,
|
||||
filename,
|
||||
}, onResponse);
|
||||
}
|
||||
}
|
||||
|
||||
class FirefoxPreferences extends BasePreferences {
|
||||
_writeToStorage(prefObj) {
|
||||
@ -134,8 +128,8 @@ class FirefoxPreferences extends BasePreferences {
|
||||
|
||||
_readFromStorage(prefObj) {
|
||||
return new Promise(function(resolve) {
|
||||
FirefoxCom.request('getPreferences', prefObj, function (prefStr) {
|
||||
var readPrefs = JSON.parse(prefStr);
|
||||
FirefoxCom.request('getPreferences', prefObj, function(prefStr) {
|
||||
let readPrefs = JSON.parse(prefStr);
|
||||
resolve(readPrefs);
|
||||
});
|
||||
});
|
||||
@ -162,13 +156,13 @@ class MozL10n {
|
||||
}
|
||||
|
||||
(function listenFindEvents() {
|
||||
var events = [
|
||||
const events = [
|
||||
'find',
|
||||
'findagain',
|
||||
'findhighlightallchange',
|
||||
'findcasesensitivitychange'
|
||||
];
|
||||
var handleEvent = function (evt) {
|
||||
let handleEvent = function(evt) {
|
||||
if (!PDFViewerApplication.initialized) {
|
||||
return;
|
||||
}
|
||||
@ -183,7 +177,7 @@ class MozL10n {
|
||||
});
|
||||
};
|
||||
|
||||
for (var i = 0, len = events.length; i < len; i++) {
|
||||
for (let i = 0, len = events.length; i < len; i++) {
|
||||
window.addEventListener(events[i], handleEvent);
|
||||
}
|
||||
})();
|
||||
@ -209,7 +203,7 @@ PDFViewerApplication.externalServices = {
|
||||
},
|
||||
|
||||
initPassiveLoading(callbacks) {
|
||||
var pdfDataRangeTransport;
|
||||
let pdfDataRangeTransport;
|
||||
|
||||
window.addEventListener('message', function windowMessage(e) {
|
||||
if (e.source !== null) {
|
||||
@ -217,7 +211,7 @@ PDFViewerApplication.externalServices = {
|
||||
console.warn('Rejected untrusted message from ' + e.origin);
|
||||
return;
|
||||
}
|
||||
var args = e.data;
|
||||
let args = e.data;
|
||||
|
||||
if (typeof args !== 'object' || !('pdfjsLoadAction' in args)) {
|
||||
return;
|
||||
@ -271,28 +265,28 @@ PDFViewerApplication.externalServices = {
|
||||
},
|
||||
|
||||
createL10n() {
|
||||
var mozL10n = document.mozL10n;
|
||||
let mozL10n = document.mozL10n;
|
||||
// TODO refactor mozL10n.setExternalLocalizerServices
|
||||
return new MozL10n(mozL10n);
|
||||
},
|
||||
|
||||
get supportsIntegratedFind() {
|
||||
var support = FirefoxCom.requestSync('supportsIntegratedFind');
|
||||
let support = FirefoxCom.requestSync('supportsIntegratedFind');
|
||||
return shadow(this, 'supportsIntegratedFind', support);
|
||||
},
|
||||
|
||||
get supportsDocumentFonts() {
|
||||
var support = FirefoxCom.requestSync('supportsDocumentFonts');
|
||||
let support = FirefoxCom.requestSync('supportsDocumentFonts');
|
||||
return shadow(this, 'supportsDocumentFonts', support);
|
||||
},
|
||||
|
||||
get supportsDocumentColors() {
|
||||
var support = FirefoxCom.requestSync('supportsDocumentColors');
|
||||
let support = FirefoxCom.requestSync('supportsDocumentColors');
|
||||
return shadow(this, 'supportsDocumentColors', support);
|
||||
},
|
||||
|
||||
get supportedMouseWheelZoomModifierKeys() {
|
||||
var support = FirefoxCom.requestSync('supportedMouseWheelZoomModifierKeys');
|
||||
let support = FirefoxCom.requestSync('supportedMouseWheelZoomModifierKeys');
|
||||
return shadow(this, 'supportedMouseWheelZoomModifierKeys', support);
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user