From fb25d27e0d980d4fd0d3ab9339364da671f5ef58 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 25 Dec 2022 02:56:39 +0100 Subject: [PATCH] Rerun script on new page --- sn_translator/sn-translator.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sn_translator/sn-translator.js b/sn_translator/sn-translator.js index c0a54c9..ff558e3 100644 --- a/sn_translator/sn-translator.js +++ b/sn_translator/sn-translator.js @@ -66,6 +66,21 @@ function addButtons() { } (async function () { - await sleep(1000); + // Sleep before running script on page load + // since else we might get overwritten by loading content. + const initialSleep = 1000; + await sleep(initialSleep); + + let pathname = window.location.pathname; + console.log(`Current location: ${pathname}`); addButtons(); + // Check if URL changed and rerun script + const scriptInterval = 1000; + setInterval(() => { + if (window.location.pathname !== pathname) { + pathname = window.location.pathname; + console.log(`New location detected: ${pathname}`); + addButtons(); + } + }, scriptInterval); })();