Bionic Readd Highlighter (English Only)

Highlight the first three letters of each word in English on a webpage.

当前为 2024-02-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bionic Readd Highlighter (English Only)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Highlight the first three letters of each word in English on a webpage.
// @author       Vijay
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    var highlighted = false;
    var currentDomain = window.location.hostname;

    // Function to highlight words
    function highlightWords() {
        var textNodes = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            null,
            false
        );

        while(textNodes.nextNode()) {
            var node = textNodes.currentNode;
            var text = node.nodeValue;

            // Regex to match words
            var wordRegex = /\b\w+\b/g;
            var match;

            // Iterate through each word in the text node
            while ((match = wordRegex.exec(text)) !== null) {
                var word = match[0];
                var highlightedWord = word.substring(0, 3);
                var remainingWord = word.substring(3);
                var replacement = '<span style="background-color: yellow;">' + highlightedWord + '</span>' + remainingWord;
                text = text.replace(word, replacement);
            }

            // Update the text node with highlighted words
            if (text !== node.nodeValue) {
                var spanNode = document.createElement('span');
                spanNode.innerHTML = text;
                node.parentNode.replaceChild(spanNode, node);
            }
        }

        highlighted = true;
    }

    // Function to remove highlighting
    function removeHighlighting() {
        var highlightedWords = document.querySelectorAll('span[style="background-color: yellow;"]');
        if (highlightedWords.length > 0) {
            highlightedWords.forEach(function(word) {
                word.outerHTML = word.innerHTML;
            });
        }

        highlighted = false;
    }

    // Floating button to toggle feature
    var floatingButton = document.createElement('div');
    floatingButton.innerHTML = '<img src="https://play-lh.googleusercontent.com/TI8o079rVoxaQ5ZeDcLfQRlS7MQrwNbpGh4-WdOYC2lYIZk1jAhABtABLU_kl2aReCSl" style="width: 50px; height: 50px; position: fixed; bottom: 20px; right: 20px; cursor: pointer;">';
    document.body.appendChild(floatingButton);

    floatingButton.addEventListener('click', function() {
        if (!highlighted) {
            highlightWords();
        } else {
            removeHighlighting();
        }
    });
})();