Procházet zdrojové kódy

made save on unload compatible with SPA

Gildas před 7 roky
rodič
revize
66acfc92c4
1 změnil soubory, kde provedl 18 přidání a 1 odebrání
  1. 18 1
      extension/core/content/content-autosave.js

+ 18 - 1
extension/core/content/content-autosave.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global singlefile, frameTree, browser, window, addEventListener, removeEventListener, document, location, docHelper, setTimeout */
+/* global singlefile, frameTree, browser, window, history, HTMLDocument, dispatchEvent, CustomEvent, addEventListener, removeEventListener, document, location, docHelper, setTimeout */
 
 this.singlefile.autosave = this.singlefile.autosave || (async () => {
 
@@ -34,6 +34,13 @@ this.singlefile.autosave = this.singlefile.autosave || (async () => {
 			refresh();
 		}
 	});
+	if (document instanceof HTMLDocument) {
+		console.warn("SingleFile is hooking The history.pushState to detect navigation."); // eslint-disable-line no-console		
+		const scriptElement = document.createElement("script");
+		scriptElement.textContent = `(${hookPushState.toString()})()`;
+		document.documentElement.appendChild(scriptElement);
+		scriptElement.remove();
+	}
 	return {};
 
 	async function autoSavePage() {
@@ -86,10 +93,12 @@ this.singlefile.autosave = this.singlefile.autosave || (async () => {
 		if (enabled) {
 			if (!listenerAdded) {
 				addEventListener("unload", onUnload);
+				addEventListener("single-file-push-state", onUnload);
 				listenerAdded = true;
 			}
 		} else {
 			removeEventListener("unload", onUnload);
+			removeEventListener("single-file-push-state", onUnload);
 			listenerAdded = false;
 		}
 	}
@@ -115,4 +124,12 @@ this.singlefile.autosave = this.singlefile.autosave || (async () => {
 		}
 	}
 
+	function hookPushState() {
+		const pushState = history.pushState;
+		history.pushState = function (state, title, url) {
+			dispatchEvent(new CustomEvent("single-file-push-state", { detail: { state, title, url } }));
+			pushState.call(history, state, title, url);
+		};
+	}
+
 })();