Greasy Fork

Greasy Fork is available in English.

Import Only Init

Updates selected Pokémon when toggling "Only show imported sets".

当前为 2024-10-26 提交的版本,查看 最新版本

// ==UserScript==
// @name        Import Only Init
// @namespace   http://greasyfork.icu/en/users/1357767-indigeau
// @version     0.0
// @description Updates selected Pokémon when toggling "Only show imported sets".
// @match       https://calc.pokemonshowdown.com/*
// @author      indigeau
// @license     GNU GPLv3
// @icon        https://www.google.com/s2/favicons?sz=64&domain=pokemonshowdown.com
// @grant       none
// ==/UserScript==

// The default imported mon
const option = {};

// Setup toggle listeners
for (const target of document.querySelectorAll('#importedSets')) {
	const root = target.parentElement.parentElement;
	
	target.addEventListener('change', () => {
		const id = target.checked ? `${option.species} (${option.id})` : window.getFirstValidSetOption().id;
		
		$(root.querySelector('input.set-selector')).val(id).change();
		root.querySelector('.select2-chosen').innerText = id;
	});
}

// Set default imported mon
const setOption = (sets) => {
	for (const species of Object.keys(sets)) {
		if (!option.species || option.species > species) {
			option.species = species;
			
			option.id = Object.keys(sets[species])[0];
		}
	}
};

// Update default imported mon when new sets are added
window.updateDex = (() => {
	const update = window.updateDex;
	
	return (sets) => {
		update(sets);
		
		setOption(sets);
	};
})();

// Initialise default imported mon using saved sets
if (localStorage.customsets) {
	setOption(JSON.parse(localStorage.customsets));
}

/* global $ */