From f6102eafccfc8af592015ce521361eccc8d433a6 Mon Sep 17 00:00:00 2001 From: Index Date: Sun, 4 Aug 2024 19:27:45 -0500 Subject: [PATCH] feat: quick play audio + bring back the great divide features --- js/background.js | 164 +++++++++++++++++++++++++++++++++++++ js/create/audio-library.js | 61 ++++++++++++++ js/the-great-divide.js | 96 ++++++++++++++++++++++ manifest.json | 10 +++ 4 files changed, 331 insertions(+) create mode 100644 js/create/audio-library.js create mode 100644 js/the-great-divide.js diff --git a/js/background.js b/js/background.js index 23d5b76..f8ea846 100755 --- a/js/background.js +++ b/js/background.js @@ -122,6 +122,170 @@ let RecordingTimePlayed = false chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.action === 'reload') { chrome.runtime.reload(); + } else if (request.action === 'greatdivide_stats') { + chrome.storage.local.get(['PolyPlus_GreatDivideStats_' + request.userID], async function(result){ + let Statistics = result['PolyPlus_GreatDivideStats_' + request.userID] + + // cache for 5 minutes + if (Statistics !== undefined && (new Date().getTime() - Statistics.requested < 300000)) { + Statistics = Statistics.data + } else { + Statistics = (await (await fetch('https://stats.silly.mom/player_stats?id=' + request.userID)).json()).results + if (Statistics !== null) { Statistics = Statistics[0] } + chrome.storage.local.set({['PolyPlus_GreatDivideStats_' + request.userID]: {data: Statistics, requested: new Date().getTime()}}, function(){}) + } + + chrome.tabs.query({ active: true, currentWindow: true }, function(tabs){ + chrome.scripting + .executeScript({ + target: {tabId: tabs[0].id}, + func: LoadStats, + args: [Statistics] + }) + }) + }) + + const LoadStats = function(stats){ + const GreatDivideCard = document.getElementById('p+greatdivide_card') + if (stats !== null) { + let KDR = (stats.Kills / stats.Deaths) + if (isNaN(KDR)) { + KDR = "N/A" + } else { + KDR = KDR.toFixed(4) + } + + if (stats.Team === 'phantoms') { + GreatDivideCard.parentElement.style.backgroundImage = 'linear-gradient(rgba(0.7, 0.7, 0.7, 0.7), rgba(0.7, 0.7, 0.7, 0.7)), url("https://c0.ptacdn.com/assets/N3DH4x5a6iW7raaQ-3lwHpRHHpWShdXc.png")'; + GreatDivideCard.parentElement.style.borderColor = ''; + GreatDivideCard.parentElement.style.border = '1.25px solid blue !important'; + } else { + GreatDivideCard.parentElement.style.backgroundImage = 'linear-gradient(rgba(0.7, 0.7, 0.7, 0.7), rgba(0.7, 0.7, 0.7, 0.7)), url("https://c0.ptacdn.com/assets/1HXpaoDLHJo2rrvwwxqJEDWvDZ6BgvSE.png")'; + GreatDivideCard.parentElement.style.borderColor = ''; + GreatDivideCard.parentElement.style.border = '1.25px solid green !important'; + } + + GreatDivideCard.innerHTML = ` +
+ + + Last Round Seen + + + ${ (stats.LastRoundSeen === 0) ? '-' : stats.LastRoundSeen } + +
+
+
+ + + Kills + + + ${stats.Kills.toLocaleString()} (${stats.UniqueKills.toLocaleString()} unique) + +
+
+ + + Deaths + + + ${stats.Deaths.toLocaleString()} + +
+
+ + + Kill Death Ratio + + + ${KDR} + +
+
+ + + Points Scored + + + ${stats.PointsScored.toLocaleString()} + +
+
+ + + Cash Earned + + + ${stats.CashEarned.toLocaleString()} + +
+
+ + + Flags Captured + + + ${stats.FlagsCaptured} (${stats.FlagsReturned} returned) + +
+
+ + + Airdrops Collected + + + ${stats.AirdropsCollected} + +
+
+
+ + + Monoliths Destroyed + + + ${stats.ObelisksDestroyed} + +
+
+ + + Blocks Placed + + + ${stats.BlocksPlaced} (${stats.BlocksDestroyed} destroyed) + +
+
+ + + Headshots + + + ${stats.Headshots} + +
+ ` + + const Script = document.createElement('script'); + Script.setAttribute('type', 'text/javascript'); + Script.setAttribute('src', chrome.runtime.getURL('resources/registerTooltips.js')); + Script.addEventListener('load', function () { + Script.remove(); + }); + document.body.appendChild(Script); + } else { + GreatDivideCard.classList.add('text-center', 'py-5') + GreatDivideCard.innerHTML = ` +

+
Not Drafted
+

This user didn't participate in The Great Divide.

+ ` + } + } + return true } else if (request.action === 'start_time_played') { if (RecordingTimePlayed === true) { console.log('Time Played: Already Started Interval') diff --git a/js/create/audio-library.js b/js/create/audio-library.js new file mode 100644 index 0000000..d0cb03c --- /dev/null +++ b/js/create/audio-library.js @@ -0,0 +1,61 @@ +const AssetGrid = document.getElementById('assets') + +const AssetObserver = new MutationObserver(async function (list) { + const SelectedTab = document.getElementsByClassName('nav-link active')[0].getAttribute('data-category') + if (SelectedTab === "audio") { + for (const record of list) { + for (const audio of record.addedNodes) { + if (audio.tagName === 'DIV') { + const PlayButton = document.createElement('button') + PlayButton.classList = 'btn btn-primary btn-sm' + PlayButton.style = 'position: absolute; bottom: 0; right: 0; margin: 5px; margin-bottom: 55px; z-index: 2000;' + PlayButton.innerHTML = '' + audio.getElementsByTagName('a')[0].parentElement.insertBefore(PlayButton, audio.getElementsByTagName('a')[0]) + audio.children[0].style.position = 'relative' + + let AudioElement = null + let Playing = false + PlayButton.addEventListener('click', async function(){ + if (!Playing) { + if (AudioElement === null) { + PlayButton.innerHTML = '
Loading...
' + const AudioURL = (await (await fetch('https://api.polytoria.com/v1/assets/serve-audio/' + audio.querySelector('a[href^="/store"]').getAttribute('href').split('/')[2])).json()) + if (AudioURL.success) { + AudioElement = new Audio(AudioURL.url) + } else { + PlayButton.remove(); + } + + AudioElement.addEventListener("canplaythrough", (event) => { + Playing = true + AudioElement.play(); + PlayButton.innerHTML = '' + PlayButton.classList = 'btn btn-warning btn-sm' + }); + } else { + Playing = true + AudioElement.play(); + PlayButton.innerHTML = '' + PlayButton.classList = 'btn btn-warning btn-sm' + } + + AudioElement.addEventListener("ended", function() { + Playing = false + PlayButton.innerHTML = '' + PlayButton.classList = 'btn btn-primary btn-sm' + }) + } else { + Playing = false + AudioElement.pause() + PlayButton.innerHTML = '' + PlayButton.classList = 'btn btn-primary btn-sm' + } + }) + } + } + AssetObserver.observe(AssetGrid, {attributes: false, childList: true, subtree: false}); + } + } +}); + +AssetObserver.observe(AssetGrid, {attributes: false, childList: true, subtree: false}); \ No newline at end of file diff --git a/js/the-great-divide.js b/js/the-great-divide.js new file mode 100644 index 0000000..b34629d --- /dev/null +++ b/js/the-great-divide.js @@ -0,0 +1,96 @@ +let EventOngoing = true; +let Team; +let HasTeam = true; + +const PlaceAllowlist = [ + '9656', + '9757' +]; + +(async () => { + Utilities = await import(chrome.runtime.getURL('resources/utils.js')) + .default + + chrome.storage.sync.get(['PolyPlus_Settings'], function(result) { + Settings = result.PolyPlus_Settings || {}; + + if (Settings.TheGreatDivide.Enabled !== true) { + //return + } + + if (Settings.TheGreatDivide.UserStatsOn === true && window.location.pathname.split('/')[1] === 'u') { + if (HasTeam === true) { + UserStatsTab() + } else { + if (EventOngoing === true) { + UserStatsTab() + } + } + } + }) + + async function UnbalancedServerMarkers() { + const Team = (await (await fetch('https://api.polytoria.com/v1/users/' + JSON.parse(window.localStorage.getItem('p+account_info')).ID + '/greatdivide')).json()).team + if (Team !== undefined) { + const Servers = Array.from(document.getElementById('servers-tabpane').children) + + Servers.forEach(server => { + const TeamCounts = { + phantoms: server.getElementsByClassName('border-phantoms').length, + cobras: server.getElementsByClassName('border-cobras').length + } + + let Enemy = "cobras" + if (Team === "cobras") { Enemy = "phantoms" } + + if (new URLSearchParams(window.location.search).has('forceServerUnbalance')) { + TeamCounts[Enemy] = 1000 + } + + if (TeamCounts[Team] < TeamCounts[Enemy]) { + const UnbalancedText = document.createElement('p') + UnbalancedText.classList = 'mb-2' + UnbalancedText.style.fontSize = '0.7rem' + UnbalancedText.style.color = 'orange' + UnbalancedText.innerHTML = `*Potentially Unbalanced ` + + const ServerInfoColumn = server.getElementsByClassName('col-3')[0] + ServerInfoColumn.children[0].style.marginBottom = '0px' + ServerInfoColumn.insertBefore(UnbalancedText, ServerInfoColumn.children[1]) + + Utilities.InjectResource("registerTooltips") + } + }) + } + } + + async function UserStatsTab() { + const EventSection = document.createElement('div') + EventSection.innerHTML = ` +
+
+ + GREATEST DIVISION + +
+
+
+ +
+
+ ` + document.getElementsByClassName('user-right')[0].appendChild(EventSection) + + const EventCard = document.getElementById('p+greatdivide_card') + EventCard.innerHTML = ` + + Loading... + + + ` + await chrome.runtime.sendMessage({ + action: "greatdivide_stats", + userID: document.querySelector('.dropdown-item.text-danger[href^="/report"]').getAttribute('href').split('?')[0].split('/')[3] + }); + } +})(); \ No newline at end of file diff --git a/manifest.json b/manifest.json index cb4d248..22bb154 100644 --- a/manifest.json +++ b/manifest.json @@ -121,6 +121,16 @@ { "matches": ["https://polytoria.com/games/*", "https://polytoria.com/shop/*", "https://polytoria.com/my/referrals", "https://polytoria.com/create/?t=*", "https://polytoria.com/user/*", "https://polytoria.com/library/*"], "js": ["/js/site-redirects.js"] + }, + + { + "matches": ["https://polytoria.com/library"], + "js": ["/js/create/audio-library.js"] + }, + + { + "matches": ["https://polytoria.com/u/**"], + "js": ["/js/the-great-divide.js"] } ], "background": {