Prechádzať zdrojové kódy

added option "link the new bookmark to the saved page" (fix #354)

Former-commit-id: 4023c37de94b58b5aa9bae37950119413bee16a2
Gildas 6 rokov pred
rodič
commit
6b95671341

+ 4 - 0
_locales/de/messages.json

@@ -403,6 +403,10 @@
 		"message": "Die Seite eines neu angelegten Bookmark speichern",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "Das neue Bookmark mit der gespeicherten Seite verlinken",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "Hilfe",
 		"description": "Options help link"

+ 4 - 0
_locales/en/messages.json

@@ -403,6 +403,10 @@
 		"message": "save the page of a newly created bookmark",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "help",
 		"description": "Options help link"

+ 4 - 0
_locales/es/messages.json

@@ -403,6 +403,10 @@
 		"message": "guardar la página de un marcador recién creado",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "enlazar el nuevo marcador a la página guardada",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "ayuda",
 		"description": "Options help link"

+ 4 - 0
_locales/fr/messages.json

@@ -403,6 +403,10 @@
 		"message": "sauvegarder la page d'un signet nouvellement créé",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "lier le nouveau signet à la page sauvegardée",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "aide (anglais)",
 		"description": "Options help link"

+ 4 - 0
_locales/ja/messages.json

@@ -403,6 +403,10 @@
 		"message": "新しく作成したブックマークのページを保存する",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "ヘルプ",
 		"description": "Options help link"

+ 4 - 0
_locales/pl/messages.json

@@ -403,6 +403,10 @@
 		"message": "zapisuj stronę nowo utworzonej zakładki",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "pomoc (w języku angielskim)",
 		"description": "Options help link"

+ 4 - 0
_locales/ru/messages.json

@@ -403,6 +403,10 @@
 		"message": "сохранить страницу недавно созданной закладки",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "помощь",
 		"description": "Options help link"

+ 4 - 0
_locales/uk/messages.json

@@ -403,6 +403,10 @@
 		"message": "save the page of a newly created bookmark",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "допомога",
 		"description": "Options help link"

+ 4 - 0
_locales/zh_CN/messages.json

@@ -403,6 +403,10 @@
 		"message": "保存到书签",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "帮助",
 		"description": "选项页帮助链接"

+ 4 - 0
_locales/zh_TW/messages.json

@@ -403,6 +403,10 @@
 		"message": "保存到書籤",
 		"description": "Options page label: 'save the page of a newly created bookmark'"
 	},
