Przeglądaj źródła

added option "open pages saved with SingleFile in the annotation editor"

Former-commit-id: bfb6eb8715cee0fa7d72d6b99beb6f4fcdd98864
Gildas 6 lat temu
rodzic
commit
a4523cad95

+ 4 - 0
_locales/de/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "Automatisches Schließen des Tabs nach dem Speichern der Seite",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/en/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "auto-close the tab after the page is saved",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/es/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "cerrar automáticamente la pestaña después de guardar la página",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/fr/messages.json

@@ -151,6 +151,10 @@
 		"message": "annoter la page avant de la sauvegarder",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "ouvrir les pages sauvées avec SingleFile dans l'éditeur d'annotations",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "fermer automatiquement l'onglet après l'enregistrement de la page",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/ja/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "ページが保存された後にタブを自動的に閉じる",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/pl/messages.json

@@ -151,6 +151,10 @@
 		"message": "adnotuj stronę przed zapisaniem",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "automatycznie zamykaj kartę po zapisaniu strony",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/ru/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "auto-close the tab after the page is saved",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/uk/messages.json

@@ -151,6 +151,10 @@
 		"message": "анотувати сторінки перед збереженням",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "автоматично закривати вкладку після збереження сторінки",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/zh_CN/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "auto-close the tab after the page is saved",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

+ 4 - 0
_locales/zh_TW/messages.json

@@ -151,6 +151,10 @@
 		"message": "annotate the page before saving",
 		"description": "Options page label: 'annotate the page before saving'"
 	},
+	"optionAutoOpenEditor": {
+		"message": "open pages saved with SingleFile in the annotation editor",
+		"description": "Options page label: 'open pages saved with SingleFile in the annotation editor'"
+	},
 	"optionAutoCloseLabel": {
 		"message": "auto-close the tab after the page is saved",
 		"description": "Options page label: 'auto-close the tab after the page is saved'"

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

@@ -83,7 +83,8 @@ singlefile.extension.core.bg.config = (() => {
 		extractAuthCode: true,
 		resolveFragmentIdentifierURLs: false,
 		userScriptEnabled: false,
-		openEditor: false
+		openEditor: false,
+		autoOpenEditor: false
 	};
 
 	let pendingUpgradePromise = upgrade();

+ 23 - 0
extension/core/bg/editor.js

@@ -26,6 +26,7 @@
 singlefile.extension.core.bg.editor = (() => {
 
 	const tabsData = new Map();
+	const partialContents = new Map();
 	const EDITOR_URL = browser.runtime.getURL("/extension/ui/editor/editor.html");
 
 	return {
@@ -53,6 +54,28 @@ singlefile.extension.core.bg.editor = (() => {
 			const tab = sender.tab;
 			return tabsData.get(tab.id);
 		}
+		if (message.method.endsWith(".open")) {
+			let contents;
+			const tab = sender.tab;
+			if (message.truncated) {
+				contents = partialContents.get(tab.id);
+				if (!contents) {
+					contents = [];
+					partialContents.set(tab.id, contents);
+				}
+				contents.push(message.content);
+				if (message.finished) {
+					partialContents.delete(tab.id);
+				}
+			} else if (message.content) {
+				contents = [message.content];
+			}
+			if (!message.truncated || message.finished) {
+				const options = await singlefile.extension.core.bg.config.getOptions(tab && tab.url);
+				await singlefile.extension.core.bg.tabs.remove(tab.id);
+				await singlefile.extension.core.bg.editor.open({ filename: message.filename, content: contents.join("") }, options);
+			}
+		}
 	}
 
 })();

+ 43 - 2
extension/core/content/content-bootstrap.js

@@ -21,12 +21,13 @@
  *   Source.
  */
 
-/* global browser, window, addEventListener, removeEventListener, document, location, setTimeout, prompt */
+/* global browser, window, addEventListener, removeEventListener, document, location, setTimeout, prompt, Node */
 
 this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.core.content.bootstrap || (async () => {
 
 	const singlefile = this.singlefile;
 
+	const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
 	const PUSH_STATE_NOTIFICATION_EVENT_NAME = "single-file-push-state";
 
 	let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved;
@@ -34,7 +35,15 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 	browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
 		options = message.options;
 		autoSaveEnabled = message.autoSaveEnabled;
-		refresh();
+		if (document.documentElement.firstChild.nodeType == Node.COMMENT_NODE && document.documentElement.firstChild.textContent.includes("Page saved with SingleFile") && options.autoOpenEditor) {
+			if (document.readyState == "loading") {
+				document.addEventListener("DOMContentLoaded", () => openEditor(document));
+			} else {
+				openEditor(document);
+			}
+		} else {
+			refresh();
+		}
 	});
 	browser.runtime.onMessage.addListener(message => onMessage(message));
 	browser.runtime.sendMessage({ method: "ui.processInit" });
@@ -148,4 +157,36 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 		});
 	}
 
