Sfoglia il codice sorgente

use generated id instead of url to identify requests

Gildas 7 anni fa
parent
commit
7e1d6e7687

+ 6 - 6
extension/core/scripts/bg/fetch.js

@@ -33,29 +33,29 @@
 			send(response);
 		}
 
-		function sendFetchResponse(response) {
-			fetchResponses.set(request.url, response);
+		function sendFetchResponse(requestId, response) {
+			fetchResponses.set(requestId, response);
 			const headers = {};
 			for (let headerName of response.headers.keys()) {
 				headers[headerName] = response.headers.get(headerName);
 			}
-			sendResponse({ headers });
+			sendResponse({ requestId, headers });
 		}
 
 		if (request.method) {
 			if (request.method == "fetch") {
 				fetch(request.url, request.options)
-					.then(sendFetchResponse)
+					.then(response => sendFetchResponse(request.requestId, response))
 					.catch(error => sendResponse({ error: error.toString() }));
 			}
 			if (request.method == "fetch.uint8array") {
-				const content = fetchResponses.get(request.url);
+				const content = fetchResponses.get(request.requestId);
 				content.arrayBuffer()
 					.then(buffer => sendResponse({ uint8array: Array.from(new Uint8Array(buffer)) }))
 					.catch(error => sendResponse({ error: error.toString() }));
 			}
 			if (request.method == "fetch.text") {
-				const content = fetchResponses.get(request.url);
+				const content = fetchResponses.get(request.requestId);
 				content.text()
 					.then(text => sendResponse({ text }))
 					.catch(error => sendResponse({ error: error.toString() }));

+ 14 - 8
extension/core/scripts/content/fetch.js

@@ -22,35 +22,41 @@
 
 window.fetch = (() => {
 
+	let requestId = 1;
+
 	return async (url, options) => {
-		const response = await chromeRuntimeSendMessage({ method: "fetch", url, options });
+		const responseFetch = await chromeRuntimeSendMessage({ method: "fetch", url, options });
 		return {
-			headers: { get: headerName => response.headers[headerName] },
+			headers: { get: headerName => responseFetch.headers[headerName] },
 			arrayBuffer: async () => {
-				const response = await chromeRuntimeSendMessage({ method: "fetch.uint8array", url });
+				const response = await chromeRuntimeSendMessage({ method: "fetch.uint8array", requestId: responseFetch.requestId });
 				return new Uint8Array(response.uint8array).buffer;
 			},
 			blob: async () => {
-				const response = await chromeRuntimeSendMessage({ method: "fetch.uint8array", url });
+				const response = await chromeRuntimeSendMessage({ method: "fetch.uint8array", requestId: responseFetch.requestId });
 				return new Blob([new Uint8Array(response.uint8array)]);
 			},
 			text: async () => {
-				const response = await chromeRuntimeSendMessage({ method: "fetch.text", url });
+				const response = await chromeRuntimeSendMessage({ method: "fetch.text", requestId: responseFetch.requestId });
 				return response.text;
 			}
 		};
 	};
 
 	function chromeRuntimeSendMessage(message) {
-		return new Promise((resolve, reject) =>
+		return new Promise((resolve, reject) => {
+			if (!message.requestId) {
+				message.requestId = requestId;
+				requestId = requestId + 1;
+			}
 			chrome.runtime.sendMessage(message, response => {
 				if (response.error) {
 					reject(response.error);
 				} else {
 					resolve(response);
 				}
-			})
-		);
+			});
+		});
 	}
 
 })();