Pokemon Showdown Backup Helper

Pokemon Showdown Local Backup Helper

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Pokemon Showdown Backup Helper
// @name:zh-CN   Pokemon Showdown 备份助手
// @namespace    pokemon-showdown-backup-helper
// @version      0.4.0
// @description  Pokemon Showdown Local Backup Helper
// @description:zh-CN  Pokemon Showdown 本地备份助手
// @author       Sabertaz
// @license      MIT License
// @match        http://play.pokemonshowdown.com/*
// @match        https://play.pokemonshowdown.com/*
// @match        http://psim.us/*
// @match        https://psim.us/*
// @match        http://legacy.psim.us/*
// @match        https://legacy.psim.us/*
// @match        http://china.psim.us/*
// @match        https://china.psim.us/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const buttonGap = 40;
    const psTeamsFilename = 'PSTeams.dat';

    const psTeamsStorageKey = 'showdown_teams';
    let buttonIdx = 1;

    const createButton = (text, func) => {
        const button = document.createElement('button');
        button.type = 'button';
        button.value = 'button';
        button.style.position = 'fixed';
        button.style.right = '40px';
        button.style.bottom = `${buttonIdx * buttonGap}px`;
        button.style.zIndex = 9999;
        button.textContent = text;
        button.classList.add('button');
        button.addEventListener('click', func);
        buttonIdx++;
        return button;
    }

    const downloadTeam = () => {
        const teamData = localStorage.getItem(psTeamsStorageKey);
        const blob = new Blob([teamData], {type: 'text/csv'});

        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveBlob(blob, psTeamsFilename);
        } else {
            const elem = window.document.createElement('a');
            elem.href = window.URL.createObjectURL(blob);
            elem.download = psTeamsFilename;
            document.body.appendChild(elem);
            elem.click();
            document.body.removeChild(elem);
        }
    };

    const uploadTeam = () => {
        if (!window.FileReader) {
            alert('Your browser is not supported');
            return false;
        }

        const uploader = document.createElement('input');
        uploader.type = 'file';
        uploader.style.display = 'none';

        // listen for files
        uploader.addEventListener('change', () => {
            const files = uploader.files;

            if (files.length) {
                const reader = new FileReader();
                reader.addEventListener('load', () => {
                    uploader.parentNode.removeChild(uploader);
                    localStorage.setItem(psTeamsStorageKey, reader.result);
                    location.reload();
                })
                reader.readAsText(files[0]);
            }
        })

        // trigger input
        document.body.appendChild(uploader);
        uploader.click();
    };


    const downloadButton = createButton('Download All Teams', downloadTeam);
    const uploadButton = createButton('Upload Local Teams', uploadTeam);
    document.body.appendChild(downloadButton);
    document.body.appendChild(uploadButton);
})();