Fix WhatsApp's Ctrl+Del hotkey

Disable WhatsApp's feature where Ctrl+Del deletes the current chat, instead of the default behavior of deleting the word after the cursor.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fix WhatsApp's Ctrl+Del hotkey
// @version      1.1
// @description  Disable WhatsApp's feature where Ctrl+Del deletes the current chat, instead of the default behavior of deleting the word after the cursor.
// @author       ajp_anton
// @license      MIT
// @match        https://web.whatsapp.com/
// @grant        none
// @namespace https://greasyfork.org/users/1454657
// ==/UserScript==

(function() {
    'use strict';

    let lastInput = null;

    function attachListenerToInput(input) {
        if (!input || input._ctrlDelFixed) return;

        input.addEventListener('keydown', function(e) {
            if (e.ctrlKey && e.key === 'Delete') {
                e.stopPropagation(); // Block chat deletion
            }
        }, true); // Use capture phase

        input._ctrlDelFixed = true; // Mark this input so we don’t attach again
        lastInput = input;
    }

    // Observe changes in the DOM
    const observer = new MutationObserver(() => {
        const input = document.querySelector('[contenteditable="true"][data-tab="10"]');
        if (input && input !== lastInput) {
            attachListenerToInput(input);
        }
    });

    // Start observing the whole document
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();