Kaynağa Gözat

send messages with frame contents to a background script if applicable

Gildas 7 yıl önce
ebeveyn
işleme
ecd8db17da

+ 1 - 1
cli/back-ends/jsdom.js

@@ -32,7 +32,7 @@ const iconv = require("iconv-lite");
 const request = require("request-promise-native");
 
 const SCRIPTS = [
-	"../../lib/frame-tree/frame-tree.js",
+	"../../lib/frame-tree/content/content-frame-tree.js",
 	"../../lib/single-file/util/doc-util.js",
 	"../../lib/single-file/util/doc-helper.js",
 	"../../lib/single-file/util/timeout.js",

+ 1 - 1
cli/back-ends/puppeteer.js

@@ -29,7 +29,7 @@ const puppeteer = require("puppeteer-core");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
-	"../../lib/frame-tree/frame-tree.js",
+	"../../lib/frame-tree/content/content-frame-tree.js",
 	"../../lib/lazy/content/content-lazy-loader.js",
 	"../../lib/single-file/util/doc-util.js",
 	"../../lib/single-file/util/doc-helper.js",

+ 1 - 1
cli/back-ends/webdriver-chromium.js

@@ -31,7 +31,7 @@ const { Builder } = require("selenium-webdriver");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
-	"../../lib/frame-tree/frame-tree.js",
+	"../../lib/frame-tree/content/content-frame-tree.js",
 	"../../lib/lazy/content/content-lazy-loader.js",
 	"../../lib/single-file/util/doc-util.js",
 	"../../lib/single-file/util/doc-helper.js",

+ 1 - 1
cli/back-ends/webdriver-gecko.js

@@ -31,7 +31,7 @@ const { Builder, By, Key } = require("selenium-webdriver");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
-	"../../lib/frame-tree/frame-tree.js",
+	"../../lib/frame-tree/content/content-frame-tree.js",
 	"../../lib/lazy/content/content-lazy-loader.js",
 	"../../lib/single-file/util/doc-util.js",
 	"../../lib/single-file/util/doc-helper.js",

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

@@ -47,7 +47,7 @@ singlefile.core = (() => {
 		"/lib/single-file/util/doc-helper.js",
 		"/lib/single-file/util/timeout.js",
 		"/lib/fetch/content/fetch.js",
-		"/lib/frame-tree/frame-tree.js",
+		"/lib/frame-tree/content/content-frame-tree.js",
 		"/extension/core/content/content-frame.js"
 	];
 

+ 33 - 0
lib/frame-tree/bg/bg-frame-tree.js

@@ -0,0 +1,33 @@
+/*
+ * Copyright 2010-2019 Gildas Lormeau
+ * contact : gildas.lormeau <at> gmail.com
+ * 
+ * This file is part of SingleFile.
+ *
+ *   SingleFile is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Lesser General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SingleFile is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Lesser General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Lesser General Public License
+ *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* global browser */
+
+this.frameTree = (() => {
+
+	"use strict";
+
+	browser.runtime.onMessage.addListener((message, sender) => {
+		if (message.method == "initResponse") {
+			browser.tabs.sendMessage(sender.tab.id, message, { frameId: 0 });
+		}
+	});
+
+})();

+ 17 - 6
lib/frame-tree/frame-tree.js → lib/frame-tree/content/content-frame-tree.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, top, document, addEventListener, docHelper, timeout, MessageChannel, superFetch, fetch, TextDecoder, DOMParser, lazyLoader, setTimeout */
+/* global window, top, document, addEventListener, docHelper, timeout, MessageChannel, superFetch, fetch, TextDecoder, DOMParser, lazyLoader, setTimeout, browser */
 
 this.frameTree = this.frameTree || (() => {
 
@@ -39,6 +39,13 @@ this.frameTree = this.frameTree || (() => {
 
 	if (TOP_WINDOW) {
 		windowId = TOP_WINDOW_ID;
+		if (this.browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
+			browser.runtime.onMessage.addListener(message => {
+				if (message.method == INIT_RESPONSE_MESSAGE) {
+					initResponse(message);
+				}
+			});
+		}
 	}
 	addEventListener("message", event => {
 		if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
@@ -232,12 +239,16 @@ this.frameTree = this.frameTree || (() => {
 	}
 
 	function sendMessage(targetWindow, message, useChannel) {
-		if (useChannel) {
-			const channel = new MessageChannel();
-			targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
-			channel.port1.postMessage(message);
+		if (targetWindow == top && this.browser && browser.runtime && browser.runtime.sendMessage) {
+			browser.runtime.sendMessage(message);
 		} else {
-			targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
+			if (useChannel) {
+				const channel = new MessageChannel();
+				targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
+				channel.port1.postMessage(message);
+			} else {
+				targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
+			}
 		}
 	}
 

+ 1 - 1
maff2html/back-ends/webdriver-gecko.js

@@ -31,7 +31,7 @@ const { Builder, By, Key } = require("selenium-webdriver");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
-	"../../lib/frame-tree/frame-tree.js",
+	"../../lib/frame-tree/content/content-frame-tree.js",
 	"../../lib/lazy/content/content-lazy-loader.js",
 	"../../lib/single-file/util/doc-util.js",
 	"../../lib/single-file/util/doc-helper.js",

+ 2 - 1
manifest.json

@@ -22,7 +22,7 @@
 				"lib/single-file/util/doc-helper.js",
 				"lib/single-file/util/doc-util.js",
 				"lib/single-file/util/timeout.js",
-				"lib/frame-tree/frame-tree.js",
+				"lib/frame-tree/content/content-frame-tree.js",
 				"extension/index.js",
 				"extension/core/content/content-frame.js"
 			],
@@ -55,6 +55,7 @@
 			"lib/browser-polyfill/chrome-browser-polyfill.js",
 			"lib/fetch/bg/fetch.js",
 			"lib/fetch/content/fetch.js",
+			"lib/frame-tree/bg/bg-frame-tree.js",
 			"extension/index.js",
 			"extension/core/bg/data/config.js",
 			"extension/core/bg/data/tabs-data.js",