|
@@ -18,7 +18,7 @@
|
|
|
* along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
|
|
* along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
-/* global browser, fetch, XPCNativeWrapper, wrappedJSObject */
|
|
|
|
|
|
|
+/* global browser, fetch, XMLHttpRequest, XPCNativeWrapper, wrappedJSObject */
|
|
|
|
|
|
|
|
this.superFetch = this.superFetch || (() => {
|
|
this.superFetch = this.superFetch || (() => {
|
|
|
|
|
|
|
@@ -38,11 +38,36 @@ this.superFetch = this.superFetch || (() => {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
- if (typeof XPCNativeWrapper != "undefined" && typeof wrappedJSObject != "undefined") {
|
|
|
|
|
- superFetch.hostFetch = async url => {
|
|
|
|
|
- return await XPCNativeWrapper(wrappedJSObject.fetch)(url, { mode: "cors", credentials: "include" });
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ superFetch.hostFetch = async url => {
|
|
|
|
|
+ const xhrPromise = new Promise((resolve, reject) => {
|
|
|
|
|
+ const xhrRequest = new XMLHttpRequest();
|
|
|
|
|
+ let resolveResponse, rejectResponse;
|
|
|
|
|
+ xhrRequest.withCredentials = true;
|
|
|
|
|
+ xhrRequest.responseType = "arraybuffer";
|
|
|
|
|
+ xhrRequest.onerror = event => reject(new Error(event.details));
|
|
|
|
|
+ xhrRequest.onreadystatechange = () => {
|
|
|
|
|
+ if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
|
|
|
|
|
+ const headers = new Map();
|
|
|
|
|
+ headers.set("content-type", xhrRequest.getResponseHeader("Content-Type"));
|
|
|
|
|
+ resolve({ headers, status: xhrRequest.status, arrayBuffer: () => new Promise((resolve, reject) => [resolveResponse, rejectResponse] = [resolve, reject]) });
|
|
|
|
|
+ }
|
|
|
|
|
+ if (xhrRequest.readyState == XMLHttpRequest.DONE) {
|
|
|
|
|
+ resolveResponse(xhrRequest.response);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ xhrRequest.onerror = error => {
|
|
|
|
|
+ reject(error);
|
|
|
|
|
+ rejectResponse(error);
|
|
|
|
|
+ };
|
|
|
|
|
+ xhrRequest.open("GET", url, true);
|
|
|
|
|
+ xhrRequest.send();
|
|
|
|
|
+ });
|
|
|
|
|
+ if (typeof XPCNativeWrapper != "undefined" && typeof wrappedJSObject != "undefined") {
|
|
|
|
|
+ return xhrPromise.catch(() => XPCNativeWrapper(wrappedJSObject.fetch)(url, { mode: "cors", credentials: "include" }));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return xhrPromise;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
return superFetch;
|
|
return superFetch;
|
|
|
|
|
|
|
|
async function sendMessage(message) {
|
|
async function sendMessage(message) {
|