TG Shortcuts

Add kb shortcut to Telegram

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         TG Shortcuts
// @namespace    http://tampermonkey.net/
// @version      2024-06-21
// @description  Add kb shortcut to Telegram
// @author       Jatin Sharma
// @match        https://web.telegram.org/k/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=telegram.org
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    function waitForElem(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

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

            // If you get "parameter 1 is not of type 'Node'" error, see https://stackoverflow.com/a/77855838/492336
            observer.observe(document.body, { childList: true, subtree: true });
        });
    }

    document.addEventListener('keydown', async function(event) {
        // Define the key combination for the shortcut
        const isShortcut = event.ctrlKey && event.key === '/' && !event.repeat;
        if(isShortcut) {
            if (document.querySelector('button.sidebar-close-button')?.offsetParent) {
                document.querySelector('button.sidebar-close-button').click();
            }
            else {
                document.querySelector('div.sidebar-header__btn-container > button').click();
                (await waitForElem('span.archived-count'));//.parentElement.click();
                setTimeout(() => document.querySelector('span.archived-count').parentElement.click(), 40);
            }
        }

    });
    // Your code here...
})();