token switcher

try to take over your tokens!

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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.greatest.deepsurf.us/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);