Kaynağa Gözat

use an iframe to download pages when "save pages in background" is disabled

Gildas 6 yıl önce
ebeveyn
işleme
21804113ab

+ 25 - 8
extension/core/content/content.js

@@ -21,11 +21,12 @@
  *   Source.
  */
 
-/* global browser, SingleFileBrowser, singlefile, frameTree, document, MouseEvent, window, lazyLoader, URL, setTimeout, docHelper, Blob */
+/* global browser, SingleFileBrowser, singlefile, frameTree, document, addEventListener, removeEventListener, window, lazyLoader, setTimeout, docHelper */
 
 this.singlefile.top = this.singlefile.top || (() => {
 
 	const MAX_CONTENT_SIZE = 64 * (1024 * 1024);
+	const DOWNLOADER_FRAME_ID = "single-file-downloader";
 	const SingleFile = SingleFileBrowser.getClass();
 
 	let processing = false;
@@ -62,6 +63,10 @@ this.singlefile.top = this.singlefile.top || (() => {
 
 	async function processPage(options) {
 		docHelper.initDoc(document);
+		const iframe = document.getElementById(DOWNLOADER_FRAME_ID);
+		if (iframe) {
+			iframe.remove();
+		}
 		if (options.shadowEnabled) {
 			singlefile.ui.onStartPage();
 		}
@@ -193,13 +198,25 @@ this.singlefile.top = this.singlefile.top || (() => {
 			page.filename = singlefile.ui.prompt("File name", page.filename);
 		}
 		if (page.filename && page.filename.length) {
-			const link = document.createElement("a");
-			document.body.appendChild(link);
-			link.download = page.filename;
-			link.href = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
-			link.dispatchEvent(new MouseEvent("click"));
-			link.remove();
-			URL.revokeObjectURL(page.url);
+			const iframe = document.createElement("iframe");
+			iframe.id = DOWNLOADER_FRAME_ID;
+			iframe.style.setProperty("display", "inline-block", "important");
+			iframe.style.setProperty("max-width", "0", "important");
+			iframe.style.setProperty("max-height", "0", "important");
+			iframe.style.setProperty("border-width", "0", "important");
+			iframe.style.setProperty("margin", "0", "important");
+			iframe.src = browser.runtime.getURL("/extension/ui/pages/downloader.html");
+			iframe.onload = () => {
+				addEventListener("message", listener, false);
+				iframe.contentWindow.postMessage(JSON.stringify([page.filename, page.content]), "*");
+			};
+			document.body.appendChild(iframe);
+		}
+
+		function listener(event) {
+			if (event.data == "content.saved") {
+				removeEventListener("message", listener, false);
+			}
 		}
 	}
 

+ 16 - 0
extension/ui/content/content-download.js

@@ -0,0 +1,16 @@
+/* global addEventListener, removeEventListener, document, URL, MouseEvent, Blob, top */
+
+addEventListener("message", listener, false);
+
+function listener(event) {
+	removeEventListener("message", listener, false);
+	const [filename, content] = JSON.parse(event.data);
+	const link = document.createElement("a");
+	document.body.appendChild(link);
+	link.download = filename;
+	const url = URL.createObjectURL(new Blob([content], { type: "text/html" }));
+	link.href = url;
+	link.dispatchEvent(new MouseEvent("click"));
+	URL.revokeObjectURL(url);
+	top.postMessage("content.saved", "*");
+}

+ 9 - 0
extension/ui/pages/downloader.html

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>SingleFile</title>
+</head>
+<body>
+    <script src="/extension/ui/content/content-download.js"></script>
+</body>
+</html>

+ 1 - 3
extension/ui/pages/help.html

@@ -313,9 +313,7 @@
 							background</span>
 						<p>Uncheck this option if you get invalid file names like
 							"37bec68b-446a-46a5-8642-19a89c231b46.html" or interrupted downloads when saving pages.
-							Unchecking this option prevent using sub-directories in filename templates. Unchecking this
-							option can also introduce a security issue where a potential malicious script could read the
-							content of the saved page.</p>
+							Unchecking this option prevent using sub-directories in filename templates.</p>
 						<p class="notice">It is recommended to <u>check</u> this option</p>
 					</li>
 					<li data-options-label="displayStatsLabel"> <span class="option">Option: display stats in the

+ 4 - 0
manifest.json

@@ -119,6 +119,10 @@
 			}
 		}
 	},
+	"web_accessible_resources": [
+		"extension/ui/content/content-download.js",
+		"extension/ui/pages/downloader.html"
+	],
 	"permissions": [
 		"menus",
 		"clipboardWrite",