token switcher

try to take over your tokens!

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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         token switcher
// @namespace    https://tampermonkey.net/
// @version      2.0.2
// @description  try to take over your tokens!
// @author       sophb.chan
// @core_author  sophb.chan
// @core_version 1.1.0
// @match        https://multiplayerpiano.org/*
// @match        https://multiplayerpiano.net/*
// @match        https://dev.multiplayerpiano.net/*
// @match        https://piano.mpp.community/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      CC BY-NC-SA 4.0
// @require      https://update.greasyfork.org/scripts/582107/1848144/MPP%20Userscript%20Core.js
// ==/UserScript==

prefix = '~';
private = true

const tokens = readItem('knownTokens', {});
tokens.current = localStorage.token;
tokens.ipbound = '';
let defaultToken = readItem('defaultToken', localStorage.token);

function setDefaultToken(name) {
    if (!user)
        throw new Error('Token name must be specified.');

    if (tokens[name] != null) {
        defaultToken = name;
        storeItem('defaultToken', name);
    } else throw new ReferenceError(`Unknown token "${name}"`);
}
function addToken() {
    const token = prompt(`
(In-built UI coming soon!)
Please specify the token you want to save:
        `.trim());

    if (token == null) return;
    if (!token) {
        alert('Cannot save empty token!');    
        return addToken();
    }

    const name = prompt(`
(In-built UI coming soon!)
Please specify the token's name:
        `.trim());

    if (!name) {
        const label = i => `unnamed-token-${i}`; 
        let unnamedIndex = 0;
        do {
            unnamedIndex++;
        } while (tokens[label(unnamedIndex)] == null);

        name = label(unnamedIndex);
    }

    const isCorrect = confirm(`
Token registered.
Name: ${name}
Token: ${token}

Press Cancel to change the information.
    `.trim());

    if (isCorrect) {
        tokens[name] = token;
        return storeItem('knownTokens', tokens);
    }
    else return addToken();
}

registerCommand('help', (({
    args
} = {}) => {
    receive(`Commands: \`${
        prefix +
        Object.keys(cmds).join(
            `\`, \`${prefix}`
        )
    }\``);
}), {
    aliases: ['h']
});
registerCommand('set', (({
    args
} = {}) => {
    if (args.length === 0) {
        receive('Please specify a user token to set to.');
        receive(`Tokens: \`${Object.keys(tokens).join('`, `')}\``);
        return;
    }
    if (tokens[args[0]]) {
        localStorage.token = tokens[args[0]];
        receive('Token set. Reconnecting...');
        MPP.client.stop();
        MPP.client.start();
    } else receive(`The token \`${args[0]}\` was not found.`);
}));
registerCommand('reset', (() => {
    localStorage.token = tokens[defaultToken];
    send('Token set to default. Reconnecting...');
    MPP.client.stop();
    MPP.client.start();
}));
registerCommand('default', (({
    args
} = {}) => {
    if (args.length === 0) {
        receive('Please specify a user token to set as the default.');
        receive(`Tokens: \`${Object.keys(tokens).join('`, `')}\``);
        return;
    }
    if (tokens[args[0]]) {
        setDefaultToken(args[0]);
        receive(`Default token now set to \`${args[0]}\`.`);
        MPP.client.stop();
        MPP.client.start();
    } else receive(`The token \`${args[0]}\` was not found.`);
}));
registerCommand('add', addToken);