SankakuComplex - URL Verifier with IndexedDB

Check if the URL has been accessed before and signaling to the user. With this script you will be able to know if you have already accessed the specific page of some image/video/tag before. If you have never accessed a URL, it will persist it in IndexedDB, if you have already accessed it, a warning message will be added to your screen.

目前為 2024-12-28 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         SankakuComplex - URL Verifier with IndexedDB
// @namespace    http://tampermonkey.net/
// @version      0.14
// @description  Check if the URL has been accessed before and signaling to the user. With this script you will be able to know if you have already accessed the specific page of some image/video/tag before. If you have never accessed a URL, it will persist it in IndexedDB, if you have already accessed it, a warning message will be added to your screen.
// @author       mtpontes
// @match       *://chan.sankakucomplex.com/*
// @match       *://idol.sankakucomplex.com/*
// @match       *://legacy.sankakucomplex.com/*
// @grant        none
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABbmlDQ1BpY2MAACiRdZHPKwRhGMc/dolYbeIgOewBOeyWKDlqFZflsFZZXGZmZ3fVzphmZpNclYuDchAXvw7+A67KlVKKlOTgL/DrIo3n3VUr8U7vPJ++7/t9et/vC6FUybC8+gGwbN9NTyRjs9m5WOMTETpoox00w3Mmp8cz/Dveb6hT9Tqhev2/78/RkjM9A+qahIcNx/WFR4VTy76jeEO4wyhqOeF94bgrBxS+ULpe5UfFhSq/KnYz6TEIqZ6xwg/Wf7BRdC3hfuEeq1Q2vs+jbhIx7ZlpqV0yu/FIM0GSGDplFinhk5BqS2Z/+wYqvimWxGPI32EFVxwFiuKNi1qWrqbUvOimfCVWVO6/8/TyQ4PV7pEkNDwEwUsvNG7B52YQfBwEwechhO/hzK75lySnkTfRN2tazx5E1+DkvKbp23C6Dp13juZqFSksM5TPw/MxtGah/Qqa56tZfa9zdAuZVXmiS9jZhT7ZH134ArhcZ+m/WStSAAAACXBIWXMAAAsSAAALEgHS3X78AAAAeElEQVQ4y2NgoCX4Xyb7H4TxqWHCo7keG5toA4CgAQebsAHYbMTlCiYibMfrCiZibcIlx0SE3/GGBRM+Gxi7HjeCMD41TESGPE5XMOGzHRsbnxcIxbsDDAMts4cbjmR7A4kpvQHkMiZCKY1QSmXBZTKedNBA1RwLAFCeNCTVhz2FAAAAAElFTkSuQmCC

// @grant        none
// @license MIT
// ==/UserScript==

(function() {

    console.log('Starting execution of the visited URLs persister...');
    const dbName = 'VisitedDB';
    const storeName = 'visitedURLs'
    const readMode = 'readonly'
    const writeMode = 'readwrite'

    function getDatabaseConnection() {
        return new Promise((resolve, reject) => {
            const request = indexedDB.open(dbName, 1);

            // If the database structure needs to be created or updated
            request.onupgradeneeded = (event) => {
                const db = event.target.result;
                if (!db.objectStoreNames.contains(storeName)) {
                    db.createObjectStore(storeName, { keyPath: 'url' });
                }
            };

            request.onsuccess = (event) => resolve(event.target.result); // Returns the database as the result of the Promise
            request.onerror = (event) => reject('Error opening the database');
        });
    }

    function getTransaction(db, mode) {
        return db.transaction([storeName], mode).objectStore(storeName);
    }

    async function checkVisitedPage(db, url) {
        const store = getTransaction(db, readMode);
        const getRequest = store.get(url);

        return new Promise((resolve, reject) => {
            getRequest.onsuccess = () => resolve(!!getRequest.result); // Operation succeeded
            getRequest.onerror = () => reject('Error accessing the database'); // Runs if the operation above fails
        });
    }

    async function addCurrentPage(db, url) {
        const store = getTransaction(db, writeMode);

        return new Promise((resolve, reject) => {
            const request = store.add({ url });

            request.onsuccess = () => resolve(); // If successful, resolve without any value
            request.onerror = () => reject('Error adding the URL to the database'); // Runs if the operation above fails
        });
    }

    function addWarning() {
        const warningDiv = document.createElement('div');
        warningDiv.style.position = 'fixed';
        warningDiv.style.top = '10px';
        warningDiv.style.left = '10px';
        warningDiv.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';
        warningDiv.style.color = 'white';
        warningDiv.style.padding = '10px';
        warningDiv.style.zIndex = '9999';
        warningDiv.innerText = 'You have already visited this page!';
        document.body.appendChild(warningDiv);
        console.log('Visited URL warning was added');
    }

    async function run() {
        try {
            const currentUrl = window.location.href;
            const db = await getDatabaseConnection();

            const isVisited = await checkVisitedPage(db, currentUrl);

            if (isVisited) {
                console.log('URL already visited');
                addWarning();
                return;
            }

            await addCurrentPage(db, currentUrl);
            console.log('URL added to the database');
        } catch (error) {
            console.error('Error:', error);
        }
    }

    run();


})();