Jelajahi Sumber

get frame contents sooner to avoid any exception

Gildas 7 tahun lalu
induk
melakukan
01882d6e1f

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

@@ -91,7 +91,7 @@ singlefile.core = (() => {
 
 	async function processStart(tab, options) {
 		if (!options.removeFrames) {
-			await FrameTree.initialize(tab.id);
+			await FrameTree.initialize(tab.id, options);
 		}
 		await executeScripts(tab.id, contentScriptFiles, { allFrames: false });
 		if (options.frameId) {

+ 2 - 2
lib/single-file/frame-tree/bg/frame-tree.js

@@ -32,7 +32,7 @@ this.FrameTree = (() => {
 	});
 
 	return {
-		async initialize(tabId) {
+		async initialize(tabId, options) {
 			return new Promise(resolve => {
 				const onMessage = message => {
 					if (message.method == "FrameTree.initResponse") {
@@ -41,7 +41,7 @@ this.FrameTree = (() => {
 					}
 				};
 				browser.runtime.onMessage.addListener(onMessage);
-				browser.tabs.sendMessage(tabId, { method: "FrameTree.initRequest", windowId: "0", index: 0 })
+				browser.tabs.sendMessage(tabId, { method: "FrameTree.initRequest", windowId: "0", index: 0, options })
 					.catch(() => { /* ignored */ });
 			});
 		}

+ 10 - 13
lib/single-file/frame-tree/content/frame-tree.js

@@ -125,7 +125,7 @@ this.FrameTree = this.FrameTree || (() => {
 		FrameTree.index = message.index;
 		const frameElements = document.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
 		if (frameElements.length) {
-			setFramesWinId(MESSAGE_PREFIX, frameElements, FrameTree.index, FrameTree.windowId, window);
+			setFramesWinId(MESSAGE_PREFIX, frameElements, message.options, FrameTree.index, FrameTree.windowId, window);
 		} else {
 			top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
 		}
@@ -152,7 +152,7 @@ this.FrameTree = this.FrameTree || (() => {
 		}
 	}
 
-	function setFramesWinId(MESSAGE_PREFIX, frameElements, index, windowId, win) {
+	function setFramesWinId(MESSAGE_PREFIX, frameElements, options, index, windowId, win) {
 		const framesData = [];
 		if (win != top) {
 			win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, index }), "*");
@@ -172,6 +172,7 @@ this.FrameTree = this.FrameTree || (() => {
 			const frameWinId = windowId + "." + index;
 			frameElement.setAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME, frameWinId);
 			let frameDoc, frameWindow, topWindow;
+			let content, emptyStyleRulesText, canvasData;
 			try {
 				frameDoc = frameElement.contentDocument;
 				frameWindow = frameElement.contentWindow;
@@ -180,10 +181,15 @@ this.FrameTree = this.FrameTree || (() => {
 				/* ignored */
 			}
 			if (frameWindow && frameDoc && topWindow) {
-				setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), index, frameWinId, frameWindow);
+				setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), options, index, frameWinId, frameWindow);
 				topWindow.addEventListener("message", onMessage, false);
+				docHelper.preProcessDoc(frameDoc, frameWindow, options);
+				content = docHelper.getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
+				emptyStyleRulesText = docHelper.getEmptyStyleRulesText(frameDoc);
+				canvasData = docHelper.getCanvasData(frameDoc);
+				docHelper.postProcessDoc(frameDoc, options);
 			} else if (frameWindow) {
-				frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
+				frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index, options }), "*");
 				timeout.set(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
 			}
 
@@ -192,16 +198,7 @@ this.FrameTree = this.FrameTree || (() => {
 					const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
 					if (message.method == "getDataRequest" && message.windowId == frameWinId) {
 						topWindow.removeEventListener("message", onMessage, false);
-						try {
-							docHelper.preProcessDoc(frameDoc, frameWindow, message.options);
-						} catch (error) {
-							/* ignored */
-						}
-						const content = docHelper.getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
-						const emptyStyleRulesText = docHelper.getEmptyStyleRulesText(frameDoc);
-						const canvasData = docHelper.getCanvasData(frameDoc);
 						top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title, emptyStyleRulesText, canvasData }), "*");
-						docHelper.postProcessDoc(frameDoc, message.options);
 					}
 				}
 			}