Bionic Readd Highlighter (English Only)

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

目前為 2024-02-27 提交的版本,檢視 最新版本

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

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

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

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

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