Greasy Fork

Greasy Fork is available in English.

[Pokeclicker] Simple Weather Changer

Adds a button to select the weather for the current region, also freezes all weather

当前为 2022-06-03 提交的版本,查看 最新版本

// ==UserScript==
// @name        [Pokeclicker] Simple Weather Changer
// @namespace   Pokeclicker Scripts
// @match       https://www.pokeclicker.com/
// @grant       none
// @version     1.1
// @author      KarmaAlex (Credit: Ephenia)
// @description Adds a button to select the weather for the current region, also freezes all weather
// ==/UserScript==

let weatherFunc;

function initWeatherChange(){
    weatherFunc = Weather.generateWeather;
    //Make button
    const weatherBtn = document.createElement('button');
    weatherBtn.textContent = 'Change';
    weatherBtn.className = 'btn btn-block btn-success';
    weatherBtn.id = 'change-weather';
    document.getElementById('townMap').appendChild(weatherBtn);
    document.getElementById('change-weather').addEventListener('click', changeWeather, false);
    //Make selectbox
    const weatherSelect = document.createElement('select');
    weatherSelect.innerHTML =
    `<option value="-1">Default</option>
    <option value="0">Clear</option>
    <option value="1">Overcast</option>
    <option value="2">Rain</option>
    <option value="3">Thunderstorm</option>
    <option value="4">Snow</option>
    <option value="5">Hail</option>
    <option value="6">Blizzard</option>
    <option value="7">Sunny</option>
    <option value="8">Sandstorm</option>
    <option value="9">Fog</option>
    <option value="10">Windy</option>`
    weatherSelect.id = 'weather-select'
    document.getElementById('townMap').appendChild(weatherSelect);

    addGlobalStyle('#change-weather { position: absolute; right: 133px; top: 0px; width: auto; height: 41px; font-size: 11px; margin: 0px; }');
    addGlobalStyle('#weather-select { position: absolute; right: 50px; top: 10px; width: auto; height: 20px; font-size: 9px; }');
    //Set weather to last weather option, is broken with new loading
    if (!isNaN(parseInt(localStorage.getItem('scriptWeather')))) {
        const getWeather = parseInt(localStorage.getItem('scriptWeather'));
        document.getElementById('weather-select').value = getWeather;
        Weather.regionalWeather.forEach((Weather) => { Weather(getWeather); });
    };
}

function changeWeather(){
    const selWeather = +document.getElementById('weather-select').value;
    if (selWeather != -1) {
        //Freeze weather
        Weather.generateWeather = function(){ return true };
        //Set Weather
        Weather.regionalWeather.forEach((Weather) => { Weather(selWeather); });
    } else {
        //Unfreeze weather
        Weather.generateWeather = weatherFunc;
        //Default Weather
        const now = new Date();
        Weather.generateWeather(now);
    }
    localStorage.setItem('scriptWeather', selWeather >= 0 ? selWeather : false);
}

function loadScript(){
    const oldInit = Preload.hideSplashScreen;

    Preload.hideSplashScreen = function(){
        const result = oldInit.apply(this, arguments);
        initWeatherChange();
        return result;
    }
}

var scriptName = 'simpleweatherchanger'

if (document.getElementById('scriptHandler') != undefined){
    const scriptElement = document.createElement('div');
    scriptElement.id = scriptName;
    document.getElementById('scriptHandler').appendChild(scriptElement);
    if (localStorage.getItem(scriptName) != null){
        if (localStorage.getItem(scriptName) == 'true'){
            loadScript();
        }
    }
    else{
        localStorage.setItem(scriptName, 'true')
        loadScript();
    }
}
else{
    loadScript();
}

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}