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('.btn-success')!;
25 const declineAll: HTMLButtonElement = actionBtns.querySelector('.btn-danger')!;
26
27 const firstPage = Array.from(container.getElementsByTagName('a'))
28 .map((link) => link.getAttribute('href')?.split('/')[2]);
29
30 const setDisabled = function(value: boolean) {
31 acceptAll.disabled = value;
32 declineAll.disabled = value;
33 };
34
35 if (firstPage.length == 0) {
36 setDisabled(true);
37 } else {
38 acceptAll.addEventListener('click', async () => {
39 setDisabled(true);
40
41 const ids = await api.iterate('internal', 'friends/requests?page=', null, 1);
42 const payloads = ids.map((request: any) => ({
43 method: 'POST',
44 headers: {
45 'Content-Type': 'application/json'
46 },
47 body: JSON.stringify({
48 userID: request.senderID
49 })
50 }));
51 api.batchAction('friends/send', payloads);
52 });
53
54 declineAll.addEventListener('click', async () => {
55 setDisabled(true);
56
57 const ids = await api.iterate('internal', 'friends/requests?page=', null, 1);
58 const payloads = ids.map((request: any) => ({
59 method: 'POST',
60 headers: {
61 'Content-Type': 'application/json'
62 },
63 body: JSON.stringify({
64 userID: request.senderID
65 })
66 }));
67 api.batchAction('friends/remove', payloads);
68 });
69 };
70
71 container.parentElement!.insertBefore(actionBtns, container);
72};
73
74export function checkboxes() {};