TG Shortcuts

Add kb shortcut to Telegram

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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