Просмотр исходного кода

Fixed autosave with SPA websites (e.g. youtube)

Former-commit-id: c8e3c6eab08aa00a4948c021f1aa1d628b551c23
Gildas 5 лет назад
Родитель
Сommit
83b5e07971
2 измененных файлов с 29 добавлено и 5 удалено
  1. 9 1
      extension/core/bg/tabs.js
  2. 20 4
      extension/core/content/content-bootstrap.js

+ 9 - 1
extension/core/bg/tabs.js

@@ -20,15 +20,17 @@
  *   notice and a URL through which recipients can access the Corresponding 
  *   notice and a URL through which recipients can access the Corresponding 
  *   Source.
  *   Source.
  */
  */
-/* global browser, singlefile */
+/* global browser, singlefile, setTimeout */
 
 
 singlefile.extension.core.bg.tabs = (() => {
 singlefile.extension.core.bg.tabs = (() => {
 
 
+	const DELAY_MAYBE_INIT = 1500;
 	const pendingPrompts = new Map();
 	const pendingPrompts = new Map();
 
 
 	browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
 	browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
 	browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
 	browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
 	browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
 	browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
+	browser.tabs.onUpdated.addListener((tabId, changeInfo) => onTabUpdated(tabId, changeInfo));
 	return {
 	return {
 		onMessage,
 		onMessage,
 		get: async options => {
 		get: async options => {
@@ -136,6 +138,12 @@ singlefile.extension.core.bg.tabs = (() => {
 		}
 		}
 	}
 	}
 
 
+	async function onTabUpdated(tabId, changeInfo) {
+		if (changeInfo.status == "complete") {
+			setTimeout(() => browser.tabs.sendMessage(tabId, { method: "content.maybeInit" }), DELAY_MAYBE_INIT);
+		}
+	}
+
 	function onTabCreated(tab) {
 	function onTabCreated(tab) {
 		singlefile.extension.ui.bg.main.onTabCreated(tab);
 		singlefile.extension.ui.bg.main.onTabCreated(tab);
 	}
 	}

+ 20 - 4
extension/core/content/content-bootstrap.js

@@ -29,7 +29,7 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 
 
 	const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
 	const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
 
 
-	let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved;
+	let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved, previousLocationHref;
 	singlefile.extension.core.content.updatedResources = {};
 	singlefile.extension.core.content.updatedResources = {};
 	browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
 	browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
 		options = message.options;
 		options = message.options;
@@ -45,12 +45,15 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 		}
 		}
 	});
 	});
 	browser.runtime.onMessage.addListener(message => {
 	browser.runtime.onMessage.addListener(message => {
-		if ((autoSaveEnabled && message.method == "content.autosave") || message.method == "content.init" || message.method == "devtools.resourceCommitted" || message.method == "common.promptValueRequest") {
+		if ((autoSaveEnabled && message.method == "content.autosave") ||
+			message.method == "content.maybeInit" ||
+			message.method == "content.init" ||
+			message.method == "devtools.resourceCommitted" ||
+			message.method == "common.promptValueRequest") {
 			return onMessage(message);
 			return onMessage(message);
 		}
 		}
 	});
 	});
-	browser.runtime.sendMessage({ method: "tabs.init" });
-	browser.runtime.sendMessage({ method: "ui.processInit" });
+	init();
 	return {};
 	return {};
 
 
 	async function onMessage(message) {
 	async function onMessage(message) {
@@ -58,6 +61,10 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 			initAutoSavePage(message);
 			initAutoSavePage(message);
 			return {};
 			return {};
 		}
 		}
+		if (message.method == "content.maybeInit") {
+			init();
+			return {};
+		}
 		if (message.method == "content.init") {
 		if (message.method == "content.init") {
 			options = message.options;
 			options = message.options;
 			autoSaveEnabled = message.autoSaveEnabled;
 			autoSaveEnabled = message.autoSaveEnabled;
@@ -74,6 +81,15 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 		}
 		}
 	}
 	}
 
 
+	function init() {
+		if (previousLocationHref != location.href) {
+			pageAutoSaved = false;
+			previousLocationHref = location.href;
+			browser.runtime.sendMessage({ method: "tabs.init" });
+			browser.runtime.sendMessage({ method: "ui.processInit" });
+		}
+	}
+
 	async function initAutoSavePage(message) {
 	async function initAutoSavePage(message) {
 		options = message.options;
 		options = message.options;
 		if (document.readyState != "complete") {
 		if (document.readyState != "complete") {