A rewrite of Poly+, my quality-of-life browser extension for Polytoria. Built entirely fresh using the WXT extension framework, Typescript, and with added better overall code quality.
extension
1import { cache } from "./storage";
2import { cacheInterface } from "./types";
3import * as currencyPackages from "./currencyPackages.json"
4
5// ? maybe make this use Meta data on an actual container
6export async function pullCache(key: string, replenish: Function, forceReplenish: boolean) {
7 const cacheStorage: cacheInterface = await cache.getValue();
8 if (!cacheStorage[key] || forceReplenish) {
9 const replenishedCache = await replenish();
10 cacheStorage[key] = replenishedCache;
11 //@ts-ignore: why
12 cache.setValue(cacheStorage);
13 return replenishedCache;
14 }
15 return cacheStorage[key];
16}
17
18export async function getUserDetails() {
19 const profileLink: HTMLLinkElement = document.querySelector('.navbar a.text-reset[href^="/users/"]')!;
20 const brickBalance = document.getElementsByClassName('brickBalanceCont')[0];
21
22 if (!profileLink || !brickBalance) return null;
23 return {
24 username: profileLink.innerText.trim(),
25 userId: parseInt(profileLink.href.split('/')[4]),
26 bricks: parseInt(brickBalance.textContent!.replace(/,/g, ""))
27 }
28}
29
30export function bricksToCurrency(bricks: number, currency: string): string | null {
31 const _currencyPackages = currencyPackages as Record<string, Array<Array<number>>>;
32 const packages = _currencyPackages[currency];
33
34 if (!packages) {
35 console.warn('[Poly+] Missing currency package data for selected currency!');
36 return null;
37 }
38
39 let totalValue = 0;
40 for (const [currencyValue, bricksValue] of packages) {
41 while (bricks >= bricksValue) {
42 bricks -= bricksValue;
43 totalValue += currencyValue;
44 }
45 }
46
47 return `~${totalValue.toFixed(2)} ${currency}`;
48}