Text Highlighter

Evidenzia il testo su una pagina web

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Text Highlighter
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Evidenzia il testo su una pagina web
// @author       Magneto1
// @license      MIT
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    // Aggiungi uno stile per l'evidenziazione
    GM_addStyle(`
        .highlight {
            background-color: yellow;
            cursor: pointer;
        }
    `);

    // Funzione per evidenziare il testo selezionato
    const highlightSelectedText = () => {
        const selection = window.getSelection();
        if (selection.rangeCount > 0) {
            const range = selection.getRangeAt(0);
            const span = document.createElement('span');
            span.className = 'highlight';
            range.surroundContents(span);
            saveHighlights();
        }
    };

    // Funzione per salvare le evidenziazioni nel localStorage
    const saveHighlights = () => {
        const highlights = document.querySelectorAll('.highlight');
        const highlightArray = Array.from(highlights).map(highlight => highlight.innerText);
        localStorage.setItem('highlights', JSON.stringify(highlightArray));
    };

    // Funzione per caricare le evidenziazioni dal localStorage
    const loadHighlights = () => {
        const highlights = JSON.parse(localStorage.getItem('highlights')) || [];
        highlights.forEach(text => {
            const regex = new RegExp(text, 'g');
            document.body.innerHTML = document.body.innerHTML.replace(regex, `<span class="highlight">${text}</span>`);
        });
    };

    // Carica le evidenziazioni all'avvio
    loadHighlights();

    // Aggiungi un comando al menu di Violentmonkey per evidenziare il testo
    GM_registerMenuCommand("Evidenzia Testo Selezionato", highlightSelectedText);
})();