import { preferences, _favoritedPlaces, _bestFriends } from "@/utils/storage"; import { pullCache } from "@/utils/utilities"; import * as api from "@/utils/api"; import * as apiTypes from "@/utils/api/types"; export default defineContentScript({ matches: ['https://polytoria.com/', 'https://polytoria.com/home'], main() { preferences.getPreferences() .then((values) => { if (values.favoritedPlaces.enabled) favoritedPlaces(); if (values.bestFriends.enabled) bestFriends(); if (values.irlBrickPrice.enabled) irlBrickPrice(); }); } }); function favoritedPlaces() { _favoritedPlaces.getValue() .then(async (places) => { const container = document.createElement('div'); container.innerHTML = `
Jump right back into your favorite places
Favorited Places
Loading...
`; const card: HTMLElement = container.getElementsByClassName('scrollFadeContainer')[0] as HTMLElement; const column = document.getElementsByClassName('col-lg-8')[0]; if (document.getElementsByClassName('home-event-container')[0] === undefined) { column.insertBefore(container, column.children[0]); } else { column.insertBefore(container, column.children[1]); }; const placeData: Array | "disabled" = await pullCache( 'favoritedPlaces', async () => await api.batch('public', 'places/', places), 300000, false ); if (placeData == "disabled") { console.error('[Poly+] API is disabled, cancelling favorited places loading..'); card.innerHTML = `

Sorry! This feature is currently unavailable. Please check back later!

`; return; }; for (let i = 0; i < places.length; i++) { const id = places.toSorted((a, b) => parseInt(b) - parseInt(a))[i] const details = placeData[parseInt(id)] if (!details) { console.warn("[Poly+] Missing cached place data for ID " + id); continue; } const scrollCard = document.createElement('a') scrollCard.classList.value = 'd-none' scrollCard.href = '/places/' + id scrollCard.innerHTML = `
${details.playing} Playing
${details.name}
` if (!details.isActive) { const PlayerCountText = scrollCard.getElementsByClassName('p+pinned_games_playing')[0]; PlayerCountText.children[0].classList.value = 'text-warning fa-duotone fa-lock'; PlayerCountText.children[1].remove(); } card.appendChild(scrollCard); } card.children[0].remove(); card.classList.add('d-flex'); Array.from(card.children).forEach((place) => { place.classList.remove('d-none') }); }); }; function bestFriends() { const friendsRow = document.querySelector('.card:has(.friendsPopup) .d-flex')!; const createHeadshot = async function(id: string) { const user: apiTypes.userApiSchema = (await (await fetch('https://api.polytoria.com/v1/users/' + id)).json()); const headshot = document.createElement('div'); // ? surely a better way to do this but who cares headshot.classList.add('friend-circle'); headshot.setAttribute('data-user-id', id.toString()); headshot.setAttribute('data-username', user.username); headshot.setAttribute('data-is-online', 'false'); headshot.setAttribute('data-location', 'offline'); headshot.innerHTML = ` ${user.username}
${user.username}
`; friendsRow.prepend(headshot); return headshot; }; _bestFriends.getValue() .then(async (friends) => { const userData = await pullCache( 'bestFriends', async () => await api.batch('public', 'users/', friends), 300000, false ); if (userData == "disabled") { console.error('[Poly+] API is disabled, cancelling best friends loading..'); return; }; for (const id of friends) { if (!userData[id]) { console.warn("[Poly+] Missing cached user data for ID " + id); continue; }; let headshot = document.getElementById('friend-' + id); if (!headshot) headshot = await createHeadshot(id); friendsRow.prepend(headshot, friendsRow.children[0]); }; }); }; function irlBrickPrice() { const trendingItems = Array.from(document.querySelectorAll('a[href^="/store"]:has(.place-card)')); for (const item of trendingItems) { const priceTag = item.getElementsByClassName('text-success')[0]; const currency = bricksToCurrency(parseInt(priceTag.textContent!), "USD"); if (currency) { const spanTag = document.createElement('span'); spanTag.classList.add('text-muted'); spanTag.style.fontSize = '0.7rem'; spanTag.style.fontWeight = 'lighter'; spanTag.innerText = ` (${currency})`; priceTag.appendChild(spanTag); }; } };