t.me fix

Make t.me image savable and prevent "open with..." dialog on load

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        t.me fix
// @description Make t.me image savable and prevent "open with..." dialog on load
// @namespace   http://github/hafeoz
// @match       https://t.me/*
// @match       https://telegram.me/joinchat/*
// @grant       none
// @run-at      document-start
//
// @version     1.1.0
// @author      hafeoz
// @icon        https://telegram.org/img/website_icon.svg
// @license     0BSD OR CC0-1.0 OR WTFPL
// @homepageURL https://gist.github.com/hafeoz/666c8cd11a3c7be20167a3aeb3e9df7b
// @supportURL  https://gist.github.com/hafeoz/666c8cd11a3c7be20167a3aeb3e9df7b
// ==/UserScript==
window.onload = function() {
    for (const elem of document.getElementsByClassName("tgme_widget_message_photo_wrap")) {
        const src = /^url\("([^"]+)"\)$/.exec(elem.style.backgroundImage)[1];
        elem.style.backgroundImage = "";
        const imgelem = document.createElement("img");
        elem.appendChild(imgelem);
        const placeholder = elem.getElementsByClassName("tgme_widget_message_photo")[0];
        if (placeholder !== undefined) {
            imgelem.classList = placeholder.classList;
            placeholder.remove();
        }
        imgelem.style.width = "100%";
        imgelem.src = src;
    }
}

// https://caniuse.com/mdn-api_element_beforescriptexecute_event
window.addEventListener("beforescriptexecute", (e) => {
    if (e.target.text.includes("protoUrl")) {
        //e.preventDefault();
        e.target.text = e.target.text.replace(/tg:\\\/\\\/resolve[^\"]+/, "");
    }
});