Sfoglia il codice sorgente

fixed CORS issue on Firefox (use XHR instead of fetch)

Gildas 7 anni fa
parent
commit
4dd5dec370
2 ha cambiato i file con 32 aggiunte e 7 eliminazioni
  1. 31 6
      lib/fetch/content/fetch.js
  2. 1 1
      lib/single-file/css-fonts-minifier.js

+ 31 - 6
lib/fetch/content/fetch.js

@@ -18,7 +18,7 @@
  *   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 || (() => {
 
@@ -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;
 
 	async function sendMessage(message) {

+ 1 - 1
lib/single-file/css-fonts-minifier.js

@@ -192,7 +192,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		return stylesheetContent;
 	}
 
-	function processFontFaceRule(rule, fontInfo, stats) {
+	function processFontFaceRule(rule, fontInfo, stats) {		
 		let fontSources = fontInfo.map(fontSource => {
 			const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
 			let fontFormat;