Code:
// ==UserScript==
// @name Pound Highlighter
// @version 0.1
// @description Highlights content based on text box entries. Color and species is a comma separated list. Stats can be empty to ignore the stats. The name is a regex matcher. ^[a-zA-Z]{1,10}$ looks for names with only characters that is up the the length of 10. ^[A-Za-z]+$ highlight names with only characters
// @author Onetwo
// @match https://www.neopets.com/pound/adopt.phtml
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
console.log("Script is running...");
// Hide the element selected by the query
const elementToHide = document.querySelector("#content > table > tbody > tr > td > div:nth-child(2) > p");
if (elementToHide) {
elementToHide.style.display = "none";
}
// Function to load saved custom values or initialize default values
function loadCustomValues() {
const customValues = JSON.parse(localStorage.getItem('customValues')) || {};
return customValues;
}
function saveCustomValues(customValues) {
const regexPattern = document.getElementById('custom_regex').value;
customValues['regexPattern'] = regexPattern; // Add regex pattern to custom values
localStorage.setItem('customValues', JSON.stringify(customValues));
}
// Function to initialize or update text boxes with custom values
function initializeTextBoxes() {
console.log("Initializing text boxes...");
const customValues = loadCustomValues();
const attributes = ['Colour', 'Species', 'Level', 'Strength', 'Defence', 'Movement'];
const regexPattern = customValues['regexPattern'] || ''; // Get regex pattern from custom values
// Add a line break before listing the text boxes
const parentElement = document.getElementById('page_header');
// Create a div before "Name Pattern"
const divBeforeNamePattern = document.createElement('div');
parentElement.appendChild(divBeforeNamePattern);
// Create input box for regex pattern
const regexLabel = document.createElement('label');
regexLabel.innerText = 'Name Regex: ';
const regexInput = document.createElement('input');
regexInput.setAttribute('type', 'text');
regexInput.setAttribute('id', 'custom_regex');
regexInput.setAttribute('placeholder', 'Enter Name pattern');
regexInput.setAttribute('value', '^[A-Za-z]+$');
regexInput.value = regexPattern;
divBeforeNamePattern.appendChild(regexLabel);
divBeforeNamePattern.appendChild(regexInput);
parentElement.appendChild(document.createElement('br')); // Add line break
// Create a container for the input boxes
const container = document.createElement('div');
container.style.display = 'flex';
container.style.flexWrap = 'wrap';
container.style.alignItems = 'flex-start'; // Align items at the start of the container
parentElement.appendChild(container);
attributes.forEach(attribute => {
const inputId = `custom_${attribute.toLowerCase()}`;
const inputValue = customValues[inputId] || '';
// Create a div for each input box
const inputDiv = document.createElement('div');
inputDiv.style.flexBasis = 'calc(50% - 10px)';
inputDiv.style.boxSizing = 'border-box';
inputDiv.style.marginRight = '10px';
const labelElement = document.createElement('label');
labelElement.style.width = "80px"; // You can adjust the width as needed
labelElement.style.display = "inline-block"; // Ensures the width property works correctly
labelElement.innerText = `${attribute}: `;
const inputElement = document.createElement('input');
inputElement.setAttribute('type', 'text');
inputElement.setAttribute('id', inputId);
inputElement.setAttribute('placeholder', `Enter ${attribute}`);
inputElement.value = inputValue;
inputDiv.appendChild(labelElement);
inputDiv.appendChild(inputElement);
container.appendChild(inputDiv);
});
}
// Function to retrieve text box values
function getTextValues() {
const customValues = {};
const attributes = ['Colour', 'Species', 'Level', 'Strength', 'Defence', 'Movement'];
attributes.forEach(attribute => {
const inputId = `custom_${attribute.toLowerCase()}`;
const inputValue = document.getElementById(inputId).value;
customValues[inputId] = inputValue;
});
return customValues;
}
// Function to check if a value exists in a comma-delimited string
function checkValueInList(customValue, value) {
// Split the comma-delimited string into an array of values
const values = customValue.split(',');
// Check if the given value exists in the array
return values.includes(value.trim());
}
// Function to highlight pet names based on regex pattern
function highlightPetNames() {
const petNameSelectors = ['#pet0_name', '#pet1_name', '#pet2_name'];
const regexPattern = document.getElementById('custom_regex').value; // Get regex pattern from input box
const regex = new RegExp(regexPattern, 'i'); // Create regex object with case-insensitive flag
petNameSelectors.forEach(selector => {
const petNameElement = document.querySelector(selector);
if (petNameElement) {
const petName = petNameElement.innerText.trim();
if (regex.test(petName)) {
petNameElement.style.backgroundColor = 'yellow';
}
}
else {
//console.log(`${selector} not found. Skipping highlighting process.`);
}
});
}
// Highlight pet attributes other than name
function highlightPetAttributes() {
const selectors = ['#pet0_table', '#pet1_table', '#pet2_table'];
const customValues = {};
// Retrieve values from text boxes
const attributes = ['Colour', 'Species', 'Level', 'Strength', 'Defence', 'Movement'];
attributes.forEach(attribute => {
const inputId = `custom_${attribute.toLowerCase()}`;
customValues[attribute.toLowerCase()] = document.getElementById(inputId).value;
});
//console.log('Custom values:', customValues);
selectors.forEach(selector => {
const table = document.querySelector(selector);
if (table) {
const rows = table.querySelectorAll('tr');
rows.forEach(row => {
const cells = row.querySelectorAll('td');
if (cells.length === 2) {
const attribute = cells[0].innerText.trim().toLowerCase().replace(':', '').replace(/\s/g, '');
const value = cells[1].querySelector('span').innerText.trim().toLowerCase().replace(/\s/g, '');
const customAttribute = customValues[attribute.toLowerCase().replace(/\s/g, '')];
if (attribute === 'species' || attribute === 'colour'){
if ( checkValueInList(customValues[attribute],value)) {
cells[1].style.backgroundColor = 'yellow';
}
}
else if (parseInt(value) >= parseInt(customValues[attribute] )) {
cells[1].style.backgroundColor = 'lightblue';
}
}
});
}
else {
//console.log(`${selector} not found. Skipping highlighting process.`);
}
});
}
// Function to remove all highlights
function clearHighlights() {
const highlightedElements = document.querySelectorAll('[style*=background-color]');
highlightedElements.forEach(element => {
element.style.backgroundColor = ''; // Remove background color
});
}
// Function to save custom values when the page is unloaded
window.addEventListener('beforeunload', () => {
const customValues = {};
const attributes = ['Colour', 'Species', 'Level', 'Strength', 'Defence', 'Movement'];
attributes.forEach(attribute => {
const inputId = `custom_${attribute.toLowerCase()}`;
const inputValue = document.getElementById(inputId).value;
customValues[inputId] = inputValue;
});
saveCustomValues(customValues);
});
// Function to periodically check for updates and re-highlight attributes and names
function checkForUpdates() {
setInterval(() => {
clearHighlights();
highlightPetAttributes();
highlightPetNames();
}, 100); // Check every 5 seconds
}
initializeTextBoxes();
highlightPetAttributes();
highlightPetNames();
// Save custom values when the page is unloaded
window.addEventListener('beforeunload', () => {
const customValues = getTextValues();
saveCustomValues(customValues);
});
// Start checking for updates
checkForUpdates();
})();