+	"optionReplaceBookmarkURL": {
+		"message": "link the new bookmark to the saved page",
+		"description": "Options page label: 'link the new bookmark to the saved page'"
+	},
 	"optionsHelpLink": {
 		"message": "幫助",
 		"description": "選項頁幫助鏈接"

+ 21 - 3
extension/core/bg/bookmarks.js

@@ -29,7 +29,8 @@ singlefile.extension.core.bg.bookmarks = (() => {
 	return {
 		onMessage,
 		saveCreatedBookmarks: enable,
-		disable
+		disable,
+		update: (id, changes) => browser.bookmarks.update(id, changes)
 	};
 
 	async function onInit() {
@@ -79,17 +80,34 @@ singlefile.extension.core.bg.bookmarks = (() => {
 		const options = await singlefile.extension.core.bg.config.getOptions(bookmarkInfo.url);
 		if (options.saveCreatedBookmarks) {
 			if (tabs.length && tabs[0].url == bookmarkInfo.url) {
-				singlefile.extension.core.bg.business.saveTabs(tabs);
+				singlefile.extension.core.bg.business.saveTabs(tabs, { bookmarkId: bookmarkInfo.id });
 			} else {
 				const tabs = await singlefile.extension.core.bg.tabs.get({});
 				if (tabs.length) {
 					const tab = tabs.find(tab => tab.url == bookmarkInfo.url);
 					if (tab) {
-						singlefile.extension.core.bg.business.saveTabs([tab]);
+						singlefile.extension.core.bg.business.saveTabs([tab], { bookmarkId: bookmarkInfo.id });
+					} else {
+						if (bookmarkInfo.url == "about:blank") {
+							browser.bookmarks.onChanged.addListener(onChanged);
+						} else {
+							saveUrl(bookmarkInfo.url);
+						}
 					}
 				}
 			}
 		}
+
+		function onChanged(id, changeInfo) {
+			if (id == bookmarkInfo.id && changeInfo.url) {
+				browser.bookmarks.onChanged.removeListener(onChanged);
+				saveUrl(changeInfo.url);
+			}
+		}
+
+		function saveUrl(url) {
+			singlefile.extension.core.bg.business.saveUrls([url], { bookmarkId: bookmarkInfo.id });
+		}
 	}
 
 })();

+ 3 - 0
extension/core/bg/business.js

@@ -171,6 +171,9 @@ singlefile.extension.core.bg.business = (() => {
 				taskInfo.options.taskId = taskId;
 				tabs.sendMessage(taskInfo.tab.id, { method: taskInfo.method, options: taskInfo.options })
 					.then(() => {
+						if (taskInfo.options.replaceBookmarkURL && taskInfo.options.bookmarkId && taskInfo.options.filename) {
+							singlefile.extension.core.bg.bookmarks.update(taskInfo.options.bookmarkId, { url: taskInfo.options.filename });
+						}
 						if (taskInfo.options.autoClose && !taskInfo.cancelled) {
 							tabs.remove(taskInfo.tab.id);
 						}

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

@@ -87,6 +87,7 @@ singlefile.extension.core.bg.config = (() => {
 		openEditor: false,
 		autoOpenEditor: false,
 		saveCreatedBookmarks: false,
+		replaceBookmarkURL: true,
 		saveFavicon: true
 	};
 

+ 8 - 2
extension/core/bg/downloads.js

@@ -222,7 +222,11 @@ singlefile.extension.core.bg.downloads = (() => {
 		if (options.incognito) {
 			downloadInfo.incognito = true;
 		}
-		await download(downloadInfo, options.filenameReplacementCharacter);
+		const downloadData = await download(downloadInfo, options.filenameReplacementCharacter);
+		if (downloadData.filename) {
+			const taskInfo = singlefile.extension.core.bg.business.getTaskInfo(pageData.taskId);
+			taskInfo.options.filename = "file:///" + encodeURI(downloadData.filename.replace(/\\/gi, "/"));
+		}
 	}
 
 	async function download(downloadInfo, replacementCharacter) {
@@ -263,7 +267,9 @@ singlefile.extension.core.bg.downloads = (() => {
 			function onChanged(event) {
 				if (event.id == downloadId && event.state) {
 					if (event.state.current == STATE_DOWNLOAD_COMPLETE) {
-						resolve({});
+						browser.downloads.search({ id: downloadId })
+							.then(downloadItems => resolve({ filename: downloadItems[0] && downloadItems[0].filename }))
+							.catch(() => resolve({}));
 						browser.downloads.onChanged.removeListener(onChanged);
 					}
 					if (event.state.current == STATE_DOWNLOAD_INTERRUPTED) {

+ 24 - 2
extension/lib/single-file/browser-polyfill/chrome-browser-polyfill.js

@@ -129,7 +129,20 @@
 				onCreated: {
 					addListener: listener => nativeAPI.bookmarks.onCreated.addListener(listener),
 					removeListener: listener => nativeAPI.bookmarks.onCreated.removeListener(listener)
-				}
+				},
+				onChanged: {
+					addListener: listener => nativeAPI.bookmarks.onChanged.addListener(listener),
+					removeListener: listener => nativeAPI.bookmarks.onChanged.removeListener(listener)
+				},
+				update: (id, changes) => new Promise((resolve, reject) => {
+					nativeAPI.bookmarks.update(id, changes, node => {
+						if (nativeAPI.runtime.lastError) {
+							reject(nativeAPI.runtime.lastError.message || nativeAPI.runtime.lastError);
+						} else {
+							resolve(node);
+						}
+					});
+				})
 			},
 			commands: {
 				onCommand: {
@@ -149,7 +162,16 @@
 				onChanged: {
 					addListener: listener => nativeAPI.downloads.onChanged.addListener(listener),
 					removeListener: listener => nativeAPI.downloads.onChanged.removeListener(listener)
-				}
+				},
+				search: query => new Promise((resolve, reject) => {
+					nativeAPI.downloads.search(query, downloadItems => {
+						if (nativeAPI.runtime.lastError) {
+							reject(nativeAPI.runtime.lastError.message || nativeAPI.runtime.lastError);
+						} else {
+							resolve(downloadItems);
+						}
+					});
+				})
 			},
 			i18n: {
 				getMessage: (messageName, substitutions) => nativeAPI.i18n.getMessage(messageName, substitutions)

+ 8 - 0
extension/ui/bg/ui-options.js

@@ -67,6 +67,7 @@
 	const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
 	const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
 	const saveCreatedBookmarksLabel = document.getElementById("saveCreatedBookmarksLabel");
+	const replaceBookmarkURLLabel = document.getElementById("replaceBookmarkURLLabel");
 	const titleLabel = document.getElementById("titleLabel");
 	const userInterfaceLabel = document.getElementById("userInterfaceLabel");
 	const filenameLabel = document.getElementById("filenameLabel");
@@ -136,6 +137,7 @@
 	const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
 	const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
 	const saveCreatedBookmarksInput = document.getElementById("saveCreatedBookmarksInput");
+	const replaceBookmarkURLInput = document.getElementById("replaceBookmarkURLInput");
 	const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
 	const infobarTemplateInput = document.getElementById("infobarTemplateInput");
 	const includeInfobarInput = document.getElementById("includeInfobarInput");
@@ -455,6 +457,7 @@
 	removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
 	removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
 	saveCreatedBookmarksLabel.textContent = browser.i18n.getMessage("optionSaveCreatedBookmarks");
+	replaceBookmarkURLLabel.textContent = browser.i18n.getMessage("optionReplaceBookmarkURL");
 	groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
 	titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
 	userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
@@ -643,6 +646,8 @@
 		groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
 		removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
 		saveCreatedBookmarksInput.checked = profileOptions.saveCreatedBookmarks;
+		replaceBookmarkURLInput.checked = profileOptions.saveCreatedBookmarks && profileOptions.backgroundSave && profileOptions.replaceBookmarkURL;
+		replaceBookmarkURLInput.disabled = !profileOptions.saveCreatedBookmarks || !profileOptions.backgroundSave || profileOptions.saveToClipboard || profileOptions.saveToGDrive;
 		infobarTemplateInput.value = profileOptions.infobarTemplate;
 		includeInfobarInput.checked = profileOptions.includeInfobar;
 		confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
@@ -703,6 +708,7 @@
 				removeAlternativeImages: removeAlternativeImagesInput.checked,
 				removeAlternativeMedias: removeAlternativeMediasInput.checked,
 				saveCreatedBookmarks: saveCreatedBookmarksInput.checked,
+				replaceBookmarkURL: replaceBookmarkURLInput.checked,
 				groupDuplicateImages: groupDuplicateImagesInput.checked,
 				infobarTemplate: infobarTemplateInput.value,
 				includeInfobar: includeInfobarInput.checked,
@@ -736,6 +742,7 @@
 				if (permissionGranted) {
 					saveCreatedBookmarksInput.checked = true;
 					await update();
+					await refresh();
 					await browser.runtime.sendMessage({ method: "bookmarks.saveCreatedBookmarks" });
 				} else {
 					await disableOption();
@@ -749,6 +756,7 @@
 
 		async function disableOption() {
 			await update();
+			await refresh();
 			await browser.runtime.sendMessage({ method: "bookmarks.disable" });
 		}
 	}

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

@@ -415,9 +415,17 @@
 					<li data-options-label="saveCreatedBookmarksLabel"> <span class="option">Option: save the page of a
 							newly created bookmark</span>
 						<p>
-							Check this option to save pages that you add into your bookmarks. Note that a page will be
-							saved if and only if the page is already displayed in a tab when adding it into the
-							bookmarks.
+							Check this option to save pages that you add into your bookmarks. Note that if the page is
+							to be saved is not already displayed in a tab, SingleFile will open temporarily
+							a new tab to save the page.
+						</p>
+						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
+					</li>
+					<li data-options-label="replaceBookmarkURLLabel"> <span class="option">Option: link the new bookmark
+							to the saved page</span>
+						<p>
+							Check this option to replace the URL of the page added into your bookmark with the file URI
+							of the saved page on your disk.
 						</p>
 						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
 					</li>

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

@@ -102,7 +102,7 @@
 			</div>
 		</details>
 		<details>
-			<summary id="stylesheetsLabel"></summary>			
+			<summary id="stylesheetsLabel"></summary>
 			<div class="option">
 				<label for="removeUnusedStylesInput" id="removeUnusedStylesLabel"></label>
 				<input type="checkbox" id="removeUnusedStylesInput">
@@ -226,6 +226,10 @@
 				<label for="saveCreatedBookmarksInput" id="saveCreatedBookmarksLabel"></label>
 				<input type="checkbox" id="saveCreatedBookmarksInput">
 			</div>
+			<div class="option second-level">
+				<label for="replaceBookmarkURLInput" id="replaceBookmarkURLLabel"></label>
+				<input type="checkbox" id="replaceBookmarkURLInput">
+			</div>
 		</details>
 		<details>
 			<summary id="autoSettingsLabel"></summary>