Discord - use all emojis

Send any emoji or sticker as an image link. Right-click for a full resolution image.

// ==UserScript==
// @name         Discord - use all emojis
// @name:ru      Discord - все смайлы
// @namespace    icosferu
// @version      1.3.10
// @description  Send any emoji or sticker as an image link. Right-click for a full resolution image.
// @description:ru  Отправляет любой эмодзи или стикер в виде ссылки на изображение. Правый клик даст полноразмерную фотку.
// @author       icosferu
// @match        https://discord.com/*
// @icon         https://www.google.com/s2/favicons?domain=discord.com
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @grant        GM.registerMenuCommand
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM.setValue
// @grant        GM.getValue
// ==/UserScript==

(async function() {
    'use strict';

    function get_token () {

        let iframe = document.createElement('iframe');
        iframe.style.display = 'none';
        document.body.appendChild(iframe);

        let storage = iframe.contentWindow.localStorage;
        let token = storage.token.replace(/"/g, '');

        iframe.remove();
        return token;
    }

    function send (message) {

        let token = get_token();

        let channel_id = document.location.pathname.split('/').pop();

        let data = {
            "content": message,
            "tts": "false"
        }

        $.ajax({
            type: 'POST',
            url: 'https://discord.com/api/v6/channels/' + channel_id + '/messages',
            data: JSON.stringify(data),
            headers: {
                '%3Aauthority': 'discord.com',
                '%3Amethod': 'POST',
                '%3Apath': '/api/v6/channels/' + channel_id + '/messages',
                '%3Ascheme': 'https',
                'Authorization': token,
                'Content-Type': 'application/json'
            }
        });
    }

    const probability = 35;

    document.addEventListener('mousedown', function (e) {

        let link = undefined;
        function set_link (img, size) {
            if (img !== null) {
                link = new URL(img.src);

                let server = $('[class*="titleSecondary-"] > strong')[0].textContent;
                link.search = '?' + (right_click ? '' : `size=${size}&`) + `server=${server}`;

            }
        }

        let right_click = (e.button === 2);

        if (e.target.classList.value.includes('emojiItem-')
        && (e.target.classList.value.includes('emojiItemDisabled')
            || right_click)) {

            if (Date.now() % 1000 < probability) {
                add_gayness(); // gayness for all!
            }

            let img = e.target.querySelector('img');
            set_link(img, 48);

        } else
        if (e.target.classList.value.includes('stickerAsset-')
        && (e.target.parentNode.parentNode.classList.value.includes('stickerUnsendable')
            || right_click)) {

            let img = e.target;
            set_link(img, 160);
            
        } else
        if (right_click && e.target.classList.value.includes('container-')
        && (e.target.querySelector('img') !== null)) {
            link = new URL(e.target.querySelector('a').href);
        }

        if (link !== undefined) {

            send(link.href);
        }
    });

    document.addEventListener('mousemove', function (e) {

        let {target} = e;
        let parent = target.parentNode;

        if (target.tagName === 'IMG'
        &&  parent.tagName === 'A') {

            let server = (new URL(parent.href)).searchParams.get('server');
            parent.title = (server !== null ? `from ${server}` : '');
            
        }
    });

    let iframe2 = document.createElement('div');

    setInterval(async () => {
        if (!await GM.getValue('active', true)) {
            return;
        }

        let elem = $('[class*="bodyWrapper-"]')[0];
        if (elem !== undefined && !document.contains(iframe2)) {
            function createElementFromHTML(htmlString) {
                var div = document.createElement('div');
                div.innerHTML = htmlString.trim();
                return div.firstChild;
            }

            let width = 400;
            let height = Math.trunc(width * 3/8)*2;

            if (Date.now() % 1000 < probability) {
                iframe2 = createElementFromHTML(`<iframe width="${width}" height="${height}" src="https://www.youtube.com/embed/DLzxrzFCyOs?autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`);
                elem.appendChild(iframe2);

                iframe2.style.position = 'absolute';
                iframe2.style['z-index'] = 10000;
                iframe2.style.left = 0;
                iframe2.style.top = 0;
                iframe2.style.width = '100%';
                iframe2.style.height = '100%';

                let div = document.createElement('div');
                elem.appendChild(div);

                div.style.position = 'absolute';
                div.style['z-index'] = 10001;
                div.style.left = 0;
                div.style.top = 0;
                div.style.width = `${width}px`;
                div.style.height = `${height}px`;
            } else {
                iframe2 = document.createElement('div');
                elem.appendChild(iframe2);
            }
            
        }
    }, 500);

    let command;

    let register1 = () => {
        command = GM_registerMenuCommand(`don't rickroll me!`, () => {
            GM.setValue('active', false);
            GM_unregisterMenuCommand(command);
            register2();
        }, '');
    };

    let register2 = () => {
        command = GM_registerMenuCommand(`rickroll me please!`, () => {
            GM.setValue('active', true);
            GM_unregisterMenuCommand(command);
            register1();
        }, '');
    };

    if (typeof GM_unregisterMenuCommand !== 'undefined') {
        if (await GM.getValue('active', true)) {
            register1();
        } else {
            register2();
        }
    } else {
        GM.registerMenuCommand(`don't rickroll me!`, () => {
            GM.setValue('active', false);
        });
        GM.registerMenuCommand(`rickroll me please!`, () => {
            GM.setValue('active', true);
        });
    }



    let last = await GM.getValue('last', 0);
    if (!isFinite(last)) {
        last = 0;
    }


    async function add_gayness () {

        if (!await GM.getValue('active', true)) {
            return;
        }

        let time = Date.now();

        if (time - last < 3600 * 1000) {
            return;
        }

        last = time;
        GM.setValue('last', last);

        function createElementFromHTML(htmlString) {
            var div = document.createElement('div');
            div.innerHTML = htmlString.trim();
            return div.firstChild;
        }

        let iframe = createElementFromHTML(`<iframe width="400" height="300" src="https://www.youtube.com/embed/DLzxrzFCyOs?autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`);
        document.body.appendChild(iframe);

        iframe.style.position = 'fixed';
        iframe.style['z-index'] = 10000;
        iframe.style.left = 0;
        iframe.style.top = 0;
        iframe.style.width = '100%';
        iframe.style.height = '100%';

        let text = '||[I\'m being rickrolled :smiling_face_with_3_hearts:]||';
                 //+ '||U can get the script here and use all emojis too: <https://greasyfork.org/en/scripts/432506-discord-use-all-emojis>||';

        setTimeout(() => send(text), 1500);
        setTimeout(() => iframe.remove(), 15000);
    }


    let style = document.createElement('style');

    style.textContent =
        `.theme-dark [class*="emojiItemDisabled"] {\n`+
        `    filter: drop-shadow(0px 0px 1px white) !important;\n`+
        `}\n`+
        `.theme-light [class*="emojiItemDisabled"] {\n`+
        `    filter: drop-shadow(0px 0px 2px purple) !important;\n`+
        `}\n`+

        `.theme-dark [class*="stickerUnsendable"] {\n`+
        `    filter: drop-shadow(0px 0px 1px white) !important;\n`+
        `}\n`+
        `.theme-light [class*="stickerUnsendable"] {\n`+
        `    filter: drop-shadow(0px 0px 2px purple) !important;\n`+
        `}\n`+

        `[class*="premiumPromo-"], [class*="upsellWrapper-"] {\n`+
        `    display: none !important;\n`+
        `}\n`;

    document.head.append(style);

})();