Middle dot word truncator

Truncate words containing '·' at the middle dot

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();