Chan SankakuComplex URL Verifier with IndexedDB

Check if the URL has been accessed before and signaling to the user

Stan na 20-12-2024. Zobacz najnowsza wersja.

// ==UserScript==
// @name         Chan SankakuComplex URL Verifier with IndexedDB
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Check if the URL has been accessed before and signaling to the user
// @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 MI
// ==/UserScript==

(function() {

    console.log('Starting execution of the visited URLs persister...');
    const dbName = 'VisitedDB';

    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('visitedURLs')) {
                    db.createObjectStore('visitedURLs', { 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 initiateDbReadTransaction(db) {
        const transaction = db.transaction(['visitedURLs'], 'readonly');
        return transaction.objectStore('visitedURLs');
    }

    function initiateDbWriteTransaction(db) {
        const transaction = db.transaction(['visitedURLs'], 'readwrite');
        return transaction.objectStore('visitedURLs');
    }

    async function checkVisitedPage(db, url) {
        const store = initiateDbReadTransaction(db);
        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 = initiateDbWriteTransaction(db);

        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();


})();