Procházet zdrojové kódy

fetch from the host page resources with status = 403

Former-commit-id: 18438958c8bd530d489b8f8202956f2dd0ad870e
Gildas před 6 roky
rodič
revize
a1aecbf1b1

+ 1 - 3
extension/core/bg/config.js

@@ -73,9 +73,7 @@ singlefile.extension.core.bg.config = (() => {
 		removeAlternativeImages: true,
 		groupDuplicateImages: true,
 		saveRawPage: false,
-		saveToClipboard: false,
-		referrerPolicy: "origin-when-cross-origin",
-		credentials: "include"
+		saveToClipboard: false
 	};
 
 	let pendingUpgradePromise = upgrade();

+ 23 - 41
lib/fetch/content/content-fetch-resources.js

@@ -21,31 +21,19 @@
  *   Source.
  */
 
-/* global browser, fetch, XMLHttpRequest */
+/* global browser, fetch, CustomEvent, dispatchEvent, addEventListener, removeEventListener */
 
 this.singlefile.lib.fetch.content.resources = this.singlefile.lib.fetch.content.resources || (() => {
 
+	const FETCH_REQUEST_EVENT = "single-file-request-fetch";
+	const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
+
 	return {
-		fetch: async (url, options = {}) => {
-			if (!options.referrerPolicy) {
-				options.referrerPolicy = "origin-when-cross-origin";
-			}
-			if (!options.credentials) {
-				options.credentials = "include";
-			}
+		fetch: async (url) => {
 			try {
 				let response = await fetch(url, { cache: "force-cache" });
 				if (response.status == 403) {
-					response = await fetch(url, { cache: "force-cache", referrerPolicy: options.referrerPolicy });
-				}
-				if (response.status == 403) {
-					response = await fetch(url, { cache: "force-cache", credentials: options.credentials, referrerPolicy: options.referrerPolicy });
-				}
-				if (response.status == 403) {
-					response = await fetch(url, { cache: "force-cache", mode: "cors", credentials: options.credentials, referrerPolicy: options.referrerPolicy });
-				}
-				if (response.status == 403) {
-					response = await xhrFetch(url);
+					response = await hostFetch(url);
 				}
 				return response;
 			}
@@ -72,31 +60,25 @@ this.singlefile.lib.fetch.content.resources = this.singlefile.lib.fetch.content.
 		}
 	}
 
-	function xhrFetch(url) {
+	function hostFetch(url) {
 		return new Promise((resolve, reject) => {
-			const xhrRequest = new XMLHttpRequest();
-			xhrRequest.responseType = "arraybuffer";
-			xhrRequest.onerror = event => reject(new Error(event.detail));
-			xhrRequest.onreadystatechange = () => {
-				if (xhrRequest.readyState == XMLHttpRequest.HEADERS_RECEIVED || xhrRequest.readyState == XMLHttpRequest.DONE) {
-					const headers = new Map();
-					headers.set("content-type", xhrRequest.getResponseHeader("Content-Type"));
-					resolve({
-						status: xhrRequest.status,
-						headers,
-						arrayBuffer: () => new Promise((resolve, reject) => {
-							xhrRequest.onerror = event => reject(new Error(event.detail));
-							if (xhrRequest.readyState == XMLHttpRequest.DONE) {
-								resolve(xhrRequest.response);
-							} else {
-								xhrRequest.onload = () => resolve(xhrRequest.response);
-							}
-						})
-					});
+			dispatchEvent(new CustomEvent(FETCH_REQUEST_EVENT, { detail: url }));
+			addEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
+
+			function onResponseFetch(event) {
+				if (event.detail.url == url) {
+					removeEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
+					if (event.detail.response) {
+						resolve({
+							status: event.detail.status,
+							headers: event.detail.headers,
+							arrayBuffer: () => new Promise(resolve => resolve(event.detail.response))
+						});
+					} else {
+						reject(event.detail.error);
+					}
 				}
-			};
-			xhrRequest.open("GET", url, true);
-			xhrRequest.send();
+			}
 		});
 	}
 

+ 16 - 1
lib/hooks/content/content-hooks-web.js

@@ -21,10 +21,13 @@
  *   Source.
  */
 
-/* global history, dispatchEvent, CustomEvent */
+/* global history, dispatchEvent, CustomEvent, fetch, addEventListener */
 
 (() => {
 
+	const FETCH_REQUEST_EVENT = "single-file-request-fetch";
+	const FETCH_RESPONSE__EVENT = "single-file-response-fetch";
+
 	const pushState = history.pushState;
 	let warningDisplayed;
 	history.pushState = function (state, title, url) {
@@ -41,4 +44,16 @@
 	};
 	history.pushState.toString = function () { return "function pushState() { [native code] }"; };
 
+	addEventListener(FETCH_REQUEST_EVENT, async event => {
+		const url = event.detail;
+		let detail;
+		try {
+			const response = await fetch(url, { cache: "force-cache" });
+			detail = { url, response: await response.arrayBuffer(), headers: response.headers, status: response.status };
+		} catch (error) {
+			detail = { url, error };
+		}
+		dispatchEvent(new CustomEvent(FETCH_RESPONSE__EVENT, { detail }));
+	});
+
 })();

+ 6 - 18
lib/single-file/single-file-core.js

@@ -341,9 +341,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					const content = await util.getContent(resourceURL, {
 						asBinary,
 						maxResourceSize: options.maxResourceSize,
-						maxResourceSizeEnabled: options.maxResourceSizeEnabled,
-						referrerPolicy: options.referrerPolicy,
-						credentials: options.credentials
+						maxResourceSizeEnabled: options.maxResourceSizeEnabled
 					});
 					indexResource = indexResource + 1;
 					onloadListener({ url: resourceURL });
@@ -419,9 +417,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 				content = await util.getContent(this.baseURI, {
 					maxResourceSize: this.options.maxResourceSize,
 					maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
-					charset,
-					referrerPolicy: this.options.referrerPolicy,
-					credentials: this.options.credentials
+					charset
 				});
 				pageContent = content.data;
 			}
@@ -850,9 +846,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
 						url: this.options.url,
 						charset: this.charset,
-						compressCSS: this.options.compressCSS,
-						referrerPolicy: this.options.referrerPolicy,
-						credentials: this.options.credentials
+						compressCSS: this.options.compressCSS
 					};
 					let stylesheetContent;
 					if (element.tagName.toLowerCase() == "link") {
@@ -1098,9 +1092,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					const content = await util.getContent(resourceURL, {
 						asBinary: true,
 						maxResourceSize: this.options.maxResourceSize,
-						maxResourceSizeEnabled: this.options.maxResourceSizeEnabled,
-						referrerPolicy: this.options.referrerPolicy,
-						credentials: this.options.credentials
+						maxResourceSizeEnabled: this.options.maxResourceSizeEnabled
 					});
 					scriptElement.setAttribute("src", content.data);
 				}
@@ -1423,9 +1415,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 							const content = await util.getContent(resourceURL, {
 								maxResourceSize: options.maxResourceSize,
 								maxResourceSizeEnabled: options.maxResourceSizeEnabled,
-								validateTextContentType: true,
-								referrerPolicy: options.referrerPolicy,
-								credentials: options.credentials
+								validateTextContentType: true
 							});
 							resourceURL = content.resourceURL;
 							let importedStylesheetContent = Util.removeCssComments(content.data);
@@ -1491,9 +1481,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 				const content = await util.getContent(resourceURL, {
 					maxResourceSize: options.maxResourceSize,
 					maxResourceSizeEnabled: options.maxResourceSizeEnabled,
-					charset: options.charset,
-					referrerPolicy: options.referrerPolicy,
-					credentials: options.credentials
+					charset: options.charset
 				});
 				resourceURL = content.resourceURL;
 				let stylesheetContent = Util.removeCssComments(content.data);

+ 1 - 4
lib/single-file/single-file-util.js

@@ -165,10 +165,7 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 					log("  // STARTED download url =", resourceURL, "asBinary =", options.asBinary);
 				}
 				try {
-					resourceContent = await util.getResourceContent(resourceURL, {
-						referrerPolicy: options.referrerPolicy,
-						credentials: options.credentials
-					});
+					resourceContent = await util.getResourceContent(resourceURL);
 				} catch (error) {
 					return { data: options.asBinary ? "data:base64," : "", resourceURL };
 				}

+ 2 - 5
lib/single-file/single-file.js

@@ -56,11 +56,8 @@ this.singlefile.lib.SingleFile = this.singlefile.lib.SingleFile || (() => {
 		getClass: () => SingleFile
 	};
 
-	async function getResourceContent(resourceURL, options) {
-		const resourceContent = await fetchResource(resourceURL, {
-			referrerPolicy: options.referrerPolicy,
-			credentials: options.credentials
-		});
+	async function getResourceContent(resourceURL) {
+		const resourceContent = await fetchResource(resourceURL);
 		const buffer = await resourceContent.arrayBuffer();
 		return {
 			getUrl() {