跳到正文

剪贴板

剪贴板 sbqoni1p

原文
剪贴板信息

剪贴板信息

收录
更新
C++
// ==UserScript==
// @name         猫娘插件
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  喵
// @author       bobo
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function processInput(target, isContentEditable) {
        let value = isContentEditable ? target.textContent : target.value;
        let cursorPos = isContentEditable ? getCaretPosition(target) : target.selectionStart;

        if (cursorPos < 1 || value[cursorPos - 1] !== '。') return;

        // 避免重复:前面已经是“喵。”
        if (cursorPos >= 2 && value[cursorPos - 2] === '喵') {
            return;
        }

        const newValue = value.substring(0, cursorPos - 1) + '喵。' + value.substring(cursorPos);

        if (isContentEditable) {
            const oldScrollTop = target.scrollTop;
            target.textContent = newValue;
            // 光标放在“喵。”之后 → cursorPos + 1
            setCaretPosition(target, cursorPos + 1);
            target.scrollTop = oldScrollTop;
        } else {
            target.value = newValue;
            target.setSelectionRange(cursorPos + 1, cursorPos + 1);
        }
    }

    function getCaretPosition(element) {
        const selection = window.getSelection();
        if (selection.rangeCount === 0) return 0;
        const range = selection.getRangeAt(0).cloneRange();
        range.selectNodeContents(element);
        range.setEnd(selection.getRangeAt(0).endContainer, selection.getRangeAt(0).endOffset);
        return range.toString().length;
    }

    function setCaretPosition(element, pos) {
        // 先获取总文本长度,避免越界
        const totalLength = element.textContent.length;
        if (pos > totalLength) pos = totalLength;

        const selection = window.getSelection();
        const range = document.createRange();
        let charCount = 0;
        let found = false;

        function walk(node) {
            if (found || node.nodeType !== Node.TEXT_NODE) return;
            const nodeLen = node.textContent.length;
            if (charCount + nodeLen >= pos) {
                const offset = pos - charCount;
                range.setStart(node, offset);
                range.collapse(true);
                found = true;
            } else {
                charCount += nodeLen;
            }
        }

        // 使用 TreeWalker 更可靠地遍历文本节点
        const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
        let node;
        while (!found && (node = walker.nextNode())) {
            walk(node);
        }

        if (!found) {
            // 放在末尾
            range.selectNodeContents(element);
            range.collapse(false);
        }

        selection.removeAllRanges();
        selection.addRange(range);
    }

    document.addEventListener('input', function(e) {
        const t = e.target;
        const isEditable = t.isContentEditable;
        const isTextInput = (t.tagName === 'INPUT' && t.type === 'text') || t.tagName === 'TEXTAREA';

        if (isEditable || isTextInput) {
            setTimeout(() => processInput(t, isEditable), 0);
        }
    }, true);

    document.addEventListener('compositionend', function(e) {
        if (e.target.isContentEditable) {
            setTimeout(() => processInput(e.target, true), 0);
        }
    }, true);
})();