Middle dot word truncator

Truncate words containing '·' at the middle dot

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Middle dot word truncator
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.1
// @author      kirinyaga
// @description Truncate words containing '·' at the middle dot
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to process text nodes
    function processTextNode(node) {
        const text = node.nodeValue;

        // Split text into words while preserving whitespace
        const words = text.split(/(\s+)/);

        // Process each word
        const processedWords = words.map(word => {
            // Skip whitespace-only segments
            if (/^\s+$/.test(word)) return word;

            // Check if word contains middle dot
            if (word.includes('·')) {
                // Split at first middle dot and take the part before it
                return word.split('·')[0];
            }
            return word;
        });

        // Reconstruct the text
        const newText = processedWords.join('');

        // Only update if changed
        if (newText !== text) {
            node.nodeValue = newText;
        }
    }

    function scanTextNodes() {
        // Create a TreeWalker to traverse all text nodes
        const treeWalker = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            {
                acceptNode: function(node) {
                    // Skip text nodes in script, style, and textarea elements
                    if (node.parentNode.nodeName === 'SCRIPT' ||
                        node.parentNode.nodeName === 'STYLE' ||
                        node.parentNode.nodeName === 'TEXTAREA') {
                        return NodeFilter.FILTER_SKIP;
                    }
                    return NodeFilter.FILTER_ACCEPT;
                }
            },
            false
        );

        // Process each text node
        const textNodes = [];
        while (treeWalker.nextNode()) {
            textNodes.push(treeWalker.currentNode);
        }

        // Process nodes in reverse to avoid issues with DOM modification
        for (let i = textNodes.length - 1; i >= 0; i--) {
            processTextNode(textNodes[i]);
        }
    }

    setTimeout(scanTextNodes,3000);
})();