Ver Fonte

added "auto-save delay" aoption and other entries in the context menu

Gildas há 7 anos atrás
pai
commit
11e9103aa2

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

@@ -42,7 +42,8 @@ singlefile.config = (() => {
 		removeVideoSrc: true,
 		displayInfobar: true,
 		displayStats: false,
-		backgroundSave: true
+		backgroundSave: true,
+		autoSaveDelay: 1
 	};
 
 	let pendingUpgradePromise;
@@ -114,10 +115,15 @@ singlefile.config = (() => {
 		if (config.backgroundSave === undefined) {
 			config.backgroundSave = true;
 		}
+		if (config.autoSaveDelay === undefined) {
+			config.autoSaveDelay = 1;
+		}
 		const platformInfo = await browser.runtime.getPlatformInfo();
 		if (platformInfo.os == "android") {
 			config.backgroundSave = false;
 			config.backgroundSaveDisabled = true;
+			config.autoSaveDelay = 0;
+			config.autoSaveDelayDisabled = true;
 		}
 	}
 

+ 16 - 9
extension/core/content/content.js

@@ -23,6 +23,8 @@
 this.singlefile.top = this.singlefile.top || (() => {
 
 	let processing = false;
+	let autoSaveTimeout;
+
 	browser.runtime.onMessage.addListener(async message => {
 		savePage(message);
 		return {};
@@ -38,18 +40,23 @@ this.singlefile.top = this.singlefile.top || (() => {
 	async function savePage(message) {
 		const options = message.options;
 		if (!processing) {
-			processing = true;
 			if (message.processStart && !options.frameId) {
-				try {
-					const page = await processPage(options);
-					await downloadPage(page, options);
-					revokeDownloadURL(page);
-				} catch (error) {
-					console.error(error); // eslint-disable-line no-console
-					browser.runtime.sendMessage({ processError: true, error });
+				if (!autoSaveTimeout && options.autoSave && options.autoSaveDelay) {
+					autoSaveTimeout = setTimeout(() => savePage(message), options.autoSaveDelay * 1000);
+				} else {
+					autoSaveTimeout = null;
+					processing = true;
+					try {
+						const page = await processPage(options);
+						await downloadPage(page, options);
+						revokeDownloadURL(page);
+					} catch (error) {
+						console.error(error); // eslint-disable-line no-console
+						browser.runtime.sendMessage({ processError: true, error });
+					}
+					processing = false;
 				}
 			}
-			processing = false;
 		}
 	}
 

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

@@ -43,6 +43,7 @@
 	const displayInfobarInput = document.getElementById("displayInfobarInput");
 	const displayStatsInput = document.getElementById("displayStatsInput");
 	const backgroundSaveInput = document.getElementById("backgroundSaveInput");
+	const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
 	let pendingSave = Promise.resolve();
 	document.getElementById("resetButton").addEventListener("click", async () => {
 		await bgPage.singlefile.config.reset();
@@ -77,6 +78,8 @@
 		displayStatsInput.checked = config.displayStats;
 		backgroundSaveInput.checked = config.backgroundSave;
 		backgroundSaveInput.disabled = config.backgroundSaveDisabled;
+		autoSaveDelayInput.value = config.autoSaveDelay;
+		autoSaveDelayInput.disabled = config.autoSaveDelayDisabled;
 	}
 
 	async function update() {
@@ -101,7 +104,8 @@
 			removeVideoSrc: removeVideoSrcInput.checked,
 			displayInfobar: displayInfobarInput.checked,
 			displayStats: displayStatsInput.checked,
-			backgroundSave: backgroundSaveInput.checked
+			backgroundSave: backgroundSaveInput.checked,
+			autoSaveDelay: autoSaveDelayInput.value
 		});
 		await pendingSave;
 		await bgPage.singlefile.ui.update();

+ 64 - 19
extension/ui/bg/ui.js

@@ -31,9 +31,13 @@ singlefile.ui = (() => {
 	const MENU_ID_SAVE_PAGE = "save-page";
 	const MENU_ID_SAVE_SELECTED = "save-selected";
 	const MENU_ID_SAVE_FRAME = "save-frame";
-	const MENU_ID_SAVE_TABS = "save-tabs";
 	const MENU_ID_SAVE_SELECTED_TABS = "save-selected-tabs";
-	const MENU_ID_AUTO_SAVE = "auto-save";
+	const MENU_ID_SAVE_UNPINNED_TABS = "save-unpinned-tabs";
+	const MENU_ID_SAVE_ALL_TABS = "save-tabs";
+	const MENU_ID_AUTO_SAVE_DISABLED = "auto-save-disabled";
+	const MENU_ID_AUTO_SAVE_TAB = "auto-save-tab";
+	const MENU_ID_AUTO_SAVE_UNPINNED = "auto-save-unpinned";
+	const MENU_ID_AUTO_SAVE_ALL = "auto-save-all";
 
 	const tabs = {};
 
@@ -49,20 +53,30 @@ singlefile.ui = (() => {
 			if (event.menuItemId == MENU_ID_SAVE_FRAME) {
 				processTab(tab, { frameId: event.frameId });
 			}
-			if (event.menuItemId == MENU_ID_SAVE_TABS) {
-				const tabs = await browser.tabs.query({ currentWindow: true });
-				tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
-			}
 			if (event.menuItemId == MENU_ID_SAVE_SELECTED_TABS) {
 				const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
 				tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
 			}
-			if (event.menuItemId == MENU_ID_AUTO_SAVE) {
+			if (event.menuItemId == MENU_ID_SAVE_UNPINNED_TABS) {
+				const tabs = await browser.tabs.query({ currentWindow: true, pinned: false });
+				tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
+			}
+			if (event.menuItemId == MENU_ID_SAVE_ALL_TABS) {
+				const tabs = await browser.tabs.query({ currentWindow: true });
+				tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
+			}
+			if (event.menuItemId == MENU_ID_AUTO_SAVE_TAB) {
 				if (!tabs[tab.id]) {
 					tabs[tab.id] = {};
 				}
 				tabs[tab.id].autoSave = event.checked;
 			}
+			if (event.menuItemId == MENU_ID_AUTO_SAVE_ALL) {
+				tabs.autoSaveAll = event.checked;
+			}
+			if (event.menuItemId == MENU_ID_AUTO_SAVE_UNPINNED) {
+				tabs.autoSaveUnpinned = event.checked;
+			}
 		});
 	}
 	browser.browserAction.onClicked.addListener(async tab => {
@@ -81,7 +95,7 @@ singlefile.ui = (() => {
 	});
 	browser.tabs.onCreated.addListener(tab => onTabActivated(tab));
 	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
-		if (tabs[tab.id] && tabs[tab.id].autoSave) {
+		if (tabs.autoSaveAll || tabs[tab.id] && tabs[tab.id].autoSave || (tabs.autoSaveUnpinned && !tab.pinned)) {
 			if (changeInfo.status == "complete") {
 				processTab(tab, { autoSave: true });
 			}
@@ -116,14 +130,9 @@ singlefile.ui = (() => {
 					title: DEFAULT_TITLE
 				});
 				browser.menus.create({
-					id: MENU_ID_SAVE_TABS,
-					contexts: ["page"],
-					title: "Save all tabs"
-				});
-				browser.menus.create({
-					id: MENU_ID_SAVE_SELECTED_TABS,
-					contexts: ["page"],
-					title: "Save selected tabs"
+					id: "separator",
+					contexts: ["all"],
+					type: "separator"
 				});
 				browser.menus.create({
 					id: MENU_ID_SAVE_SELECTED,
@@ -135,18 +144,54 @@ singlefile.ui = (() => {
 					contexts: ["frame"],
 					title: "Save frame"
 				});
+				browser.menus.create({
+					id: MENU_ID_SAVE_SELECTED_TABS,
+					contexts: ["page"],
+					title: "Save selected tabs"
+				});
+				browser.menus.create({
+					id: MENU_ID_SAVE_UNPINNED_TABS,
+					contexts: ["page"],
+					title: "Save unpinned tabs"
+				});
+				browser.menus.create({
+					id: MENU_ID_SAVE_ALL_TABS,
+					contexts: ["page"],
+					title: "Save all tabs"
+				});
 				browser.menus.create({
 					id: "separator",
 					contexts: ["all"],
 					type: "separator"
 				});
 				browser.menus.create({
-					id: MENU_ID_AUTO_SAVE,
-					type: "checkbox",
+					id: MENU_ID_AUTO_SAVE_DISABLED,
+					type: "radio",
+					title: "Disable Auto-save",
+					contexts: ["all"],
+					checked: true
+				});
+				browser.menus.create({
+					id: MENU_ID_AUTO_SAVE_TAB,
+					type: "radio",
 					title: "Auto-save this tab",
 					contexts: ["all"],
 					checked: false
 				});
+				browser.menus.create({
+					id: MENU_ID_AUTO_SAVE_UNPINNED,
+					type: "radio",
+					title: "Auto-save unpinned tabs",
+					contexts: ["all"],
+					checked: false
+				});
+				browser.menus.create({
+					id: MENU_ID_AUTO_SAVE_ALL,
+					type: "radio",
+					title: "Auto-save all tabs",
+					contexts: ["all"],
+					checked: false
+				});
 			} else {
 				await browser.menus.removeAll();
 			}
@@ -206,7 +251,7 @@ singlefile.ui = (() => {
 	function onTabEnd(tabId) {
 		refreshBadge(tabId, {
 			text: "OK",
-			color: tabs[tabId].autoSave ? [255, 141, 1, 255] : [4, 229, 36, 255],
+			color: tabs.autoSaveAll || tabs.autoSaveUnpinned || tabs[tabId].autoSave ? [255, 141, 1, 255] : [4, 229, 36, 255],
 			title: DEFAULT_TITLE,
 			path: DEFAULT_ICON_PATH,
 			progress: -1,

+ 16 - 9
extension/ui/pages/help.html

@@ -37,7 +37,8 @@
 					<li>Right-click on the SingleFile button and select "Options" to open the options page.</li>
 					<li>To save multiple tabs, select them first and click on the SingleFile button <img src="../resources/icon_16.png"
 						 class="icon">.</li>
-					<li>Use the context menu to save a tab, all tabs, selected tabs, a selected content, or a frame content</li>
+					<li>Use the context menu to save a tab, all tabs, selected tabs, a selected content, or a frame content. The context
+						menu also allows activating automatic page saving.</li>
 				</ul>
 			</li>
 			<li>
@@ -99,14 +100,6 @@
 							<u>uncheck</u> this option</p>
 					</li>
 
-					<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>
 						<p>Check this option to remove all HTML comments, and unneeded spaces or returns. This helps to reduce the size of
@@ -196,6 +189,20 @@
 						</p>
 					</li>
 
+					<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">auto-save wait delay (sec.)</span>
+						<p>Specify the time to wait in seconds before saving a page when auto-save is active.
+						</p>
+					</li>
+
 					<li>
 						<span class="option">Reset to default options</span>
 						<p>Reset all the options to their default value.</p>

+ 11 - 4
extension/ui/pages/options.html

@@ -39,10 +39,6 @@
 				<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>
@@ -102,6 +98,17 @@
 				<input type="number" id="maxResourceSizeInput" min="1">
 			</div>
 		</details>
+		<details>
+			<summary>Misc.</summary>
+			<div class="option">
+				<label for="backgroundSaveInput">save pages in background</label>
+				<input type="checkbox" id="backgroundSaveInput">
+			</div>
+			<div class="option">
+				<label for="autoSaveDelayInput">auto-save wait delay (sec.)</label>
+				<input type="number" id="autoSaveDelayInput" min="0">
+			</div>
+		</details>
 		<div class="option bottom">
 			<a href="help.html" target="SingleFileHelpPage">help</a>
 			<button id="resetButton" title="Reset all the options to default values">Reset</button>