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 config from "../config.json";
2
3export async function batch(
4 type: "public" | "internal",
5 path: string,
6 ids: string[],
7 headers?: Record<string, any>
8): Promise<any> {
9 if (!config.api.enabled) {
10 return "disabled"
11 };
12
13 if (!headers) headers = {};
14 const res: Record<string, any> = {};
15
16 for (const id of ids) {
17 const info = await (await fetch(config.api.urls[type] + path + id, headers)).json();
18 res[id] = info;
19 };
20 return res;
21};
22
23export async function iterate(
24 type: "public" | "internal",
25 path: string,
26 keys: {
27 data: string,
28 metadata: string
29 } | null,
30 startPage: number,
31 endingPage?: number
32): Promise<any> {
33 if (!config.api.enabled) {
34 return "disabled"
35 };
36
37 let res: Array<any> = [];
38 const firstPage: { [key: string]: any } = await (await fetch(config.api.urls[type] + path + 1)).json();
39
40 if (!keys) {
41 keys = {
42 data: "data",
43 metadata: "meta"
44 };
45 };
46 res.push(...firstPage[keys.data]);
47
48 if (!endingPage) endingPage = firstPage[keys.metadata].lastPage;
49 for (let index = startPage + 1; index < endingPage!; index++) {
50 const page = await (await fetch(config.api.urls[type] + path + index)).json();
51 res.push(...page[keys.data]);
52 };
53 return res;
54};
55
56export async function batchAction(path: string, headers: Array<Record<string, any>>) {
57 if (!config.api.enabled) {
58 return "disabled"
59 };
60
61 for (const request of headers) {
62 fetch(config.api.urls.internal + path, request);
63 };
64};