Login Via Token

Adds a native looking button to the login screen for your token.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

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

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         Login Via Token
// @namespace    https://v15.studio/
// @version      v4.0.0
// @description  Adds a native looking button to the login screen for your token.
// @author       Emmet_v15
// @license      BSD
// @match        *://discord.com/login
// @grant        none
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @run-at       document-end
// ==/UserScript==

if (window.location.pathname === '/login' && localStorage.token && !localStorage.working) {
    localStorage.removeItem('token');
    window.location.reload();
} else if (localStorage.working) {
    localStorage.removeItem('working');
}

function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}
waitForElm('button[type="submit"][class*="button__"]').then((elm) => {

    const customButton = elm.cloneNode(true);
    customButton.id = 'tokenLoginBtn';
    customButton.type = 'button';
    const buttonText = customButton.querySelector('[class*="contents__"]');
    if (buttonText) buttonText.textContent = 'Log In Via Token';

    elm.className = elm.className.replace(/marginBottom8_(\S+)/g, 'marginBottom20_$1');
    elm.addEventListener('click', function() {
        document.getElementById('tokenInput').value = " ";
    });

    const passwordField = document.querySelector('[name="password"]');
    const fieldWrapper = passwordField.closest('[class*="container__"][data-layout="vertical"]');

    const tokenFieldHTML = fieldWrapper.outerHTML
        .replace(/password/g, 'token')
        .replace(/Password/g, 'Token')
        .replace(/uid_\d+/g, 'tokenInput')
        .replace(/:r\d+:/g, 'tokenLabel');

    const container = document.createElement('div');
    container.innerHTML = tokenFieldHTML;
    const tokenField = container.firstChild;

    const marginClass = document.querySelector('[class*="marginBottom20"]').className.match(/marginBottom20\S+/)[0];
    if (marginClass) {
        tokenField.classList.add(marginClass);
    } else {
        tokenField.style.marginBottom = '20px';
    }

    elm.parentNode.insertBefore(tokenField, elm.nextSibling);
    elm.parentNode.insertBefore(customButton, tokenField.nextSibling);

    customButton.addEventListener('click', function() {
        const token = document.getElementById('tokenInput').value.trim();
        const iframe = document.body.appendChild(document.createElement('iframe'));
        iframe.contentWindow.localStorage.token = `"${token}"`;
        iframe.contentWindow.localStorage.working = `1`;
        window.location.reload();
    });
});