Display Onclick Events as Links (Configurable)

Finds onclick attributes, displays them as links with an option to open in the same tab.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Display Onclick Events as Links (Configurable)
// @namespace    http://tampermonkey.net/
// @version      0.8.3
// @description  Finds onclick attributes, displays them as links with an option to open in the same tab.
// @author       You
// @match        *angusnicneven.com/*
// @grant        none
// ==/UserScript==

// --- SCRIPT CONFIGURATION ---
// Set this to 'true' to open links in a new tab.
// Set this to 'false' to open links in the same tab.
const openLinksInNewTab = false;
// --- END CONFIGURATION ---

(function() {
    'use strict';

    // A debounce function to prevent the script from running too often and freezing the page.
    function debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }

    const findAndDisplayOnclicks = () => {
        // Find or create the display container.
        let container = document.getElementById('onclick-display-container');
        if (!container) {
            container = document.createElement('div');
            container.id = 'onclick-display-container';
            container.style.position = 'fixed';
            container.style.bottom = '10px';
            container.style.right = '10px';
            container.style.backgroundColor = 'rgba(255, 255, 255, 0.95)';
            container.style.border = '1px solid black';
            container.style.padding = '10px';
            container.style.zIndex = '9999';
            container.style.maxHeight = '300px';
            container.style.overflowY = 'auto';
            document.body.appendChild(container);
        }

        const elementsWithOnclick = document.querySelectorAll('[onclick]');

        // Clear previous results.
        container.innerHTML = '';

        if (elementsWithOnclick.length > 0) {
            const heading = document.createElement('h3');
            heading.textContent = `Onclick Events Found: (${elementsWithOnclick.length})`;
            heading.style.marginTop = '0';
            heading.style.marginBottom = '10px';
            heading.style.color = 'black';
            heading.style.fontFamily = 'Arial';
            container.appendChild(heading);

            elementsWithOnclick.forEach((element, index) => {
                const onclickValue = element.getAttribute('onclick');

                // Sanitize the value for the URL.
                const sanitizedOnclickValue = onclickValue
                    .replace(/window\.location=/g, '')
                    .replace(/'/g, '')
                    .replace(/window\.location = /g,'');



                const url = `https://www.angusnicneven.com/${sanitizedOnclickValue}`;

                const linkElement = document.createElement('a');
                linkElement.href = url;
                linkElement.textContent = onclickValue;

                // --- MODIFICATION START ---
                // Set the link target based on the configuration option at the top of the script.
                if (openLinksInNewTab) {
                    linkElement.target = '_blank';
                }
                // --- MODIFICATION END ---

                linkElement.style.color = 'black';
                linkElement.style.fontFamily = 'monospace';
                linkElement.style.fontSize = '16px';
                linkElement.style.wordBreak = 'break-all';

                const lineContainer = document.createElement('div');
                lineContainer.style.borderBottom = '1px solid #ccc';
                lineContainer.style.paddingBottom = '5px';
                lineContainer.style.marginBottom = '5px';

                lineContainer.appendChild(document.createTextNode(`${index + 1}: `));
                lineContainer.appendChild(linkElement);

                container.appendChild(lineContainer);
            });
            container.style.display = 'block';
        } else {
            container.style.display = 'none';
        }
    };

    const debouncedFind = debounce(findAndDisplayOnclicks, 250);

    const observer = new MutationObserver(() => {
        debouncedFind();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true
    });

    document.addEventListener('DOMContentLoaded', findAndDisplayOnclicks);
    window.addEventListener('load', findAndDisplayOnclicks);

})();