URL Verifier with IndexedDB

Check if the URL has been accessed before and signal it to the user. With this script you can know if a specific page has been accessed before. If you have never accessed a URL, it will persist it in IndexedDB, if you have already accessed it, when you access it again a warning message will be added to your screen. I personally use this script to check if I have already accessed a specific page of an adult image/video site, to avoid downloading them again unnecessarily. Use your creativity.

Od 20.12.2024.. Pogledajte najnovija verzija.

// ==UserScript==
// @name         URL Verifier with IndexedDB
// @namespace    http://tampermonkey.net/
// @version      0.12
// @description  Check if the URL has been accessed before and signal it to the user. With this script you can know if a specific page has been accessed before. If you have never accessed a URL, it will persist it in IndexedDB, if you have already accessed it, when you access it again a warning message will be added to your screen. I personally use this script to check if I have already accessed a specific page of an adult image/video site, to avoid downloading them again unnecessarily. Use your creativity.
// @author       mtpontes
// @grant        none
// @license MI

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