Bläddra i källkod

use requestedFrame/requestedFrameId to find and retrieve selected frame content

Gildas 7 år sedan
förälder
incheckning
f0c5ebddb3

+ 4 - 6
extension/core/bg/core.js

@@ -45,8 +45,7 @@ singlefile.core = (() => {
 		"/extension/index.js",
 		"/lib/single-file/util/doc-helper.js",
 		"/lib/fetch/content/fetch.js",
-		"/lib/frame-tree/content/content-frame-tree.js",
-		"/extension/core/content/content-frame.js"
+		"/lib/frame-tree/content/content-frame-tree.js"
 	];
 
 	const modulesScriptFiles = [
@@ -86,7 +85,7 @@ singlefile.core = (() => {
 					let scriptsInjected;
 					if (!mergedOptions.removeFrames) {
 						try {
-							await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, runAt: "document_start" });
+							await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, matchAboutBlank: true, runAt: "document_start" });
 						} catch (error) {
 							// ignored
 						}
@@ -101,10 +100,9 @@ singlefile.core = (() => {
 					if (scriptsInjected) {
 						singlefile.ui.button.onInitialize(tabId, options, 2);
 						if (mergedOptions.frameId) {
-							await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
-						} else {
-							await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
+							await browser.tabs.executeScript(tab.id, { code: "document.documentElement.dataset.requestedFrameId = true", frameId: mergedOptions.frameId, matchAboutBlank: true, runAt: "document_start" });
 						}
+						await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
 					} else {
 						singlefile.ui.button.onForbiddenDomain(tabId, options);
 					}

+ 0 - 43
extension/core/content/content-frame.js

@@ -1,43 +0,0 @@
-/*
- * 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, window, top, document, docHelper */
-
-this.singlefile.frame = this.singlefile.frame || (() => {
-
-	if (window != top) {
-		browser.runtime.onMessage.addListener(message => {
-			if (message.saveFrame) {
-				message.options.content = docHelper.serialize(document);
-				delete message.saveFrame;
-				delete message.options.frameId;
-				message.savePage = true;
-				message.options.url = document.location.href;
-				if (this.browser && browser.runtime && browser.runtime.sendMessage) {
-					browser.runtime.sendMessage(message);
-				} else {
-					top.postMessage("__SingleFile__::" + JSON.stringify(message), "*");
-				}
-			}
-		});
-	}
-	return true;
-
-})();

+ 15 - 2
extension/core/content/content.js

@@ -46,7 +46,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 
 	async function savePage(message) {
 		const options = message.options;
-		if (!processing && !options.frameId) {
+		if (!processing) {
 			let selectionFound;
 			if (options.selected) {
 				selectionFound = await singlefile.ui.markSelection();
@@ -128,8 +128,21 @@ this.singlefile.top = this.singlefile.top || (() => {
 			}
 		};
 		[options.framesData] = await Promise.all(preInitializationPromises);
-		options.doc = document;
+		const selectedFrame = options.framesData.find(frameData => frameData.requestedFrame);
 		options.win = window;
+		if (selectedFrame) {
+			options.content = selectedFrame.content;
+			options.url = selectedFrame.baseURI;
+			options.canvasData = selectedFrame.canvasData;
+			options.fontsData = selectedFrame.fontsData;
+			options.stylesheetContents = selectedFrame.stylesheetContents;
+			options.imageData = selectedFrame.imageData;
+			options.postersData = selectedFrame.postersData;
+			options.usedFonts = selectedFrame.usedFonts;
+			options.shadowRootContents = selectedFrame.shadowRootContents;
+		} else {
+			options.doc = document;
+		}
 		await processor.run();
 		if (!options.saveRawPage && !options.removeFrames && this.frameTree) {
 			this.frameTree.cleanup(options);

+ 4 - 3
lib/frame-tree/content/content-frame-tree.js

@@ -152,13 +152,13 @@ this.frameTree = this.frameTree || (() => {
 		}
 	}
 	function processFrames(doc, frameElements, options, parentWindowId, sessionId) {
-		processFramesAsync(frameElements, options, parentWindowId, sessionId);
+		processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
 		if (frameElements.length) {
 			processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
 		}
 	}
 
-	function processFramesAsync(frameElements, options, parentWindowId, sessionId) {
+	function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
 		const framesData = [];
 		frameElements.forEach((frameElement, frameIndex) => {
 			const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
@@ -171,7 +171,8 @@ this.frameTree = this.frameTree || (() => {
 			}
 			setTimeout(() => sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
 		});
-		sendInitResponse({ framesData, sessionId });
+		sendInitResponse({ framesData, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
+		delete document.documentElement.dataset.requestedFrameId;
 	}
 
 	function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {

+ 1 - 2
manifest.json

@@ -22,8 +22,7 @@
 				"lib/single-file/util/doc-helper.js",
 				"lib/single-file/util/doc-util.js",
 				"lib/frame-tree/content/content-frame-tree.js",
-				"extension/index.js",
-				"extension/core/content/content-frame.js"
+				"extension/index.js"
 			],
 			"all_frames": true,
 			"match_about_blank": true