feat: "pinned games" rewrite

This commit is contained in:
Index 2024-07-15 08:53:47 -05:00
parent 90dab88d0e
commit 0eca6b62c7
2 changed files with 173 additions and 312 deletions

243
js/account/home.js Executable file → Normal file
View file

@ -1,194 +1,81 @@
/*
this file needs a rewrite by me lol
*/
var Settings;
var PinnedGamesData;
var BestFriendsData;
let Utilities;
(async () => {
Utilities = await import(chrome.runtime.getURL('resources/utils.js'));
Utilities = Utilities.default;
chrome.storage.sync.get(['PolyPlus_Settings'], async function (result) {
Settings = Utilities.MergeObjects(result.PolyPlus_Settings || Utilities.DefaultSettings);
if (Settings.IRLPriceWithCurrency && Settings.IRLPriceWithCurrency.Enabled === true) {
IRLPrice();
chrome.storage.sync.get(['PolyPlus_Settings', 'PolyPlus_PinnedGames'], async function(result){
Settings = result.PolyPlus_Settings || {
PinnedGamesOn: true
}
if (Settings.HomeFriendCountOn === true) {
ShowFriendCount();
if (Settings.PinnedGamesOn === true) {
const PlaceIDs = result.PolyPlus_PinnedGames || [];
const PinnedGamesContainer = document.createElement('div')
PinnedGamesContainer.innerHTML = `
<div class="row reqFadeAnim px-2 px-lg-0">
<div class="col">
<h6 class="dash-ctitle2">Jump right back into your favorite games</h6>
<h5 class="dash-ctitle">Pinned Games</h5>
</div>
</div>
<div class="card card-dash mcard mb-3">
<div class="card-body p-0 m-1 scrollFadeContainer" id="p+pinned_games_card">
<div class="text-center p-5">
<div class="spinner-border text-muted" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
`
const RightSideColumn = document.getElementsByClassName('col-lg-8')[0];
if (document.getElementsByClassName('home-event-container')[0] === undefined) {
RightSideColumn.insertBefore(PinnedGamesContainer, RightSideColumn.children[0]);
} else {
RightSideColumn.insertBefore(PinnedGamesContainer, RightSideColumn.children[1]);
}
if (Settings.PinnedGamesOn === true || Settings.BestFriendsOn === true) {
Update();
}
});
})();
const PinnedGamesCard = document.getElementById('p+pinned_games_card')
for (let i = 0; i < PlaceIDs.length; i++) {
const PlaceID = PlaceIDs.toSorted((a, b) => b - a)[i]
const PlaceDetails = (await (await fetch('https://api.polytoria.com/v1/places/' + PlaceID)).json())
let ContainerElement = `
<div class="card-body p-0 m-1 scrollFadeContainer d-flex"></div>`;
let GameContainerElement = `
<div class="scrollFade card me-2 place-card force-desktop text-center mb-2" style="opacity: 1;">
const PlaceCard = document.createElement('a')
PlaceCard.classList = 'd-none'
PlaceCard.href = '/places/' + PlaceID
PlaceCard.innerHTML = `
<div class="scrollFade card me-2 place-card force-desktop text-center mb-2" style="opacity: 1;">
<div class="card-body">
<div class="ratings-header">
<img src=":Thumbnail" class="place-card-image" style="position: relative;">
<div style="position: absolute;background: linear-gradient(to bottom, black, transparent, transparent, transparent);width: 100%;height: 100%;top: 0;left: 0;border-radius: 10px;padding-top: 5px;color: gray;font-size: 0.8rem;">
<img src="${PlaceDetails.thumbnail}" class="place-card-image" style="position: relative;">
<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;">
<i class="fa-duotone fa-users"></i>
<span>
<i id="thumbup-icn" class="thumb-icon far fa-thumbs-up"></i>
:Likes
</span>
|
<span>
<i id="thumbdown-icn" class="thumb-icon far fa-thumbs-down"></i>
:Dislikes
${PlaceDetails.playing}
Playing
</span>
</div>
</div>
<div>
<div class="mt-2 mb-1 place-card-title">
:GameName
${PlaceDetails.name}
</div>
</div>
</div>
</div>
`;
let TitleElement = `
<div class="col">
<h6 class="dash-ctitle2">Jump right back into your favorite games</h6>
<h5 class="dash-ctitle">Pinned Games</h5>
</div>`;
var FriendContainer = document.querySelector('.card:has(.friendsPopup) .card-body');
</div>
`
let NewContainer = document.createElement('div');
NewContainer.style.display = 'none';
NewContainer.classList = 'card card-dash mcard mb-3';
NewContainer.style.animationDelay = '0.18s';
NewContainer.innerHTML = ContainerElement;
let NewTitle = document.createElement('div');
NewTitle.style.display = 'none';
NewTitle.classList = 'row reqFadeAnim px-2 px-lg-0';
NewTitle.innerHTML = TitleElement;
let BestFriendsContainer = document.createElement('div');
BestFriendsContainer.classList = 'd-flex';
BestFriendsContainer.style = 'display: none; border-bottom: 1px solid #000; padding-bottom: 10px; margin-bottom: 10px; width: 100%;';
let Spacer = document.createElement('div');
Spacer.innerHTML = ' ';
Spacer.style.width = '50px';
Spacer.prepend(BestFriendsContainer);
FriendContainer.prepend(BestFriendsContainer);
async function Update() {
chrome.storage.sync.get(['PolyPlus_PinnedGames'], function (result) {
PinnedGamesData = result.PolyPlus_PinnedGames.toSorted((a, b) => b - a) || [];
if (Settings.PinnedGamesOn === true) {
PinnedGames();
} else {
NewContainer.style.display = 'none';
NewTitle.style.display = 'none';
}
});
chrome.storage.sync.get(['PolyPlus_BestFriends'], function (result) {
BestFriendsData = result.PolyPlus_BestFriends || [];
if (Settings.BestFriendsOn === true) {
BestFriends();
} else {
BestFriendsContainer.style.display = 'none';
Spacer.style.display = 'none';
}
});
}
function PinnedGames() {
var Existing = NewContainer.children[0].children;
Array.from(Existing).forEach((element) => {
element.remove();
});
if (PinnedGamesData.length === 0) {
NewContainer.style.display = 'none';
NewTitle.style.display = 'none';
} else {
NewContainer.style.display = '';
NewTitle.style.display = '';
if (!PlaceDetails.isActive) {
const PlayerCountText = PlaceCard.getElementsByClassName('p+pinned_games_playing')[0]
PlayerCountText.children[0].classList = 'text-warning fa-duotone fa-lock'
PlayerCountText.children[1].remove()
}
for (let element of PinnedGamesData) {
fetch('https://api.polytoria.com/v1/places/' + element)
.then((response) => response.json())
.then((data) => {
let GameName = data.name;
let GameThumbnail = data.thumbnail;
var NewGameContainer = document.createElement('a');
NewGameContainer.innerHTML = GameContainerElement.replace(':GameName', GameName)
.replace(':Thumbnail', GameThumbnail)
.replace(':Likes', data.rating.likes)
.replace(':Dislikes', data.rating.dislikes);
NewGameContainer.href = '/places/' + element;
/*
if (new Date().getDate() >= new Date(data.updatedAt).getDate()) {
console.log('Game has updated')
PinnedGamesCard.appendChild(PlaceCard)
}
*/
NewContainer.children[0].appendChild(NewGameContainer);
})
.catch((error) => {
console.error('Error:', error);
});
}
}
function BestFriends() {
return
Array.from(document.querySelectorAll('[bestFriend]')).forEach((element) => {
element.removeAttribute('bestFriend');
element.getElementsByClassName('friend-name')[0].style.color = 'initial';
FriendContainer.appendChild(element);
});
if (BestFriendsData.length === 0) {
BestFriendsContainer.style.visibility = 'hidden';
BestFriendsContainer.style.padding = '0px !important';
BestFriendsContainer.style.margin = '0px !important';
} else {
BestFriendsContainer.style.visibility = 'visible';
BestFriendsContainer.style.padding = '';
BestFriendsContainer.style.margin = '';
PinnedGamesCard.children[0].remove()
PinnedGamesCard.classList.add('d-flex')
Array.from(PinnedGamesCard.children).forEach((place) => {place.classList.remove('d-none')})
}
BestFriendsData.forEach((element) => {
let ExistingFriend = document.getElementById('friend-' + element);
if (ExistingFriend) {
ExistingFriend.setAttribute('bestFriend', 'true');
ExistingFriend.getElementsByClassName('friend-name')[0].style.color = 'yellow';
BestFriendsContainer.prepend(ExistingFriend);
}
});
}
var SecondaryColumn = document.getElementsByClassName('col-lg-8')[0];
if (document.getElementsByClassName('home-event-container')[0] === undefined) {
SecondaryColumn.insertBefore(NewContainer, SecondaryColumn.children[0]);
SecondaryColumn.insertBefore(NewTitle, SecondaryColumn.children[0]);
} else {
SecondaryColumn.insertBefore(NewContainer, SecondaryColumn.children[1]);
SecondaryColumn.insertBefore(NewTitle, SecondaryColumn.children[1]);
}
async function IRLPrice() {
if (Settings.IRLPriceWithCurrency && Settings.IRLPriceWithCurrency.Enabled === true) {
(async () => {
Utilities = await import(chrome.runtime.getURL('resources/utils.js'));
Utilities = Utilities.default;
@ -207,23 +94,15 @@ async function IRLPrice() {
}
}
})();
}
async function ShowFriendCount() {
let FriendCount = (await (await fetch('https://polytoria.com/api/friends?page=1')).json()).meta.total;
/*
const FirstPage = (await (await fetch('https://polytoria.com/api/friends?page=1')).json())
if (FirstPage.meta.lastPage > 1) {
const LastPage = (await (await fetch('https://polytoria.com/api/friends?page=' + Pages)).json())
FriendCount = (12*(FirstPage.meta.pages-1)) + LastPage.data.length
} else {
FriendCount = FirstPage.data.length
}
*/
if (Settings.HomeFriendCountOn === true) {
const FriendCount = (await (await fetch('https://polytoria.com/api/friends?page=1')).json()).meta.total;
const CountText = document.createElement('small');
CountText.classList = 'text-muted fw-lighter';
CountText.style.fontSize = '0.8rem';
CountText.innerText = ' (' + FriendCount + ')';
document.querySelector('#home-friendsOnline h5').appendChild(CountText);
}
}
})

View file

@ -1,4 +1,4 @@
const PlaceID = window.location.pathname.split('/')[2];
const PlaceID = parseInt(window.location.pathname.split('/')[2]);
const UserID = JSON.parse(window.localStorage.getItem('p+account_info')).ID;
const GameCreator = document.querySelector('#main-content .card-body .col div.text-muted a[href^="/users/"]').getAttribute('href').split('/')[2];
@ -46,7 +46,7 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [
RatingsContainer.children[0].appendChild(PercentageLabel);
chrome.storage.sync.get(['PolyPlus_Settings', 'PolyPlus_TimePlayed'], async function (result) {
chrome.storage.sync.get(['PolyPlus_Settings', 'PolyPlus_PinnedGames', 'PolyPlus_TimePlayed'], async function (result) {
Settings = result.PolyPlus_Settings || {};
TimePlayed = result.PolyPlus_TimePlayed || {};
@ -54,7 +54,7 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [
Utilities = Utilities.default;
if (Settings.PinnedGamesOn === true) {
PinnedGames();
PinnedGames(result.PolyPlus_PinnedGames);
}
if (Settings.InlineEditingOn === true && GameCreator === UserID) {
@ -127,7 +127,6 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [
}
if (Settings.ImprovedAchievements.OpacityOn && Settings.ImprovedAchievements.OpacityOn === true) {
console.log(Settings)
for (let achievement of Achievements) {
if ((achievement.getElementsByClassName('fad fa-check-circle')[0] !== undefined) === false) {
achievement.style.opacity = '0.5';
@ -142,94 +141,76 @@ const Gamepasses = Array.from(GamepassesTab.getElementsByClassName('card')) || [
});
})();
async function PinnedGames() {
chrome.storage.sync.get(['PolyPlus_PinnedGames'], function (result) {
PinnedGamesData = result.PolyPlus_PinnedGames || [];
function PinnedGames(placeIDs) {
const PinButton = document.createElement('button')
PinButton.classList = 'btn btn-primary btn-sm'
PinButton.style = 'position: absolute; top: 0; right: 0; margin: 4px; font-size: 1.3em;'
PinButton.innerHTML = `
<i class="fa-regular fa-star"></i>
`
const PinBtn = document.createElement('button');
PinBtn.classList = 'btn btn-warning btn-sm';
PinBtn.style = 'position: absolute; right: 0; margin-right: 7px;';
if (PinnedGamesData.includes(parseInt(PlaceID))) {
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Un-pin';
const UpdatePinButtonState = function(){
PinButton.classList = 'btn btn-primary btn-sm'
if (placeIDs.indexOf(PlaceID) === -1) {
// Not Pinned
if (placeIDs.length >= Utilities.Limits.PinnedGames) {
PinButton.disabled = true
}
PinButton.children[0].classList = 'fa-regular fa-star'
} else {
if (PinnedGamesData.length !== Utilities.Limits.PinnedGames) {
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin';
} else {
PinBtn.setAttribute('disabled', true);
PinBtn.innerHTML = `<i class="fa-duotone fa-star"></i> Pin (max ${Utilities.Limits.PinnedGames}/${Utilities.Limits.PinnedGames})`;
// Pinned
PinButton.children[0].classList = 'fa-duotone fa-star'
}
}
PinBtn.addEventListener('click', function () {
PinBtn.setAttribute('disabled', 'true');
chrome.storage.sync.get(['PolyPlus_PinnedGames'], function (result) {
PinnedGamesData = result.PolyPlus_PinnedGames || [];
/*
const Index = PinnedGames.indexOf(parseInt(PlaceID))
if (Index !== -1) {
//delete PinnedGames[PlaceID]
PinnedGames.splice(Index, 1)
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin'
} else {
//PinnedGames[PlaceID] = {lastVisited: new Date()}
PinnedGames.push(parseInt(PlaceID))
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Un-pin'
PinButton.addEventListener('mouseenter', function(){
if (placeIDs.indexOf(PlaceID) !== -1) {
PinButton.classList.add('btn-danger')
PinButton.classList.remove('btn-primary')
PinButton.children[0].classList.add('fa-star-half-stroke')
PinButton.children[0].classList.remove('fa-star')
}
*/
const Index = PinnedGamesData.indexOf(parseInt(PlaceID));
if (Index !== -1) {
PinnedGamesData.splice(Index, 1);
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin';
} else {
PinnedGamesData.push(parseInt(PlaceID));
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Un-pin';
})
PinButton.addEventListener('mouseleave', function(){
if (placeIDs.indexOf(PlaceID) !== -1) {
PinButton.classList.add('btn-primary')
PinButton.classList.remove('btn-danger')
PinButton.children[0].classList.add('fa-star')
PinButton.children[0].classList.remove('fa-star-half-stroke')
}
})
UpdatePinButtonState()
document.querySelector('h1.my-0').parentElement.appendChild(PinButton)
PinButton.addEventListener('click', function(){
if (placeIDs.length >= Utilities.Limits.PinnedGames) {
PinButton.disabled = true
return
}
chrome.storage.sync.set({PolyPlus_PinnedGames: PinnedGamesData, arrayOrder: true}, function () {
setTimeout(function () {
PinBtn.removeAttribute('disabled');
console.log(PinnedGamesData);
}, 1250);
});
});
if (placeIDs.indexOf(PlaceID) === -1) {
placeIDs.push(PlaceID)
} else {
placeIDs.splice(placeIDs.indexOf(PlaceID), 1)
}
PinButton.disabled = true
UpdatePinButtonState()
chrome.storage.sync.set({'PolyPlus_PinnedGames': placeIDs}, function(){
setTimeout(() => {
PinButton.disabled = false
}, 750);
})
});
document.getElementsByClassName('card-header')[2].appendChild(PinBtn);
chrome.storage.onChanged.addListener(function (changes, namespace) {
chrome.storage.onChanged.addListener(function (changes) {
if ('PolyPlus_PinnedGames' in changes) {
chrome.storage.sync.get(['PolyPlus_PinnedGames'], function (result) {
PinnedGamesData = result.PolyPlus_PinnedGames || [];
/*
if (PinnedGamesData[PlaceID]) {
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Un-pin'
} else {
if (PinnedGamesData.length !== 5) {
PinBtn.removeAttribute('disabled')
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin'
} else {
PinBtn.setAttribute('disabled', true)
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin (max 5/5)'
placeIDs = changes.PolyPlus_PinnedGames.newValue
UpdatePinButtonState()
}
}
*/
if (PinnedGamesData.includes(parseInt(PlaceID))) {
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Un-pin';
} else {
if (PinnedGamesData.length !== Utilities.Limits.PinnedGames) {
PinBtn.removeAttribute('disabled');
PinBtn.innerHTML = '<i class="fa-duotone fa-star"></i> Pin';
} else {
PinBtn.setAttribute('disabled', true);
PinBtn.innerHTML = `<i class="fa-duotone fa-star"></i> Pin (max ${Utilities.Limits.PinnedGames}/${Utilities.Limits.PinnedGames})`;
}
}
});
}
});
});
}
@ -547,6 +528,7 @@ async function ReaddCopyable() {
}
if (PlaceDetails.isCopyable) {
console.log('is copyable')
const TitleCardButtons = document.querySelector('.card:has(h1.my-0) .col-auto[style^="m"]')
const DownloadCopyButton = document.createElement('button')