From 476538e99482e0d3a51575567f29a18cd26c107c Mon Sep 17 00:00:00 2001 From: MusicalBean Date: Sat, 26 Aug 2023 22:30:09 -0700 Subject: [PATCH] Improve refresh logic and fix bugs --- package.json | 2 +- source/Background/index.ts | 6 +-- source/ContentScript/index.ts | 22 ++++++++++ .../page-elements/trade-finder-row.ts | 10 +++-- .../page-enhancers/who-owns-enhancer.ts | 2 +- .../page-enhancers/who-wants-enhancer.ts | 2 - source/ContentScript/trader-repository.ts | 41 +++++++++++++++++++ source/manifest.json | 4 +- 8 files changed, 77 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 0951d29..c6f0c0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "encora-tools", - "version": "0.1.0", + "version": "0.1", "description": "Adds extra features to the Encora trading site.", "private": true, "repository": "https://github.com/musicalbean/encora-tools.git", diff --git a/source/Background/index.ts b/source/Background/index.ts index 39efddb..6c9e647 100644 --- a/source/Background/index.ts +++ b/source/Background/index.ts @@ -1,6 +1,6 @@ -import 'emoji-log'; -import {browser} from 'webextension-polyfill-ts'; +import { browser } from "webextension-polyfill-ts"; +import TraderRepository from "../ContentScript/trader-repository"; browser.runtime.onInstalled.addListener((): void => { - console.emoji('🦄', 'extension installed'); + TraderRepository.refreshGlobalTradersStorage(); }); diff --git a/source/ContentScript/index.ts b/source/ContentScript/index.ts index 46a948e..9384bce 100644 --- a/source/ContentScript/index.ts +++ b/source/ContentScript/index.ts @@ -2,6 +2,8 @@ import TradeFinderEnhancer from "./page-enhancers/trade-finder-enhancer"; import TradeFinderTheirsEnhancer from "./page-enhancers/trade-finder-theirs-enhancer"; import WhoOwnsEnhancer from "./page-enhancers/who-owns-enhancer"; import WhoWantsEnhancer from "./page-enhancers/who-wants-enhancer"; +import Settings from "./settings"; +import TraderRepository from "./trader-repository"; if (window.location.href.includes("who-owns.php")) { WhoOwnsEnhancer.enhance(document); @@ -18,3 +20,23 @@ if (window.location.href.includes("profile/trade_finder.php")) { if (window.location.href.includes("profile/trade_finder_theirs.php")) { TradeFinderTheirsEnhancer.enhance(document); } + +let refreshLinks = [ + "a[href*='/add-to-collection.php']", + "a[href*='/remove-from-collection.php']", + "a[href*='/add-to-wants.php']", + "a[href*='/remove-from-wants.php']", +]; + +refreshLinks.forEach((link) => { + document.querySelectorAll(link).forEach((link) => { + link.addEventListener("click", async () => { + setTimeout(() => { + TraderRepository.refreshGlobalTradersStorage(); + console.log("refreshing"); + }, 1000); + }); + }); +}); + +TraderRepository.refreshIfNecessary(); diff --git a/source/ContentScript/page-elements/trade-finder-row.ts b/source/ContentScript/page-elements/trade-finder-row.ts index 6ea5c20..dce71cc 100644 --- a/source/ContentScript/page-elements/trade-finder-row.ts +++ b/source/ContentScript/page-elements/trade-finder-row.ts @@ -35,9 +35,13 @@ export default class TradeFinderRow { } get open(): boolean { - return this.cells[0] - .querySelector("span")! - .innerText.includes("Open to trades"); + let badge = this.cells[0].querySelector("span"); + + if (!badge) { + return false; + } + + return badge.innerText.includes("Open to trades"); } get count(): number { diff --git a/source/ContentScript/page-enhancers/who-owns-enhancer.ts b/source/ContentScript/page-enhancers/who-owns-enhancer.ts index ae850b6..ba910cd 100644 --- a/source/ContentScript/page-enhancers/who-owns-enhancer.ts +++ b/source/ContentScript/page-enhancers/who-owns-enhancer.ts @@ -52,7 +52,7 @@ export default class WhoOwnsEnhancer { Settings.set("hideNonOpenRows", true); links - .filter((link) => link!.trader!.open === false) + .filter((link) => !link.trader || link!.trader!.open === false) .forEach((link) => link!.hide()); } diff --git a/source/ContentScript/page-enhancers/who-wants-enhancer.ts b/source/ContentScript/page-enhancers/who-wants-enhancer.ts index 205821f..26d9b03 100644 --- a/source/ContentScript/page-enhancers/who-wants-enhancer.ts +++ b/source/ContentScript/page-enhancers/who-wants-enhancer.ts @@ -9,8 +9,6 @@ export default class WhoWantsEnhancer { let b = Array.from(bs).find((b) => b.innerText.trim() === "WANTERS:")!; - console.log(bs); - let toggle = Toggle.create(); toggle.element.style.marginBottom = "1.5em"; diff --git a/source/ContentScript/trader-repository.ts b/source/ContentScript/trader-repository.ts index 322eead..56d4934 100644 --- a/source/ContentScript/trader-repository.ts +++ b/source/ContentScript/trader-repository.ts @@ -1,5 +1,6 @@ import { browser } from "webextension-polyfill-ts"; import Trader from "./trader"; +import Settings from "./settings"; export default class TraderRepository { page: Document; @@ -19,6 +20,18 @@ export default class TraderRepository { return new TraderRepository(traders!); } + /** + * Refreshes the global storage if necessary. + */ + static async refreshIfNecessary() { + if ( + !(await this.hasTradersInStorage()) || + (await this.shouldBeRefreshed()) + ) { + await this.refreshGlobalTradersStorage(); + } + } + /** * Fetches the traders who want my items from the Encora website. */ @@ -60,6 +73,8 @@ export default class TraderRepository { * from the Encora website and stores them in the global storage. */ static async refreshGlobalTradersStorage() { + await Settings.set("refresh", false); + let [wantsTraders, ownsTraders] = await Promise.all([ this.fetchTradersWhoWantMyItems(), this.fetchTradersWhoOwnMyWants(), @@ -69,7 +84,12 @@ export default class TraderRepository { (trader) => trader.toJSON() ); + if (traders.length === 0) { + return; + } + browser.storage.local.set({ traders: traders }); + await Settings.set("lastUpdated", new Date().toString()); } /** @@ -81,6 +101,27 @@ export default class TraderRepository { return storage.traders != null; } + /** + * Checks if the global storage should be refreshed. + */ + static async shouldBeRefreshed(): Promise { + let lastUpdated = await Settings.get("lastUpdated"); + + if (lastUpdated == null) { + return true; + } + + let now = new Date(); + let lastUpdatedDate = new Date(lastUpdated); + + // Refresh if the last update was more than 5 minutes ago. + if (now.getTime() - lastUpdatedDate.getTime() > 1000 * 60 * 5) { + return true; + } + + return false; + } + /** * Loads the traders from the global storage. */ diff --git a/source/manifest.json b/source/manifest.json index aa61fec..4ccb5fe 100644 --- a/source/manifest.json +++ b/source/manifest.json @@ -1,7 +1,7 @@ { - "manifest_version": "0.1.0", + "manifest_version": 2, "name": "Encora Tools", - "version": "0.0.1", + "version": "0.1.0", "icons": { "48": "assets/icons/tools-48.png",