Convert FirefoxCom to a class, with static methods

*Please note:* It's highly recommended to ignore whitespace-only changes when looking at this patch.

Besides modernizing this code, by converting it to a standard class, the existing JSDoc comments are updated to actually agree better with the way that this functionality is used now. (The next patch will reduce usage of `FirefoxCom.request` significantly, hence the JSDocs for the optional `callback` is removed to not unnecessarily advertise that functionality.)

Finally, the unnecessary/unused `return` statement at the end of `FirefoxCom.request` is also removed.
This commit is contained in:
Jonas Jenwald 2021-01-01 14:21:47 +01:00
parent 04321546ce
commit 0caf72d5bd

View File

@ -25,69 +25,65 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
); );
} }
const FirefoxCom = (function FirefoxComClosure() { class FirefoxCom {
return { /**
/** * Creates an event that the extension is listening for and will
* Creates an event that the extension is listening for and will * synchronously respond to.
* synchronously respond to. * NOTE: It is recommended to use request() instead since one day we may not
* NOTE: It is reccomended to use request() instead since one day we may not * be able to synchronously reply.
* be able to synchronously reply. * @param {string} action - The action to trigger.
* @param {string} action - The action to trigger. * @param {Object|string} [data] - The data to send.
* @param {string} [data] - The data to send. * @returns {*} The response.
* @returns {*} The response. */
*/ static requestSync(action, data) {
requestSync(action, data) { const request = document.createTextNode("");
const request = document.createTextNode(""); document.documentElement.appendChild(request);
document.documentElement.appendChild(request);
const sender = document.createEvent("CustomEvent"); const sender = document.createEvent("CustomEvent");
sender.initCustomEvent("pdf.js.message", true, false, { sender.initCustomEvent("pdf.js.message", true, false, {
action, action,
data, data,
sync: true, sync: true,
}); });
request.dispatchEvent(sender); request.dispatchEvent(sender);
const response = sender.detail.response; const response = sender.detail.response;
request.remove(); request.remove();
return response; return response;
}, }
/** /**
* Creates an event that the extension is listening for and will * Creates an event that the extension is listening for and will, optionally,
* asynchronously respond by calling the callback. * asynchronously respond to.
* @param {string} action - The action to trigger. * @param {string} action - The action to trigger.
* @param {string} [data] - The data to send. * @param {Object|string} [data] - The data to send.
* @param {Function} [callback] - Response callback that will be called */
* with one data argument. static request(action, data, callback = null) {
*/ const request = document.createTextNode("");
request(action, data, callback) { if (callback) {
const request = document.createTextNode(""); request.addEventListener(
if (callback) { "pdf.js.response",
request.addEventListener( event => {
"pdf.js.response", const response = event.detail.response;
event => { event.target.remove();
const response = event.detail.response;
event.target.remove();
callback(response); callback(response);
}, },
{ once: true } { once: true }
); );
} }
document.documentElement.appendChild(request); document.documentElement.appendChild(request);
const sender = document.createEvent("CustomEvent"); const sender = document.createEvent("CustomEvent");
sender.initCustomEvent("pdf.js.message", true, false, { sender.initCustomEvent("pdf.js.message", true, false, {
action, action,
data, data,
sync: false, sync: false,
responseExpected: !!callback, responseExpected: !!callback,
}); });
return request.dispatchEvent(sender); request.dispatchEvent(sender);
}, }
}; }
})();
class DownloadManager { class DownloadManager {
downloadUrl(url, filename) { downloadUrl(url, filename) {