+	async function openEditor(document) {
+		serializeShadowRoots(document);
+		const content = singlefile.lib.modules.serializer.process(document);
+		for (let blockIndex = 0; blockIndex * MAX_CONTENT_SIZE < content.length; blockIndex++) {
+			const message = {
+				method: "editor.open",
+				filename: decodeURIComponent(location.href.match(/^.*\/(.*)$/)[1])
+			};
+			message.truncated = content.length > MAX_CONTENT_SIZE;
+			if (message.truncated) {
+				message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > content.length;
+				message.content = content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
+			} else {
+				message.content = content;
+			}
+			await browser.runtime.sendMessage(message);
+		}
+	}
+
+	function serializeShadowRoots(node) {
+		const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
+		node.querySelectorAll("*").forEach(element => {
+			if (element.shadowRoot) {
+				serializeShadowRoots(element.shadowRoot);
+				const templateElement = document.createElement("template");
+				templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
+				templateElement.appendChild(element.shadowRoot);
+				element.appendChild(templateElement);
+			}
+		});
+	}
+
 })();

+ 7 - 2
extension/ui/bg/ui-options.js

@@ -84,6 +84,7 @@
 	const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
 	const autoCloseLabel = document.getElementById("autoCloseLabel");
 	const openEditorLabel = document.getElementById("openEditorLabel");
+	const autoOpenEditorLabel = document.getElementById("autoOpenEditorLabel");
 	const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
 	const includeInfobarLabel = document.getElementById("includeInfobarLabel");
 	const miscLabel = document.getElementById("miscLabel");
@@ -136,6 +137,7 @@
 	const confirmInfobarInput = document.getElementById("confirmInfobarInput");
 	const autoCloseInput = document.getElementById("autoCloseInput");
 	const openEditorInput = document.getElementById("openEditorInput");
+	const autoOpenEditorInput = document.getElementById("autoOpenEditorInput");
 	const expandAllButton = document.getElementById("expandAllButton");
 	const rulesDeleteAllButton = document.getElementById("rulesDeleteAllButton");
 	const ruleUrlInput = document.getElementById("ruleUrlInput");
@@ -165,7 +167,7 @@
 		sidePanelDisplay = true;
 		document.querySelector(".options-title").remove();
 		document.documentElement.classList.add("side-panel");
-	}	
+	}
 	browser.runtime.onMessage.addListener(message => {
 		if (message.method == "options.refresh" || (message.method == "options.refreshPanel" && sidePanelDisplay)) {
 			refresh(message.profileName);
@@ -436,6 +438,7 @@
 	confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
 	autoCloseLabel.textContent = browser.i18n.getMessage("optionAutoCloseLabel");
 	openEditorLabel.textContent = browser.i18n.getMessage("optionOpenEditorLabel");
+	autoOpenEditorLabel.textContent = browser.i18n.getMessage("optionAutoOpenEditor");
 	resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
 	exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
 	importButton.textContent = browser.i18n.getMessage("optionsImportButton");
@@ -607,6 +610,7 @@
 		confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
 		autoCloseInput.checked = profileOptions.autoClose;
 		openEditorInput.checked = profileOptions.openEditor;
+		autoOpenEditorInput.checked = profileOptions.autoOpenEditor;
 		removeFramesInput.disabled = saveRawPageInput.checked;
 		removeFramesInput.checked = removeFramesInput.checked || saveRawPageInput.checked;
 		loadDeferredImagesInput.disabled = saveRawPageInput.checked;
@@ -664,7 +668,8 @@
 				includeInfobar: includeInfobarInput.checked,
 				confirmInfobarContent: confirmInfobarInput.checked,
 				autoClose: autoCloseInput.checked,
-				openEditor: openEditorInput.checked
+				openEditor: openEditorInput.checked,
+				autoOpenEditor: autoOpenEditorInput.checked
 			}
 		});
 		await pendingSave;

+ 6 - 0
extension/ui/pages/help.html

@@ -131,6 +131,12 @@
 							removes the entry "Annotate and save the page..." from the context menu.</p>
 						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
 					</li>
+					<li data-options-label="autoOpenEditorLabel"> <span class="option">Option: open the annotation
+							editor when viewing a page saved with SingleFile </span>
+						<p>Check this option to open the annotation editor when displaying a page saved by SingleFile.
+						</p>
+						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
+					</li>
 					<li data-options-label="confirmInfobarLabel"> <span class="option">Option: open a prompt dialog to
 							edit the infobar content</span>
 						<p>Check this option to display a prompt dialog in order to confirm the infobar content before

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

@@ -46,6 +46,10 @@
 				<label for="openEditorInput" id="openEditorLabel"></label>
 				<input type="checkbox" id="openEditorInput">
 			</div>
+			<div class="option">
+				<label for="autoOpenEditorInput" id="autoOpenEditorLabel"></label>
+				<input type="checkbox" id="autoOpenEditorInput">
+			</div>
 			<div class="option">
 				<label for="confirmInfobarInput" id="confirmInfobarLabel"></label>
 				<input type="checkbox" id="confirmInfobarInput">

+ 1 - 0
manifest.json

@@ -43,6 +43,7 @@
 			"js": [
 				"lib/single-file/index.js",
 				"lib/single-file/processors/hooks/content/content-hooks.js",
+				"lib/single-file/modules/html-serializer.js",
 				"extension/lib/single-file/index.js",
 				"extension/core/index.js",
 				"extension/core/content/content-bootstrap.js"