Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 90 additions & 85 deletions src/ext/action-popup/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -146,54 +146,54 @@
initialize();
}

async function shouldCheckForUpdates() {
// if there's no network connectivity, do not check for updates
if (!window || !window.navigator || !window.navigator.onLine) {
console.info("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.info("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.info("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.info("not enough time has passed, not running update check");
return false;
}
// async function shouldCheckForUpdates() {
// // if there's no network connectivity, do not check for updates
// if (!window || !window.navigator || !window.navigator.onLine) {
// console.info("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.info("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.info("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.info("not enough time has passed, not running update check");
// return false;
// }

console.info(
`${(timestampMs - lastUpdateCheck) / (1000 * 60 * 60)} hours have passed`,
);
console.info("running update check");
// otherwise run the check
return true;
}
// console.info(
// `${(timestampMs - lastUpdateCheck) / (1000 * 60 * 60)} hours have passed`,
// );
// console.info("running update check");
// // otherwise run the check
// return true;
// }

async function openSaveLocation() {
disabled = true;
Expand Down Expand Up @@ -302,33 +302,36 @@
}
items = matches.matches;

// get updates
const checkUpdates = await shouldCheckForUpdates();
if (checkUpdates) {
let updatesResponse;
try {
// save timestamp in ms to extension storage
const timestampMs = Date.now();
await browser.storage.local.set({ lastUpdateCheck: timestampMs });
abort = true;
updatesResponse = await sendNativeMessage({ name: "POPUP_UPDATES" });
} catch (error) {
console.error(`Error for updates promise: ${error}`);
initError = true;
loading = false;
abort = false;
return;
}
if (updatesResponse.error) {
errorNotification = updatesResponse.error;
loading = false;
disabled = false;
abort = false;
return;
}
updates = updatesResponse.updates;
abort = false;
}
/**
* get updates
* disabled due to - https://github.com/quoid/userscripts/issues/894
*/
// const checkUpdates = await shouldCheckForUpdates();
// if (checkUpdates) {
// let updatesResponse;
// try {
// // save timestamp in ms to extension storage
// const timestampMs = Date.now();
// await browser.storage.local.set({ lastUpdateCheck: timestampMs });
// abort = true;
// updatesResponse = await sendNativeMessage({ name: "POPUP_UPDATES" });
// } catch (error) {
// console.error(`Error for updates promise: ${error}`);
// initError = true;
// loading = false;
// abort = false;
// return;
// }
// if (updatesResponse.error) {
// errorNotification = updatesResponse.error;
// loading = false;
// disabled = false;
// abort = false;
// return;
// }
// updates = updatesResponse.updates;
// abort = false;
// }

// check if current page url is a userscript
if (strippedUrl.endsWith(".user.js")) {
Expand All @@ -344,15 +347,18 @@
disabled = false;
}

async function abortUpdates() {
// sends message to swift side canceling all URLSession tasks
sendNativeMessage({ name: "CANCEL_REQUESTS" });
// timestamp for checking updates happens right before update fetching
// that means when this function runs the timestamp has already been saved
// reloading the window will essentially skip the update check
// since the subsequent popup load will not check for updates
window.location.reload();
}
/**
* Not working due to: https://github.com/quoid/userscripts/issues/276#issuecomment-3507547737
*/
// async function abortUpdates() {
// // sends message to swift side canceling all URLSession tasks
// sendNativeMessage({ name: "CANCEL_REQUESTS" });
// // timestamp for checking updates happens right before update fetching
// // that means when this function runs the timestamp has already been saved
// // reloading the window will essentially skip the update check
// // since the subsequent popup load will not check for updates
// window.location.reload();
// }

/** @type {import("svelte/elements").UIEventHandler<Window>} */
// async function resizeHandler(event) {
Expand Down Expand Up @@ -473,7 +479,6 @@
loading={disabled}
closeClick={() => (showUpdates = false)}
showLoaderOnDisabled={true}
abortClick={abortUpdates}
abort={showUpdates}
>
<UpdateView
Expand Down Expand Up @@ -565,7 +570,7 @@
</div>
<div class="main {rowColors || ''}" bind:this={main}>
{#if loading}
<Loader abortClick={abortUpdates} {abort} />
<Loader {abort} />
{:else if inactive}
<div class="none">Popup inactive on extension page</div>
{:else if firstGuide}
Expand Down
3 changes: 1 addition & 2 deletions src/ext/action-popup/Components/View.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
export let closeClick;
export let showLoaderOnDisabled = true;
export let abort = false;
export let abortClick = () => {};

function slide(node, params) {
return {
Expand All @@ -28,7 +27,7 @@
</div>
<div class="view__body">
{#if loading && showLoaderOnDisabled}
<Loader backgroundColor="var(--color-bg-primary)" {abortClick} {abort} />
<Loader backgroundColor="var(--color-bg-primary)" {abort} />
{:else}
<slot><div>Slot content is required...</div></slot>
{/if}
Expand Down
7 changes: 1 addition & 6 deletions src/ext/shared/Components/Loader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import iconLoader from "../../shared/img/icon-loader.svg?raw";

export let abort = false;
export let abortClick = () => {};
export let backgroundColor = "var(--color-bg-secondary)";

/**
Expand All @@ -27,11 +26,7 @@
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html iconLoader}
{#if abort}
<div>
Fetching resources, <button class="link" on:click={abortClick}>
cancel request
</button>
</div>
<div>Fetching resources, please wait... (timeout after 30s)</div>
{/if}
</div>

Expand Down