Gildas 5 лет назад
Родитель
Сommit
8556d933b4

+ 9 - 7
extension/core/content/content-bootstrap.js

@@ -24,7 +24,7 @@
 /* global browser, window, addEventListener, removeEventListener, document, location, setTimeout, prompt, Node */
 
 this.extension.core.content.bootstrap = this.extension.core.content.bootstrap || (() => {
-
+	
 	const singlefile = this.singlefile;
 	const extension = this.extension;
 
@@ -126,6 +126,7 @@ this.extension.core.content.bootstrap = this.extension.core.content.bootstrap ||
 				await new Promise(resolve => autoSaveTimeout = setTimeout(resolve, options.autoSaveDelay * 1000));
 				await autoSavePage();
 			} else {
+				const waitForUserScript = this._singleFile_waitForUserScript;
 				let frames = [];
 				let framesSessionId;
 				autoSaveTimeout = null;
@@ -133,8 +134,8 @@ this.extension.core.content.bootstrap = this.extension.core.content.bootstrap ||
 					frames = await singlefile.processors.frameTree.getAsync(options);
 				}
 				framesSessionId = frames && frames.sessionId;
-				if (options.userScriptEnabled && helper.waitForUserScript.callback) {
-					await helper.waitForUserScript.callback(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
+				if (options.userScriptEnabled && waitForUserScript) {
+					await waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 				}
 				const docData = helper.preProcessDoc(document, window, options);
 				savePage(docData, frames);
@@ -142,8 +143,8 @@ this.extension.core.content.bootstrap = this.extension.core.content.bootstrap ||
 					singlefile.processors.frameTree.cleanup(framesSessionId);
 				}
 				helper.postProcessDoc(document, docData.markedElements);
-				if (options.userScriptEnabled && helper.waitForUserScript.callback) {
-					await helper.waitForUserScript.callback(helper.ON_AFTER_CAPTURE_EVENT_NAME);
+				if (options.userScriptEnabled && waitForUserScript) {
+					await waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
 				}
 				pageAutoSaved = true;
 				autoSavingPage = false;
@@ -166,12 +167,13 @@ this.extension.core.content.bootstrap = this.extension.core.content.bootstrap ||
 	function onUnload() {
 		const helper = singlefile.helper;
 		if (!pageAutoSaved || options.autoSaveUnload) {
+			const waitForUserScript = this._singleFile_waitForUserScript;
 			let frames = [];
 			if (!options.removeFrames && window.frames && window.frames.length) {
 				frames = singlefile.processors.frameTree.getSync(options);
 			}
-			if (options.userScriptEnabled && helper.waitForUserScript.callback) {
-				helper.waitForUserScript.callback(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
+			if (options.userScriptEnabled && waitForUserScript) {
+				waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 			}
 			const docData = helper.preProcessDoc(document, window, options);
 			savePage(docData, frames);

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

@@ -28,7 +28,6 @@ import {
 	ON_BEFORE_CAPTURE_EVENT_NAME,
 	ON_AFTER_CAPTURE_EVENT_NAME,
 	WIN_ID_ATTRIBUTE_NAME,
-	waitForUserScript,
 	preProcessDoc,
 	serialize,
 	postProcessDoc,
@@ -39,7 +38,6 @@ const helper = {
 	ON_BEFORE_CAPTURE_EVENT_NAME,
 	ON_AFTER_CAPTURE_EVENT_NAME,
 	WIN_ID_ATTRIBUTE_NAME,
-	waitForUserScript,
 	preProcessDoc,
 	serialize,
 	postProcessDoc,
@@ -160,38 +158,38 @@ function getNewSessionId() {
 }
 
 function initRequestSync(message) {
-	const waitForUserScript = helper.waitForUserScript;
 	const sessionId = message.sessionId;
+	const waitForUserScript = globalThis._singleFile_waitForUserScript;
 	if (!TOP_WINDOW) {
 		windowId = globalThis.frameId = message.windowId;
 	}
 	processFrames(document, message.options, windowId, sessionId);
 	if (!TOP_WINDOW) {
-		if (message.options.userScriptEnabled && waitForUserScript.callback) {
-			waitForUserScript.callback(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
+		if (message.options.userScriptEnabled && waitForUserScript) {
+			waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 		}
 		sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
-		if (message.options.userScriptEnabled && waitForUserScript.callback) {
-			waitForUserScript.callback(helper.ON_AFTER_CAPTURE_EVENT_NAME);
+		if (message.options.userScriptEnabled && waitForUserScript) {
+			waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
 		}
 		delete document.documentElement.dataset.requestedFrameId;
 	}
 }
 
 async function initRequestAsync(message) {
-	const waitForUserScript = helper.waitForUserScript;
 	const sessionId = message.sessionId;
+	const waitForUserScript = globalThis._singleFile_waitForUserScript;
 	if (!TOP_WINDOW) {
 		windowId = globalThis.frameId = message.windowId;
 	}
 	processFrames(document, message.options, windowId, sessionId);
 	if (!TOP_WINDOW) {
-		if (message.options.userScriptEnabled && waitForUserScript.callback) {
-			await waitForUserScript.callback(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
+		if (message.options.userScriptEnabled && waitForUserScript) {
+			await waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 		}
 		sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
-		if (message.options.userScriptEnabled && waitForUserScript.callback) {
-			await waitForUserScript.callback(helper.ON_AFTER_CAPTURE_EVENT_NAME);
+		if (message.options.userScriptEnabled && waitForUserScript) {
+			await waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
 		}
 		delete document.documentElement.dataset.requestedFrameId;
 	}

+ 3 - 2
lib/single-file/single-file-bootstrap.js

@@ -4,7 +4,7 @@ import {
 	COMMENT_HEADER_LEGACY,
 	ON_BEFORE_CAPTURE_EVENT_NAME,
 	ON_AFTER_CAPTURE_EVENT_NAME,
-	waitForUserScript,
+	initUserScriptHandler,
 	preProcessDoc,
 	postProcessDoc,
 	serialize,
@@ -17,13 +17,14 @@ const helper = {
 	COMMENT_HEADER_LEGACY,
 	ON_BEFORE_CAPTURE_EVENT_NAME,
 	ON_AFTER_CAPTURE_EVENT_NAME,
-	waitForUserScript,
 	preProcessDoc,
 	postProcessDoc,
 	serialize,
 	getShadowRoot
 };
 
+initUserScriptHandler();
+
 export {
 	helper,
 	processors

+ 5 - 4
lib/single-file/single-file-core.js

@@ -40,14 +40,15 @@ class SingleFileClass {
 		this.options = options;
 	}
 	async run() {
-		if (this.options.userScriptEnabled && util.waitForUserScript.callback) {
-			await util.waitForUserScript.callback(util.ON_BEFORE_CAPTURE_EVENT_NAME);
+		const waitForUserScript = globalThis._singleFile_waitForUserScript;
+		if (this.options.userScriptEnabled && waitForUserScript) {
+			await waitForUserScript(util.ON_BEFORE_CAPTURE_EVENT_NAME);
 		}
 		this.runner = new Runner(this.options, true);
 		await this.runner.loadPage();
 		await this.runner.initialize();
-		if (this.options.userScriptEnabled && util.waitForUserScript.callback) {
-			await util.waitForUserScript.callback(util.ON_AFTER_CAPTURE_EVENT_NAME);
+		if (this.options.userScriptEnabled && waitForUserScript) {
+			await waitForUserScript(util.ON_AFTER_CAPTURE_EVENT_NAME);
 		}
 		await this.runner.run();
 	}

+ 12 - 12
lib/single-file/single-file-helper.js

@@ -62,19 +62,8 @@ const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
 const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
 const dispatchEvent = event => globalThis.dispatchEvent(event);
 
-let waitForUserScript = {};
-
-addEventListener("single-file-user-script-init", () => waitForUserScript.callback = async eventPrefixName => {
-	const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true });
-	const promiseResponse = new Promise(resolve => addEventListener(eventPrefixName + "-response", resolve));
-	dispatchEvent(event);
-	if (event.defaultPrevented) {
-		await promiseResponse;
-	}
-});
-
 export {
-	waitForUserScript,
+	initUserScriptHandler,
 	initDoc,
 	preProcessDoc,
 	postProcessDoc,
@@ -106,6 +95,17 @@ export {
 	SINGLE_FILE_UI_ELEMENT_CLASS
 };
 
+function initUserScriptHandler() {
+	addEventListener("single-file-user-script-init", () => globalThis._singleFile_waitForUserScript = async eventPrefixName => {
+		const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true });
+		const promiseResponse = new Promise(resolve => addEventListener(eventPrefixName + "-response", resolve));
+		dispatchEvent(event);
+		if (event.defaultPrevented) {
+			await promiseResponse;
+		}
+	});	
+}
+
 function initDoc(doc) {
 	doc.querySelectorAll("meta[http-equiv=refresh]").forEach(element => {
 		element.removeAttribute("http-equiv");

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

@@ -161,7 +161,6 @@ function getInstance(utilOptions) {
 		removeQuotes(string) {
 			return helper.removeQuotes(string);
 		},
-		waitForUserScript: helper.waitForUserScript,
 		ON_BEFORE_CAPTURE_EVENT_NAME: helper.ON_BEFORE_CAPTURE_EVENT_NAME,
 		ON_AFTER_CAPTURE_EVENT_NAME: helper.ON_AFTER_CAPTURE_EVENT_NAME,
 		WIN_ID_ATTRIBUTE_NAME: helper.WIN_ID_ATTRIBUTE_NAME,