import { preferences, _favoritedPlaces, _bestFriends } from "@/utils/storage";
import { placeApiSchema, userApiSchema } from "@/utils/types";
import { pullCache } from "@/utils/utilities";
import api from "@/utils/api";
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();
});
}
});
function favoritedPlaces() {
_favoritedPlaces.getValue()
.then(async (places) => {
const container = document.createElement('div');
container.innerHTML = `
Jump right back into your favorite places
Favorited Places
`;
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 = await pullCache(
'favoritedPlaces',
async () => await api.places.batch!(places as string[]),
300000,
false
);
const card = container.getElementsByClassName('scrollFadeContainer')[0]
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 = `
`
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: 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 = `
`;
friendsRow.prepend(headshot);
return headshot;
};
_bestFriends.getValue()
.then(async (friends) => {
const userData = await pullCache(
'bestFriends',
async () => await api.users.batch!(friends as string[]),
300000,
false
);
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]);
};
});
}