Add redirect confirmation to reddit links (written for old)

Confirm navigation for links containing 'user', 'permalink', or 'parent' on Reddit

目前为 2024-07-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         Add redirect confirmation to reddit links (written for old)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Confirm navigation for links containing 'user', 'permalink', or 'parent' on Reddit
// @author       cgpt
// @match        *://*.reddit.com/*
// @grant        none
// @license MIT 
// ==/UserScript==

(function() {
    'use strict';

    // Function to create and show the modal
    function showModal(link) {
        // Create the modal container
        const modal = document.createElement('div');
        modal.style.position = 'fixed';
        modal.style.left = '0';
        modal.style.top = '0';
        modal.style.width = '100vw';
        modal.style.height = '100vh';
        modal.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
        modal.style.display = 'flex';
        modal.style.justifyContent = 'center';
        modal.style.alignItems = 'center';
        modal.style.zIndex = '10000';

        // Function to remove the modal
        function removeModal() {
            document.body.removeChild(modal);
        }

        // Event listener for modal close scenarios
        modal.addEventListener('click', function(event) {
            if (event.target === modal) {
                removeModal();
            }
        });

        // Escape key handler
        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                removeModal();
            }
        }, { once: true }); // Automatically removes listener after first use

        // Create the modal content
        const modalContent = document.createElement('div');
        modalContent.style.padding = '20px';
        modalContent.style.backgroundColor = 'white';
        modalContent.style.borderRadius = '5px';
        modalContent.innerText = `Navigate to ${link.href}?`;

        // Create Yes button
        const yesButton = document.createElement('button');
        yesButton.innerText = 'Yes';
        yesButton.onclick = function() {
            window.location.href = link.href;
        };

        // Create No button
        const noButton = document.createElement('button');
        noButton.innerText = 'No';
        noButton.onclick = removeModal;

        modalContent.appendChild(yesButton);
        modalContent.appendChild(document.createTextNode(' ')); // Spacer
        modalContent.appendChild(noButton);

        modal.appendChild(modalContent);
        document.body.appendChild(modal);
    }

    // Event listener for all 'a' elements
    document.addEventListener('click', function(e) {
        const target = e.target.closest('a');
        if (target && (target.href.includes('user') || target.textContent.toLowerCase().includes('permalink') || target.textContent.toLowerCase().includes('parent'))) {
            e.preventDefault();
            showModal(target);
        }
    }, true);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址