token switcher

try to take over your tokens!

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);