Skip to content

Instantly share code, notes, and snippets.

@carcigenicate
Last active May 21, 2023 22:57
Show Gist options
  • Save carcigenicate/b99a82d85c534679a972cffb6ad4bc71 to your computer and use it in GitHub Desktop.
Save carcigenicate/b99a82d85c534679a972cffb6ad4bc71 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Seed Counter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Counts seeds
// @author carcigenicate
// @match https://www.zeldadungeon.net/breath-of-the-wild-interactive-map?*
// @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
let completedCountBox;
function getCompletedSeedsFromLocalStorage() {
const rawLocal = localStorage.getItem('maps-botw-completion');
const parsedLocal = JSON.parse(rawLocal);
return Object.keys(parsedLocal).filter((key) => key.startsWith('Seed') && parsedLocal[key] === true);
}
async function getCompletedSeedsFromApi() {
const response = await fetch('https://www.zeldadungeon.net/wiki/api.php?format=json&action=query&list=map_userdata&zdm_map=botw');
const parsedResonse = await response.json();
const rawCompleted = parsedResonse.query?.map_userdata?.completed;
if (rawCompleted) {
const rawSplit = rawCompleted.split(',');
return rawSplit.filter((item) => item.startsWith('Seed'));
} else {
console.error("Could not get completed seed count");
}
}
async function getUserCompletedSeedCounts() {
const local = localStorage.getItem('maps-botw-completion');
let completedSeeds;
if (local === null || local === '{}') {
// User is logged in
completedSeeds = await getCompletedSeedsFromApi();
} else {
// User is logged out
completedSeeds = await getCompletedSeedsFromLocalStorage();
}
return completedSeeds.length;
}
async function populateCountBox() {
const count = await getUserCompletedSeedCounts();
completedCountBox.innerText = `Completed Seeds: ${count}`;
}
window.addEventListener('load', () => {
completedCountBox = document.createElement('div');
Object.assign(completedCountBox.style, {
fontSize: '1.25rem',
color: 'white',
backgroundColor: 'black',
padding: '0.25rem',
});
const refreshButton = document.createElement('button');
refreshButton.innerText = 'Refresh Count';
refreshButton.onclick = () => populateCountBox();
const parent = document.createElement('div');
Object.assign(parent.style, {
position: 'absolute',
top: '33vh',
left: '0.5rem',
display: 'flex',
gap: '1rem',
zIndex: 99999,
});
parent.appendChild(completedCountBox);
parent.appendChild(refreshButton);
document.body.appendChild(parent);
populateCountBox();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment