Procházet zdrojové kódy

added "save pages in background" option

Gildas před 7 roky
rodič
revize
7c1173ee8f

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

@@ -41,7 +41,8 @@ singlefile.config = (() => {
 		removeAudioSrc: true,
 		removeVideoSrc: true,
 		displayInfobar: true,
-		displayStats: false
+		displayStats: false,
+		backgroundSave: true
 	};
 
 	let pendingUpgradePromise;
@@ -110,6 +111,9 @@ singlefile.config = (() => {
 		if (config.displayInfobar === undefined) {
 			config.displayInfobar = true;
 		}
+		if (config.backgroundSave === undefined) {
+			config.backgroundSave = true;
+		}
 	}
 
 	return {

+ 21 - 13
extension/core/content/content.js

@@ -134,22 +134,30 @@ this.singlefile.top = this.singlefile.top || (() => {
 	}
 
 	async function downloadPage(page, options) {
-		const response = await browser.runtime.sendMessage({ download: true, url: page.url, saveAs: options.confirmFilename, filename: page.filename });
-		if (response.notSupported) {
-			const response = await browser.runtime.sendMessage({ download: true, content: page.content, saveAs: options.confirmFilename, filename: page.filename });
+		if (options.backgroundSave) {
+			const response = await browser.runtime.sendMessage({ download: true, url: page.url, saveAs: options.confirmFilename, filename: page.filename });
 			if (response.notSupported) {
-				if (options.confirmFilename) {
-					page.filename = 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 = page.url;
-					link.dispatchEvent(new MouseEvent("click"));
-					link.remove();
+				const response = await browser.runtime.sendMessage({ download: true, content: page.content, saveAs: options.confirmFilename, filename: page.filename });
+				if (response.notSupported) {
+					downloadPageFallback(page, options);
 				}
 			}
+		} else {
+			downloadPageFallback(page, options);
+		}
+	}
+
+	function downloadPageFallback(page, options) {
+		if (options.confirmFilename) {
+			page.filename = 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 = page.url;
+			link.dispatchEvent(new MouseEvent("click"));
+			link.remove();
 		}
 	}
 

+ 4 - 1
extension/ui/bg/options.js

@@ -42,6 +42,7 @@
 	const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
 	const displayInfobarInput = document.getElementById("displayInfobarInput");
 	const displayStatsInput = document.getElementById("displayStatsInput");
+	const backgroundSaveInput = document.getElementById("backgroundSaveInput");
 	let pendingSave = Promise.resolve();
 	document.getElementById("resetButton").addEventListener("click", async () => {
 		await bgPage.singlefile.config.reset();
@@ -74,6 +75,7 @@
 		removeVideoSrcInput.checked = config.removeVideoSrc;
 		displayInfobarInput.checked = config.displayInfobar;
 		displayStatsInput.checked = config.displayStats;
+		backgroundSaveInput.checked = config.backgroundSave;
 	}
 
 	async function update() {
@@ -97,7 +99,8 @@
 			removeAudioSrc: removeAudioSrcInput.checked,
 			removeVideoSrc: removeVideoSrcInput.checked,
 			displayInfobar: displayInfobarInput.checked,
-			displayStats: displayStatsInput.checked
+			displayStats: displayStatsInput.checked,
+			backgroundSave: backgroundSaveInput.checked
 		});
 		await pendingSave;
 		await bgPage.singlefile.ui.update();

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

@@ -91,7 +91,14 @@
 						<p class="notice">It is recommended to
 							<u>uncheck</u> this option</p>
 					</li>
-					<label for="confirmFilenameInput"></label>
+
+					<li>
+						<span class="option">save pages in background</span>
+						<p>Uncheck this option if you get invalid file names like "37bec68b-446a-46a5-8642-19a89c231b46.html" when saving pages.
+						</p>
+						<p class="notice">It is recommended to
+							<u>check</u> this option</p>
+					</li>
 
 					<li>
 						<span class="option">compress HTML</span>

+ 5 - 1
extension/ui/pages/options.html

@@ -36,9 +36,13 @@
 				<input type="checkbox" id="appendSaveDateInput">
 			</div>
 			<div class="option">
-				<label for="confirmFilenameInput">enter file name before saving the page</label>
+				<label for="confirmFilenameInput">confirm file name before saving the page</label>
 				<input type="checkbox" id="confirmFilenameInput">
 			</div>
+			<div class="option">
+				<label for="backgroundSaveInput">save pages in background</label>
+				<input type="checkbox" id="backgroundSaveInput">
+			</div>
 		</details>
 		<details>
 			<summary>Page content</summary>