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 { preferences, _favoritedPlaces, _bestFriends } from "@/utils/storage"; 2import { placeApiSchema, userApiSchema } from "@/utils/types"; 3import { pullCache } from "@/utils/utilities"; 4import * as api from "@/utils/api"; 5 6export default defineContentScript({ 7 matches: ['https://polytoria.com/', 'https://polytoria.com/home'], 8 main() { 9 preferences.getPreferences() 10 .then((values) => { 11 if (values.favoritedPlaces.enabled) favoritedPlaces(); 12 if (values.bestFriends.enabled) bestFriends(); 13 if (values.irlBrickPrice.enabled) irlBrickPrice(); 14 }); 15 } 16}); 17 18function favoritedPlaces() { 19 _favoritedPlaces.getValue() 20 .then(async (places) => { 21 const container = document.createElement('div'); 22 container.innerHTML = ` 23 <div class="row reqFadeAnim px-2 px-lg-0"> 24 <div class="col"> 25 <h6 class="dash-ctitle2">Jump right back into your favorite places</h6> 26 <h5 class="dash-ctitle">Favorited Places</h5> 27 </div> 28 </div> 29 <div class="card card-dash mcard mb-3"> 30 <div class="card-body p-0 m-1 scrollFadeContainer"> 31 <div class="text-center p-5"> 32 <div class="spinner-border text-muted" role="status"> 33 <span class="visually-hidden">Loading...</span> 34 </div> 35 </div> 36 </div> 37 </div> 38 `; 39 40 const card: HTMLElement = container.getElementsByClassName('scrollFadeContainer')[0] as HTMLElement; 41 const column = document.getElementsByClassName('col-lg-8')[0]; 42 43 if (document.getElementsByClassName('home-event-container')[0] === undefined) { 44 column.insertBefore(container, column.children[0]); 45 } else { 46 column.insertBefore(container, column.children[1]); 47 }; 48 49 const placeData: Array<placeApiSchema> | "disabled" = await pullCache( 50 'favoritedPlaces', 51 async () => await api.batch('public', 'places/', places), 52 300000, 53 true 54 ); 55 56 if (placeData == "disabled") { 57 console.error('[Poly+] API is disabled, cancelling favorited places loading..'); 58 card.innerHTML = ` 59 <div class="text-center p-2"> 60 <img src="${ browser.runtime.getURL('/svgs/error.svg') }" width="100" height="100"> 61 <p class="text-muted mb-0">Sorry! This feature is currently unavailable. Please check back later!</p> 62 </div> 63 `; 64 65 return; 66 }; 67 68 for (let i = 0; i < places.length; i++) { 69 const id = places.toSorted((a, b) => parseInt(b) - parseInt(a))[i] 70 const details = placeData[parseInt(id)] 71 if (!details) { 72 console.warn("[Poly+] Missing cached place data for ID " + id); 73 continue; 74 } 75 76 const scrollCard = document.createElement('a') 77 scrollCard.classList.value = 'd-none' 78 scrollCard.href = '/places/' + id 79 scrollCard.innerHTML = ` 80 <div class="scrollFade card me-2 place-card force-desktop text-center mb-2" style="opacity: 1;"> 81 <div class="card-body"> 82 <div class="ratings-header"> 83 <img src="${details.thumbnail}" class="place-card-image" style="position: relative;"> 84 <div class="p+pinned_games_playing" style="position: absolute;background: linear-gradient(to bottom, #000000f7, transparent, transparent, transparent);width: 100%;height: 100%;top: 0;left: 0;border-radius: 15px;padding-top: 12px;color: gray;font-size: 0.8rem;"> 85 <i class="fa-duotone fa-users"></i> 86 <span> 87 ${details.playing} 88 Playing 89 </span> 90 </div> 91 </div> 92 <div> 93 <div class="mt-2 mb-1 place-card-title"> 94 ${details.name} 95 </div> 96 </div> 97 </div> 98 </div> 99 ` 100 101 if (!details.isActive) { 102 const PlayerCountText = scrollCard.getElementsByClassName('p+pinned_games_playing')[0]; 103 PlayerCountText.children[0].classList.value = 'text-warning fa-duotone fa-lock'; 104 PlayerCountText.children[1].remove(); 105 } 106 107 card.appendChild(scrollCard); 108 } 109 card.children[0].remove(); 110 card.classList.add('d-flex'); 111 Array.from(card.children).forEach((place) => { place.classList.remove('d-none') }); 112 }); 113}; 114 115function bestFriends() { 116 const friendsRow = document.querySelector('.card:has(.friendsPopup) .d-flex')!; 117 118 const createHeadshot = async function(id: string) { 119 const user: userApiSchema = (await (await fetch('https://api.polytoria.com/v1/users/' + id)).json()); 120 const headshot = document.createElement('div'); 121 122 // ? surely a better way to do this but who cares 123 headshot.classList.add('friend-circle'); 124 headshot.setAttribute('data-user-id', id.toString()); 125 headshot.setAttribute('data-username', user.username); 126 headshot.setAttribute('data-is-online', 'false'); 127 headshot.setAttribute('data-location', 'offline'); 128 129 headshot.innerHTML = ` 130 <img width="90" height="auto" src="${user.thumbnail.icon}" alt="${user.username}" class="img-fluid rounded-circle border border-2 "> 131 <div class="friend-name text-truncate mt-1"> 132 <div style="font-size: 0.5rem; line-height: 0.5rem; display: inline-block;"> 133 <span class="text-muted"> 134 <i class="fas fa-dot-circle"></i> 135 </span> 136 </div> 137 ${user.username} 138 </div> 139 `; 140 141 friendsRow.prepend(headshot); 142 return headshot; 143 }; 144 145 _bestFriends.getValue() 146 .then(async (friends) => { 147 const userData = await pullCache( 148 'bestFriends', 149 async () => await api.batch('public', 'users/', friends), 150 300000, 151 true 152 ); 153 154 if (userData == "disabled") { 155 console.error('[Poly+] API is disabled, cancelling best friends loading..'); 156 return; 157 }; 158 159 for (const id of friends) { 160 if (!userData[id]) { 161 console.warn("[Poly+] Missing cached user data for ID " + id); 162 continue; 163 }; 164 165 let headshot = document.getElementById('friend-' + id); 166 if (!headshot) headshot = await createHeadshot(id); 167 friendsRow.prepend(headshot, friendsRow.children[0]); 168 }; 169 }); 170}; 171 172function irlBrickPrice() { 173 const trendingItems = Array.from(document.querySelectorAll('a[href^="/store"]:has(.place-card)')); 174 175 for (const item of trendingItems) { 176 const priceTag = item.getElementsByClassName('text-success')[0]; 177 const currency = bricksToCurrency(parseInt(priceTag.textContent!), "USD"); 178 179 if (currency) { 180 const spanTag = document.createElement('span'); 181 spanTag.classList.add('text-muted'); 182 spanTag.style.fontSize = '0.7rem'; 183 spanTag.style.fontWeight = 'lighter'; 184 spanTag.innerText = ` (${currency})`; 185 priceTag.appendChild(spanTag); 186 }; 187 } 188};