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 * as api from "@/utils/api";
2
3/**
4 * Adds a quick accept all friend requests button & a decline all friend requests button to the top of the /my/friends/ page grid.
5 */
6export function actions() {
7 const container = document.getElementById("friends-container")!;
8
9 const actionBtns = document.createElement("div");
10 actionBtns.classList.add("row", "mb-3");
11 actionBtns.innerHTML = `
12 <div class="col">
13 <button class="btn btn-success w-100">
14 Accept All
15 </button>
16 </div>
17 <div class="col">
18 <button class="btn btn-danger w-100">
19 Decline All
20 </button>
21 </div>
22 `;
23
24 const acceptAll: HTMLButtonElement = actionBtns.querySelector(
25 ".btn-success",
26 )!;
27 const declineAll: HTMLButtonElement = actionBtns.querySelector(
28 ".btn-danger",
29 )!;
30
31 const firstPage = Array.from(container.getElementsByTagName("a"))
32 .map((link) => link.getAttribute("href")?.split("/")[2]);
33
34 const setDisabled = function (value: boolean) {
35 acceptAll.disabled = value;
36 declineAll.disabled = value;
37 };
38
39 if (firstPage.length == 0) {
40 setDisabled(true);
41 } else {
42 acceptAll.addEventListener("click", async () => {
43 setDisabled(true);
44
45 const ids = await api.iterate(
46 "internal",
47 "friends/requests?page=",
48 null,
49 1,
50 );
51 const payloads = ids.map((request: any) => ({
52 method: "POST",
53 headers: {
54 "Content-Type": "application/json",
55 },
56 body: JSON.stringify({
57 userID: request.senderID,
58 }),
59 }));
60 api.batchAction("friends/send", payloads);
61 });
62
63 declineAll.addEventListener("click", async () => {
64 setDisabled(true);
65
66 const ids = await api.iterate(
67 "internal",
68 "friends/requests?page=",
69 null,
70 1,
71 );
72 const payloads = ids.map((request: any) => ({
73 method: "POST",
74 headers: {
75 "Content-Type": "application/json",
76 },
77 body: JSON.stringify({
78 userID: request.senderID,
79 }),
80 }));
81 api.batchAction("friends/remove", payloads);
82 });
83 }
84
85 container.parentElement!.insertBefore(actionBtns, container);
86}
87
88export function checkboxes() {}