打码文字

按住Alt,选中文字,然后模糊

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         打码文字
// @version      0.1.2
// @description  按住Alt,选中文字,然后模糊
// @author       People11
// @match        *://*/*
// @namespace https://greasyfork.org/users/1143233
// ==/UserScript==

(function() {
    'use strict';

    const style = {
        filter: 'blur(9px)',
        userSelect: 'none'
    };

    function wrapTextWithSpan(textNode, start, end, style) {
        const span = document.createElement('span');
        Object.assign(span.style, style);

        const range = document.createRange();
        range.setStart(textNode, start);
        range.setEnd(textNode, end);
        range.surroundContents(span);
    }

    function processNode(node, style) {
        if (node.nodeType === Node.TEXT_NODE && window.getSelection().containsNode(node, true)) {
            wrapTextWithSpan(node, 0, node.nodeValue.length, style);
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            Array.from(node.childNodes).forEach(childNode => {
                if (window.getSelection().containsNode(childNode, true)) {
                    processNode(childNode, style);
                }
            });
        }
    }

    document.onmouseup = (event) => {
        if (event.altKey) {
            const selection = window.getSelection();
            if (!selection.isCollapsed) {
                const range = selection.getRangeAt(0);
                const commonAncestorContainer = range.commonAncestorContainer;

                if (commonAncestorContainer.nodeType === Node.TEXT_NODE) {
                    wrapTextWithSpan(commonAncestorContainer, range.startOffset, range.endOffset, style);
                } else if (commonAncestorContainer.nodeType === Node.ELEMENT_NODE) {
                    Array.from(commonAncestorContainer.childNodes).forEach(childNode => {
                        processNode(childNode, style);
                    });
                }

                selection.removeAllRanges();
            }
        }
    };
})();