Text Highlighter

Evidenzia il testo su una pagina web

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();