Merge pull request #12802 from Snuffleupagus/FirefoxCom-requestAsync
Add a new `FirefoxCom.requestAsync` method, to simplify the code in `web/firefoxcom.js`
This commit is contained in:
commit
c3e02b3471
@ -25,69 +25,78 @@ 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 requestAsync() instead since one day we may
|
||||||
* NOTE: It is reccomended to use request() instead since one day we may not
|
* 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
|
||||||
* 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
|
* @returns {Promise<any>} A promise that is resolved with the response data.
|
||||||
* with one data argument.
|
*/
|
||||||
*/
|
static requestAsync(action, data) {
|
||||||
request(action, data, callback) {
|
return new Promise(resolve => {
|
||||||
const request = document.createTextNode("");
|
this.request(action, data, resolve);
|
||||||
if (callback) {
|
});
|
||||||
request.addEventListener(
|
}
|
||||||
"pdf.js.response",
|
|
||||||
event => {
|
|
||||||
const response = event.detail.response;
|
|
||||||
event.target.remove();
|
|
||||||
|
|
||||||
callback(response);
|
/**
|
||||||
},
|
* Creates an event that the extension is listening for and will, optionally,
|
||||||
{ once: true }
|
* asynchronously respond to.
|
||||||
);
|
* @param {string} action - The action to trigger.
|
||||||
}
|
* @param {Object|string} [data] - The data to send.
|
||||||
document.documentElement.appendChild(request);
|
*/
|
||||||
|
static request(action, data, callback = null) {
|
||||||
|
const request = document.createTextNode("");
|
||||||
|
if (callback) {
|
||||||
|
request.addEventListener(
|
||||||
|
"pdf.js.response",
|
||||||
|
event => {
|
||||||
|
const response = event.detail.response;
|
||||||
|
event.target.remove();
|
||||||
|
|
||||||
const sender = document.createEvent("CustomEvent");
|
callback(response);
|
||||||
sender.initCustomEvent("pdf.js.message", true, false, {
|
},
|
||||||
action,
|
{ once: true }
|
||||||
data,
|
);
|
||||||
sync: false,
|
}
|
||||||
responseExpected: !!callback,
|
document.documentElement.appendChild(request);
|
||||||
});
|
|
||||||
return request.dispatchEvent(sender);
|
const sender = document.createEvent("CustomEvent");
|
||||||
},
|
sender.initCustomEvent("pdf.js.message", true, false, {
|
||||||
};
|
action,
|
||||||
})();
|
data,
|
||||||
|
sync: false,
|
||||||
|
responseExpected: !!callback,
|
||||||
|
});
|
||||||
|
request.dispatchEvent(sender);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class DownloadManager {
|
class DownloadManager {
|
||||||
downloadUrl(url, filename) {
|
downloadUrl(url, filename) {
|
||||||
@ -101,58 +110,42 @@ class DownloadManager {
|
|||||||
const blobUrl = URL.createObjectURL(
|
const blobUrl = URL.createObjectURL(
|
||||||
new Blob([data], { type: contentType })
|
new Blob([data], { type: contentType })
|
||||||
);
|
);
|
||||||
const onResponse = err => {
|
|
||||||
URL.revokeObjectURL(blobUrl);
|
|
||||||
};
|
|
||||||
|
|
||||||
FirefoxCom.request(
|
FirefoxCom.requestAsync("download", {
|
||||||
"download",
|
blobUrl,
|
||||||
{
|
originalUrl: blobUrl,
|
||||||
blobUrl,
|
filename,
|
||||||
originalUrl: blobUrl,
|
isAttachment: true,
|
||||||
filename,
|
}).then(error => {
|
||||||
isAttachment: true,
|
URL.revokeObjectURL(blobUrl);
|
||||||
},
|
});
|
||||||
onResponse
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
download(blob, url, filename, sourceEventType = "download") {
|
download(blob, url, filename, sourceEventType = "download") {
|
||||||
const blobUrl = URL.createObjectURL(blob);
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
const onResponse = err => {
|
|
||||||
if (err && this.onerror) {
|
FirefoxCom.requestAsync("download", {
|
||||||
this.onerror(err);
|
blobUrl,
|
||||||
|
originalUrl: url,
|
||||||
|
filename,
|
||||||
|
sourceEventType,
|
||||||
|
}).then(error => {
|
||||||
|
if (error && this.onerror) {
|
||||||
|
this.onerror(error);
|
||||||
}
|
}
|
||||||
URL.revokeObjectURL(blobUrl);
|
URL.revokeObjectURL(blobUrl);
|
||||||
};
|
});
|
||||||
|
|
||||||
FirefoxCom.request(
|
|
||||||
"download",
|
|
||||||
{
|
|
||||||
blobUrl,
|
|
||||||
originalUrl: url,
|
|
||||||
filename,
|
|
||||||
sourceEventType,
|
|
||||||
},
|
|
||||||
onResponse
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FirefoxPreferences extends BasePreferences {
|
class FirefoxPreferences extends BasePreferences {
|
||||||
async _writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return new Promise(function (resolve) {
|
return FirefoxCom.requestAsync("setPreferences", prefObj);
|
||||||
FirefoxCom.request("setPreferences", prefObj, resolve);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return new Promise(function (resolve) {
|
const prefStr = await FirefoxCom.requestAsync("getPreferences", prefObj);
|
||||||
FirefoxCom.request("getPreferences", prefObj, function (prefStr) {
|
return JSON.parse(prefStr);
|
||||||
const readPrefs = JSON.parse(prefStr);
|
|
||||||
resolve(readPrefs);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,13 +251,10 @@ class FirefoxComDataRangeTransport extends PDFDataRangeTransport {
|
|||||||
|
|
||||||
class FirefoxScripting {
|
class FirefoxScripting {
|
||||||
static async createSandbox(data) {
|
static async createSandbox(data) {
|
||||||
return new Promise(resolve => {
|
const success = await FirefoxCom.requestAsync("createSandbox", data);
|
||||||
FirefoxCom.request("createSandbox", data, resolve);
|
if (!success) {
|
||||||
}).then(success => {
|
throw new Error("Cannot create sandbox.");
|
||||||
if (!success) {
|
}
|
||||||
throw new Error("Cannot create sandbox.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async dispatchEventInSandbox(event) {
|
static async dispatchEventInSandbox(event) {
|
||||||
@ -347,9 +337,7 @@ class FirefoxExternalServices extends DefaultExternalServices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async fallback(data) {
|
static async fallback(data) {
|
||||||
return new Promise(resolve => {
|
return FirefoxCom.requestAsync("fallback", data);
|
||||||
FirefoxCom.request("fallback", data, resolve);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static reportTelemetry(data) {
|
static reportTelemetry(data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user