Greasy Fork

来自缓存

Greasy Fork is available in English.

智能屏蔽 ESC (针对 Gemini/Telegram/AI Studio)

专门解决在 Gemini、AI Studio 和 Telegram Web 中使用中文输入法时,按 ESC 撤回词项却意外打断 AI 生成或触发快捷键的问题。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         智能屏蔽 ESC (针对 Gemini/Telegram/AI Studio)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  专门解决在 Gemini、AI Studio 和 Telegram Web 中使用中文输入法时,按 ESC 撤回词项却意外打断 AI 生成或触发快捷键的问题。
// @author       Roy
// @match        https://gemini.google.com/*
// @match        https://aistudio.google.com/*
// @match        https://web.telegram.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isComposing = false;
    let lastCompositionEndTime = 0;

    // 记录输入法开始:此时正在敲拼音选词
    window.addEventListener('compositionstart', () => {
        isComposing = true;
    }, true);

    // 记录输入法结束:可能是选词上屏,也可能是按 ESC 取消了选词
    window.addEventListener('compositionend', () => {
        isComposing = false;
        lastCompositionEndTime = Date.now();
    }, true);

    function handleEscKey(e) {
        if (e.key === 'Escape' || e.keyCode === 27) {
            const now = Date.now();
            
            // 判定:如果正在输入,或者 100ms 内刚刚结束输入(ESC 导致的结束)
            // 我们认为这个 ESC 是发给输入法的,而不是发给网页的
            const isImeEsc = isComposing || (now - lastCompositionEndTime < 100);

            if (isImeEsc) {
                const activeElement = document.activeElement;
                // 确保焦点在输入框内
                const isInputFocused = activeElement && (
                    ['TEXTAREA', 'INPUT'].includes(activeElement.tagName) ||
                    activeElement.isContentEditable ||
                    activeElement.getAttribute('role') === 'textbox' ||
                    activeElement.closest('[contenteditable="true"]')
                );

                if (isInputFocused) {
                    // 拦截事件捕获,不让网页原生 JS监听到这个 ESC
                    e.stopImmediatePropagation();
                    e.stopPropagation();
                    console.log('Smart ESC Shield: 拦截了一次输入法冲突事件');
                }
            }
        }
    }

    // 在捕获阶段监听 keydown
    window.addEventListener('keydown', handleEscKey, true);

})();