Преглед изворни кода

implemented integration with userscripts

Former-commit-id: feea16fa18bd8c58783fa15ee7d18e4c8554ed53
Gildas пре 6 година
родитељ
комит
281fea43a8
2 измењених фајлова са 18 додато и 1 уклоњено
  1. 15 1
      extension/core/content/content-bootstrap.js
  2. 3 0
      extension/core/content/content-main.js

+ 15 - 1
extension/core/content/content-bootstrap.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global browser, window, addEventListener, removeEventListener, document, location, setTimeout */
+/* global browser, window, addEventListener, removeEventListener, document, location, setTimeout, CustomEvent, dispatchEvent */
 
 this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.core.content.bootstrap || (async () => {
 
@@ -37,6 +37,14 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 	browser.runtime.onMessage.addListener(message => { onMessage(message); });
 	browser.runtime.sendMessage({ method: "ui.processInit" });
 	addEventListener("single-file-push-state", () => browser.runtime.sendMessage({ method: "ui.processInit" }));
+	addEventListener("single-file-user-script-init", () => singlefile.waitForUserScript = async () => {
+		const event = new CustomEvent("single-file-on-capture-request", { cancelable: true });
+		const promiseResponse = new Promise(resolve => addEventListener("single-file-on-capture-response", resolve));
+		dispatchEvent(event);
+		if (event.defaultPrevented) {
+			await promiseResponse;
+		}
+	});
 	return {};
 
 	async function onMessage(message) {
@@ -77,6 +85,9 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 				if (!options.removeFrames && singlefile.lib.frameTree.content.frames && window.frames && window.frames.length) {
 					frames = await singlefile.lib.frameTree.content.frames.getAsync(options);
 				}
+				if (singlefile.waitForUserScript) {
+					await singlefile.waitForUserScript();
+				}
 				savePage(docData, frames);
 				helper.postProcessDoc(document, docData.markedElements);
 				pageAutoSaved = true;
@@ -107,6 +118,9 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 			if (!options.removeFrames && singlefile.lib.frameTree.content.frames && window.frames && window.frames.length) {
 				frames = singlefile.lib.frameTree.content.frames.getSync(options);
 			}
+			if (singlefile.waitForUserScript) {
+				singlefile.waitForUserScript();
+			}
 			savePage(docData, frames);
 		}
 	}

+ 3 - 0
extension/core/content/content-main.js

@@ -185,6 +185,9 @@ this.singlefile.extension.core.content.main = this.singlefile.extension.core.con
 			options.doc = document;
 		}
 		if (!processor.cancelled) {
+			if (singlefile.waitForUserScript) {
+				await singlefile.waitForUserScript();
+			}
 			await processor.run();
 		}
 		if (!options.saveRawPage && !options.removeFrames && frames) {