Link Click Tracker

クリック済みリンクを記録し、視覚的に分かるように表示

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Link Click Tracker
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  クリック済みリンクを記録し、視覚的に分かるように表示
// @author       Your Name
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    // ハッシュ値を計算する関数 (SHA-256 + 先頭8文字)
    async function computeHash(link) {
        const encoder = new TextEncoder();
        const data = encoder.encode(link);
        const hashBuffer = await crypto.subtle.digest("SHA-256", data);
        const hashArray = Array.from(new Uint8Array(hashBuffer));
        const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, "0")).join("");
        return hashHex.substring(0, 8);
    }

    // リンクにチェックマークを追加する関数
    function addCheckmark(link) {
        // リンク内にimgタグが含まれていない場合のみチェックマークを追加
        if (!link.textContent.includes('✅') && !link.querySelector("img")) {
            link.textContent = "✅ " + link.textContent; // テキストの先頭にチェックマークを追加
        }
    }

    // ページ内のすべてのリンクをチェック
    async function checkLinks() {
        const visitedLinks = JSON.parse(localStorage.getItem("visitedLinks")) || {};
        const links = document.querySelectorAll("a[href]");

        for (const link of links) {
            const href = link.href;

            // ハッシュ値を計算して確認
            const hash = await computeHash(href);
            if (visitedLinks[hash]) {
                addCheckmark(link);
            }

            // クリックイベントを追加
            link.addEventListener("click", async () => {
                const clickHash = await computeHash(href);
                visitedLinks[clickHash] = true;
                localStorage.setItem("visitedLinks", JSON.stringify(visitedLinks));
                checkLinks();
            });
        }
    }

    // ページロード時にリンクをチェック
    checkLinks();
})();