Skip to content

Instantly share code, notes, and snippets.

@tyhallcsu
Last active June 18, 2024 01:02
Show Gist options
  • Save tyhallcsu/07b4fa997729a9e9220aee22b5254b2e to your computer and use it in GitHub Desktop.
Save tyhallcsu/07b4fa997729a9e9220aee22b5254b2e to your computer and use it in GitHub Desktop.
Detects and counts the number of times a specific image appears on a page, and maintains the count across page reloads
// ==UserScript==
// @name Pokemon Gods Egg Counter
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Detects and counts the number of times a specific image appears on a page, and maintains the count across page reloads
// @author sharmanhall
// @match https://pokemongods.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to get the current count from localStorage
function getCount() {
return parseInt(localStorage.getItem('eggCount') || '0', 10);
}
// Function to update the count in localStorage
function updateCount(newCount) {
localStorage.setItem('eggCount', newCount);
}
// Function to create a counter element on the page
function createCounterElement() {
const counterDiv = document.createElement('div');
counterDiv.id = 'eggCounter';
counterDiv.style.position = 'fixed';
counterDiv.style.top = '10px';
counterDiv.style.right = '10px';
counterDiv.style.padding = '10px';
counterDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
counterDiv.style.color = '#fff';
counterDiv.style.fontSize = '16px';
counterDiv.style.zIndex = '99999';
document.body.appendChild(counterDiv);
updateCounterElement(getCount());
}
// Function to update the displayed counter element
function updateCounterElement(count) {
const counterDiv = document.getElementById('eggCounter');
counterDiv.textContent = `Eggs Collected: ${count}`;
}
// Function to detect the image and increment the counter
function detectEggImage() {
const imgSrc = 'https://pokemongods.com/images/pokemonEgg.png';
const images = document.querySelectorAll(`img[src="${imgSrc}"]`);
if (images.length > 0) {
const currentCount = getCount();
const newCount = currentCount + images.length;
updateCount(newCount);
updateCounterElement(newCount);
}
}
// Initialize the counter element on page load
createCounterElement();
// Set an interval to check for the image every 2 seconds
setInterval(detectEggImage, 2000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment