From 030856be2a11b02d67365a7b0e0709f60e02d93c Mon Sep 17 00:00:00 2001 From: quoid Date: Sat, 8 Jan 2022 09:25:59 -0500 Subject: [PATCH 1/5] fix `GM_addStyle()` #194 #195 --- .../Userscripts Extension/Resources/content.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/extension/Userscripts Extension/Resources/content.js b/extension/Userscripts Extension/Resources/content.js index 650af3ee..cbfdea6e 100644 --- a/extension/Userscripts Extension/Resources/content.js +++ b/extension/Userscripts Extension/Resources/content.js @@ -334,16 +334,8 @@ function deleteValue(key) { } function addStyleSync(css) { - browser.runtime.sendMessage({name: "API_ADD_STYLE_SYNC", css: css}); + window.postMessage({id: uid, name: "API_ADD_STYLE_SYNC", css: css}); return css; - // try { - // const tag = document.createElement("style"); - // tag.textContent = css; - // document.head.appendChild(tag); - // return css; - // } catch (e) { - // console.log(e); - // } } function addStyle(css) { @@ -509,6 +501,14 @@ window.addEventListener("message", e => { } catch (e) { console.log(e); } + } else if (name === "API_ADD_STYLE_SYNC") { + console.log("SYNC"); + try { + message = {name: "API_ADD_STYLE_SYNC", css: e.data.css}; + browser.runtime.sendMessage(message); + } catch (e) { + console.log(e); + } } else if (name === "API_XHR_INJ") { message = {name: "API_XHR_CS", details: e.data.details, xhrId: e.data.xhrId}; browser.runtime.sendMessage(message, response => { From 0e5d3846d615b8dcb2cce1dc473dfec2cc16fdf6 Mon Sep 17 00:00:00 2001 From: quoid Date: Sat, 8 Jan 2022 09:26:06 -0500 Subject: [PATCH 2/5] update eslint --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index 9fc720a3..b33c1c1e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -102,7 +102,7 @@ "space-before-function-paren": ["error", { "anonymous": "never", "named": "never", - "asyncArrow": "never" + "asyncArrow": "always" }], "space-in-parens": [ "error", From e2f90e2e5df58e7d070207f1d5e9dc6b867a0ca0 Mon Sep 17 00:00:00 2001 From: quoid Date: Sat, 8 Jan 2022 09:26:52 -0500 Subject: [PATCH 3/5] remove console.log #194 #195 --- extension/Userscripts Extension/Resources/content.js | 1 - 1 file changed, 1 deletion(-) diff --git a/extension/Userscripts Extension/Resources/content.js b/extension/Userscripts Extension/Resources/content.js index cbfdea6e..abccefc4 100644 --- a/extension/Userscripts Extension/Resources/content.js +++ b/extension/Userscripts Extension/Resources/content.js @@ -502,7 +502,6 @@ window.addEventListener("message", e => { console.log(e); } } else if (name === "API_ADD_STYLE_SYNC") { - console.log("SYNC"); try { message = {name: "API_ADD_STYLE_SYNC", css: e.data.css}; browser.runtime.sendMessage(message); From 27339eca513c137a6abda2f4cc8ede4e52d1933b Mon Sep 17 00:00:00 2001 From: quoid Date: Sat, 8 Jan 2022 12:46:08 -0500 Subject: [PATCH 4/5] improve update checking #191 #196 --- .../Userscripts Extension/Resources/popup.js | 131 +++++++++++++----- src/popup/App.svelte | 85 +++++++++--- 2 files changed, 166 insertions(+), 50 deletions(-) diff --git a/extension/Userscripts Extension/Resources/popup.js b/extension/Userscripts Extension/Resources/popup.js index 0ecc4cfc..8ec73dc0 100644 --- a/extension/Userscripts Extension/Resources/popup.js +++ b/extension/Userscripts Extension/Resources/popup.js @@ -2837,12 +2837,12 @@ return child_ctx; } - // (416:0) {#if !active} + // (468:0) {#if !active} function create_if_block_10(ctx) { return { c: noop, m: noop, d: noop }; } - // (419:0) {#if showInstallPrompt} + // (471:0) {#if showInstallPrompt} function create_if_block_9(ctx) { let div; let t0; @@ -2884,7 +2884,7 @@ }; } - // (424:0) {#if error} + // (476:0) {#if error} function create_if_block_8(ctx) { let div; let t0; @@ -2934,7 +2934,7 @@ }; } - // (446:8) {:else} + // (498:8) {:else} function create_else_block$3(ctx) { let div; let each_blocks = []; @@ -3007,7 +3007,7 @@ }; } - // (444:35) + // (496:35) function create_if_block_7(ctx) { let div; @@ -3029,7 +3029,7 @@ }; } - // (440:28) + // (492:28) function create_if_block_6$1(ctx) { let div; let t0; @@ -3067,7 +3067,7 @@ }; } - // (438:8) {#if inactive} + // (490:8) {#if inactive} function create_if_block_5$1(ctx) { let div; @@ -3089,7 +3089,7 @@ }; } - // (435:4) {#if loading} + // (487:4) {#if loading} function create_if_block_4$1(ctx) { let loader; let current; @@ -3119,7 +3119,7 @@ }; } - // (448:16) {#each list as item (item.filename)} + // (500:16) {#each list as item (item.filename)} function create_each_block$3(key_1, ctx) { let first; let popupitem; @@ -3178,7 +3178,7 @@ }; } - // (461:0) {#if !inactive && platform === "macos"} + // (513:0) {#if !inactive && platform === "macos"} function create_if_block_3$1(ctx) { let div1; let div0; @@ -3211,7 +3211,7 @@ }; } - // (494:18) + // (546:18) function create_if_block_2$1(ctx) { let view; let current; @@ -3261,7 +3261,7 @@ }; } - // (480:22) + // (532:22) function create_if_block_1$1(ctx) { let view; let current; @@ -3311,7 +3311,7 @@ }; } - // (466:0) {#if showUpdates} + // (518:0) {#if showUpdates} function create_if_block$6(ctx) { let view; let current; @@ -3361,7 +3361,7 @@ }; } - // (495:4) {showAll = false; refreshView()}} showLoaderOnDisabled={false} > + // (547:4) {showAll = false; refreshView()}} showLoaderOnDisabled={false} > function create_default_slot_2(ctx) { let allitemsview; let current; @@ -3401,7 +3401,7 @@ }; } - // (481:4) showInstall = false} showLoaderOnDisabled={true} > + // (533:4) showInstall = false} showLoaderOnDisabled={true} > function create_default_slot_1(ctx) { let installview; let current; @@ -3445,7 +3445,7 @@ }; } - // (467:4) showUpdates = false} showLoaderOnDisabled={true} > + // (519:4) showUpdates = false} showLoaderOnDisabled={true} > function create_default_slot(ctx) { let updateview; let current; @@ -3861,6 +3861,62 @@ await browser.tabs.create({ url }); } + async function shouldCheckForUpdates() { + // if there's no network connectivity, do not check for updates + if (!window || !window.navigator || !window.navigator.onLine) { + console.log("user is offline, not running update check"); + return false; + } + + // when an update check is run, a timestamp is saved to extension storage + // only check for updates every n milliseconds to avoid delaying popup load regularly + const checkInterval = 24 * 60 * 60 * 1000; // 24hr, 86400000 + + const timestampMs = Date.now(); + let lastUpdateCheck = 0; + + // check extension storage for saved key/val + // if there's an issue getting extension storage, skip the check + let lastUpdateCheckObj; + + try { + lastUpdateCheckObj = await browser.storage.local.get(["lastUpdateCheck"]); + } catch(error) { + console.error("Error checking extension storage " + error); + return false; + } + + // if extension storage doesn't have key, run the check + // key/val will be saved after the update check runs + if (Object.keys(lastUpdateCheckObj).length === 0) { + console.log("no last check saved, running update check"); + return true; + } + + // if the val is not a number, something went wrong, check anyway + // when update re-runs, new val of the proper type will be saved + if (!Number.isFinite(lastUpdateCheckObj.lastUpdateCheck)) { + console.log("run check saved with wrong type, running update check"); + return true; + } + + // at this point it is known that key exists and value is a number + // update local var with the val saved to extension storage + lastUpdateCheck = lastUpdateCheckObj.lastUpdateCheck; + + // if less than n milliseconds have passed, don't check + if (timestampMs - lastUpdateCheck < checkInterval) { + console.log("not enough time has passed, not running update check"); + return false; + } + + console.log((timestampMs - lastUpdateCheck) / (1000 * 60 * 60) + " hours have passed"); + console.log("running update check"); + + // otherwise run the check + return true; + } + function instance$9($$self, $$props, $$invalidate) { let error = undefined; let active = true; @@ -4100,24 +4156,33 @@ } // get updates - let updatesResponse; + const checkUpdates = await shouldCheckForUpdates(); - try { - updatesResponse = await browser.runtime.sendNativeMessage({ name: "POPUP_UPDATES" }); - } catch(error) { - console.log("Error for updates promise: " + error); - $$invalidate(11, initError = true); - $$invalidate(2, loading = false); - return; - } + if (checkUpdates) { + let updatesResponse; - if (updatesResponse.error) { - $$invalidate(0, error = updatesResponse.error); - $$invalidate(2, loading = false); - $$invalidate(3, disabled = false); - return; - } else { - $$invalidate(6, updates = updatesResponse.updates); + try { + updatesResponse = await browser.runtime.sendNativeMessage({ name: "POPUP_UPDATES" }); + + // save timestamp in ms to extension storage + const timestampMs = Date.now(); + + await browser.storage.local.set({ "lastUpdateCheck": timestampMs }); + } catch(error) { + console.log("Error for updates promise: " + error); + $$invalidate(11, initError = true); + $$invalidate(2, loading = false); + return; + } + + if (updatesResponse.error) { + $$invalidate(0, error = updatesResponse.error); + $$invalidate(2, loading = false); + $$invalidate(3, disabled = false); + return; + } else { + $$invalidate(6, updates = updatesResponse.updates); + } } // check if current page url is a userscript @@ -4172,8 +4237,6 @@ } } - console.log("rs"); - // on ios and ipados (split view) programmatically set the height of the scrollable container // first get the header height const headerHeight = header.offsetHeight; diff --git a/src/popup/App.svelte b/src/popup/App.svelte index 975f8e98..0cc932c2 100644 --- a/src/popup/App.svelte +++ b/src/popup/App.svelte @@ -157,6 +157,53 @@ await browser.tabs.create({url: url}); } + async function shouldCheckForUpdates() { + // if there's no network connectivity, do not check for updates + if (!window || !window.navigator || !window.navigator.onLine) { + console.log("user is offline, not running update check"); + return false; + } + // when an update check is run, a timestamp is saved to extension storage + // only check for updates every n milliseconds to avoid delaying popup load regularly + const checkInterval = 24 * 60 * 60 * 1000; // 24hr, 86400000 + const timestampMs = Date.now(); + let lastUpdateCheck = 0; + // check extension storage for saved key/val + // if there's an issue getting extension storage, skip the check + let lastUpdateCheckObj; + try { + lastUpdateCheckObj = await browser.storage.local.get(["lastUpdateCheck"]); + } catch (error) { + console.error("Error checking extension storage " + error); + return false; + } + // if extension storage doesn't have key, run the check + // key/val will be saved after the update check runs + if (Object.keys(lastUpdateCheckObj).length === 0) { + console.log("no last check saved, running update check"); + return true; + } + // if the val is not a number, something went wrong, check anyway + // when update re-runs, new val of the proper type will be saved + if (!Number.isFinite(lastUpdateCheckObj.lastUpdateCheck)) { + console.log("run check saved with wrong type, running update check"); + return true; + } + // at this point it is known that key exists and value is a number + // update local var with the val saved to extension storage + lastUpdateCheck = lastUpdateCheckObj.lastUpdateCheck; + // if less than n milliseconds have passed, don't check + if ((timestampMs - lastUpdateCheck) < checkInterval) { + console.log("not enough time has passed, not running update check"); + return false; + } + + console.log(((timestampMs - lastUpdateCheck) / (1000 * 60 * 60)) + " hours have passed"); + console.log("running update check"); + // otherwise run the check + return true; + } + async function openSaveLocation() { disabled = true; loading = true; @@ -253,22 +300,28 @@ } // get updates - let updatesResponse; - try { - updatesResponse = await browser.runtime.sendNativeMessage({name: "POPUP_UPDATES"}); - } catch (error) { - console.log("Error for updates promise: " + error); - initError = true; - loading = false; - return; - } - if (updatesResponse.error) { - error = updatesResponse.error; - loading = false; - disabled = false; - return; - } else { - updates = updatesResponse.updates; + const checkUpdates = await shouldCheckForUpdates(); + if (checkUpdates) { + let updatesResponse; + try { + updatesResponse = await browser.runtime.sendNativeMessage({name: "POPUP_UPDATES"}); + // save timestamp in ms to extension storage + const timestampMs = Date.now(); + await browser.storage.local.set({"lastUpdateCheck": timestampMs}); + } catch (error) { + console.log("Error for updates promise: " + error); + initError = true; + loading = false; + return; + } + if (updatesResponse.error) { + error = updatesResponse.error; + loading = false; + disabled = false; + return; + } else { + updates = updatesResponse.updates; + } } // check if current page url is a userscript From 039ae56d535df08f968f63e6da977831709e47d7 Mon Sep 17 00:00:00 2001 From: quoid Date: Sat, 8 Jan 2022 13:23:14 -0500 Subject: [PATCH 5/5] update version and build numbers --- .../Userscripts.xcodeproj/project.pbxproj | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/extension/Userscripts.xcodeproj/project.pbxproj b/extension/Userscripts.xcodeproj/project.pbxproj index d9d3d931..a9c33848 100644 --- a/extension/Userscripts.xcodeproj/project.pbxproj +++ b/extension/Userscripts.xcodeproj/project.pbxproj @@ -551,7 +551,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CODE_SIGN_ENTITLEMENTS = "Userscripts-iOS/Userscripts-iOS.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = J74Q8V8V8N; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "Userscripts-iOS/Info.plist"; @@ -567,7 +567,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.1; OTHER_LDFLAGS = ( "-framework", SafariServices, @@ -592,7 +592,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CODE_SIGN_ENTITLEMENTS = "Userscripts-iOS/Userscripts-iOS.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = J74Q8V8V8N; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "Userscripts-iOS/Info.plist"; @@ -608,7 +608,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.1; OTHER_LDFLAGS = ( "-framework", SafariServices, @@ -631,7 +631,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CODE_SIGN_ENTITLEMENTS = "Userscripts-iOS Extension/Userscripts-iOS Extension.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = J74Q8V8V8N; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "Userscripts-iOS Extension/Info.plist"; @@ -643,7 +643,7 @@ "@executable_path/Frameworks", "@executable_path/../../Frameworks", ); - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.1; OTHER_LDFLAGS = ( "-framework", SafariServices, @@ -664,7 +664,7 @@ CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; CODE_SIGN_ENTITLEMENTS = "Userscripts-iOS Extension/Userscripts-iOS Extension.entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 13; + CURRENT_PROJECT_VERSION = 14; DEVELOPMENT_TEAM = J74Q8V8V8N; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "Userscripts-iOS Extension/Info.plist"; @@ -676,7 +676,7 @@ "@executable_path/Frameworks", "@executable_path/../../Frameworks", ); - MARKETING_VERSION = 1.0; + MARKETING_VERSION = 1.0.1; OTHER_LDFLAGS = ( "-framework", SafariServices, @@ -817,7 +817,7 @@ CODE_SIGN_ENTITLEMENTS = "Userscripts Extension/Userscripts Extension.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 41; + CURRENT_PROJECT_VERSION = 42; DEVELOPMENT_TEAM = J74Q8V8V8N; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "Userscripts Extension/Info.plist"; @@ -827,7 +827,7 @@ "@executable_path/../../../../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 4.0.9; + MARKETING_VERSION = 4.0.10; PRODUCT_BUNDLE_IDENTIFIER = "com.userscripts.macos.Userscripts-Extension"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -841,7 +841,7 @@ CODE_SIGN_ENTITLEMENTS = "Userscripts Extension/Userscripts Extension.entitlements"; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 41; + CURRENT_PROJECT_VERSION = 42; DEVELOPMENT_TEAM = J74Q8V8V8N; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = "Userscripts Extension/Info.plist"; @@ -851,7 +851,7 @@ "@executable_path/../../../../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 4.0.9; + MARKETING_VERSION = 4.0.10; PRODUCT_BUNDLE_IDENTIFIER = "com.userscripts.macos.Userscripts-Extension"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -868,7 +868,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 41; + CURRENT_PROJECT_VERSION = 42; DEVELOPMENT_TEAM = J74Q8V8V8N; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = Userscripts/Info.plist; @@ -877,7 +877,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 4.0.9; + MARKETING_VERSION = 4.0.10; PRODUCT_BUNDLE_IDENTIFIER = com.userscripts.macos; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; @@ -893,7 +893,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 41; + CURRENT_PROJECT_VERSION = 42; DEVELOPMENT_TEAM = J74Q8V8V8N; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = Userscripts/Info.plist; @@ -902,7 +902,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 4.0.9; + MARKETING_VERSION = 4.0.10; PRODUCT_BUNDLE_IDENTIFIER = com.userscripts.macos; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0;