Telegram Web - Allow Saving Content

Bypass Telegram's saving content restrictions for media and text

目前為 2023-10-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Telegram Web - Allow Saving Content
// @namespace    c0d3r
// @license      MIT
// @version      0.2
// @description  Bypass Telegram's saving content restrictions for media and text
// @author       c0d3r
// @match        https://web.telegram.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=telegram.org
// @grant        unsafeWindow
// @grant        GM_addStyle
// ==/UserScript==

// Download selected media, uses WebK's built-in functions
async function downloadMedia(pid, mid) {
    // Get the message object based on peer and message ID
    var msg = await unsafeWindow.mtprotoMessagePort.getMessageByPeer(pid, mid);
    var myMedia;

    if (msg.media) {
        // Extract the media object; simple alternative to getMediaFromMessage
        myMedia = msg.media.document || msg.media.photo;
    }

    if (myMedia) {
        // Download media using the built-in function; auto sets file name and extension
        unsafeWindow.appDownloadManager.downloadToDisc({media: myMedia});
    }
}

(function () {
    'use strict';

    if (window.location.pathname.startsWith('/a/')) {
        // Redirect to the WebK version from the WebA version
        window.location.replace(window.location.href.replace('.org/a/', '.org/k/'));
    } else {
        // The root element used for watching and listening
        var colCenter = document.querySelector('#column-center');

        // Array of class names for media; we only add Download button if these are right clicked
        var clArray = ['photo', 'audio', 'video', 'voice-message', 'media-round', 'grouped-item', 'document-container', 'sticker'];

        // HTML code for the Download button
        var btnHtml = '<div class="btn-menu-item rp-overflow" id="down-btn"><span class="tgico btn-menu-item-icon"></span><span class="i18n btn-menu-item-text">Download</span></div>';

        // A flag for checking if we need to add the Download button
        var needBtn = false;

        // Variables for the current message and peer ID
        var curMid, curPid, observer;

        // Add CSS styles to allow text selection
        GM_addStyle('.chat.no-forwards .bubbles, .bubble, .bubble-content { -webkit-user-select: text!important; -moz-user-select: text!important; user-select: text!important; }');

        colCenter.addEventListener('mouseup', function (e) {
            // Listen to the right mouse button clicks
            if (e.button === 2) {
                needBtn = false;
                // Test if the current chat has restricted content saving
                if (document.querySelector('.chat.no-forwards')) {
                    // Find the closest element containing message and peer IDs
                    var closest = e.target.closest('[data-mid]');
                    if (closest) {
                        // Check if the element actually contains some media classes
                        if (clArray.some(function (clName) {
                            return closest.classList.contains(clName);
                        })) {
                            curMid = closest.dataset.mid;
                            curPid = closest.dataset.peerId;
                            needBtn = true;
                        }
                    }
                }
            }
        });

        observer = new MutationObserver(function (mutList) {
            mutList.forEach(function (mut) {
                mut.addedNodes.forEach(function (anod) {
                    // Check if context menu has been added to the DOM
                    if (anod.id == 'bubble-contextmenu' && needBtn) {
                        // Add the custom Download button and assign a click event
                        anod.querySelector('.btn-menu-item').insertAdjacentHTML('beforebegin', btnHtml);
                        anod.querySelector('#down-btn').addEventListener('click', function () {
                            downloadMedia(curPid, curMid);
                        });
                    }
                });
            });
        });

        // Observe when context menu is added to the DOM
        observer.observe(colCenter, {
            subtree: true, childList: true
        });
    }
})();