From 1fd36f5a6d66e403603714b9f653fcdae33fd769 Mon Sep 17 00:00:00 2001 From: Index Date: Fri, 28 Jun 2024 10:33:24 -0500 Subject: [PATCH] feat: great divide user statistics section instead of tab --- js/background.js | 42 +++++++++++++++++++++++++------------ the-great-divide.js | 50 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/js/background.js b/js/background.js index 5f549b1..35f8883 100755 --- a/js/background.js +++ b/js/background.js @@ -101,25 +101,41 @@ chrome.runtime.onInstalled.addListener(() => { }) }); -chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) { +chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.action === 'reload') { chrome.runtime.reload(); } else if (request.action === 'greatdivide_stats') { - const Statistics = (await (await fetch('https://stats.silly.mom/player_stats?id=' + request.userID)).json()).results + chrome.storage.local.get(['PolyPlus_GreatDivideStats_' + request.userID], async function(result){ + let Statistics = result['PolyPlus_GreatDivideStats_' + request.userID] - chrome.tabs.query({ active: true, currentWindow: true }, function(tabs){ - chrome.scripting - .executeScript({ - target: {tabId: tabs[0].id}, - func: LoadStats, - args: [Statistics] - }) + // cache for 5 minutes + if (Statistics !== undefined && (new Date().getTime() - Statistics.requested < 5000)) { + Statistics = Statistics.data + } else { + Statistics = (await (await fetch('https://stats.silly.mom/player_stats?id=' + request.userID)).json()).results[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){ if (stats !== null) { - stats = stats[0] - document.getElementById('p+greatdivide_stats').innerHTML = ` + let KDR = (stats.Kills / stats.Deaths) + if (isNaN(KDR)) { + KDR = "N/A" + } else { + KDR = KDR.toFixed(4) + } + + document.getElementById('p+greatdivide_card').innerHTML = `
@@ -144,7 +160,7 @@ chrome.runtime.onMessage.addListener(async function (request, sender, sendRespon Kill Death Ratio - ${(stats.Kills / stats.Deaths).toFixed(4)} + ${KDR}
@@ -174,7 +190,7 @@ chrome.runtime.onMessage.addListener(async function (request, sender, sendRespon ${stats.FlagsCaptured} (${stats.FlagsReturned} returned)
-
+
Airdrops Collected diff --git a/the-great-divide.js b/the-great-divide.js index 31e4efb..8012e1b 100644 --- a/the-great-divide.js +++ b/the-great-divide.js @@ -62,6 +62,54 @@ async function UnbalancedServerMarkers() { } async function UserStatsTab() { + const EventSection = document.createElement('div') + EventSection.innerHTML = ` +
+
+ Great Divide +
+
+
+ +
+
+ ` + document.getElementsByClassName('user-right')[0].appendChild(EventSection) + + const EventCard = document.getElementById('p+greatdivide_card') + EventCard.innerHTML = ` + + loading... + + + ` + chrome.runtime.sendMessage({ + action: "greatdivide_stats", + userID: document.querySelector('.dropdown-item.text-danger[href^="/report"]').getAttribute('href').split('?')[0].split('/')[3] + }); + + /* + let Fetched = false + EventCard.children[0].addEventListener('click', function(){ + if (Fetched === false) { + EventCard.innerHTML = ` + + loading... + + + ` + + chrome.runtime.sendMessage({ + action: "greatdivide_stats", + userID: document.querySelector('.dropdown-item.text-danger[href^="/report"]').getAttribute('href').split('?')[0].split('/')[3] + }); + Fetched = true + } + }) + */ +} + +async function UserStatsTabOLD() { const Tabs = document.getElementById('user-info-tabs') const EventTab = document.createElement('li') @@ -100,7 +148,7 @@ async function UserStatsTab() { let SelectedTab if (tab.children[0].getAttribute('data-bs-target')) { SelectedTab = document.getElementById(tab.children[0].getAttribute('data-bs-target').substring(1)) - } else { + } else if (tab.children[0].classList.contains('fw-bold')) { SelectedTab = TabContainer } SelectedTab.classList.add('active');