隐藏知乎网页中的知乎直达外链

Replace specific links with text-only spans.

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

// ==UserScript==
// @name         隐藏知乎网页中的知乎直达外链
// @name:en      Remove Zhida Link and ZDI--FourPointedStar at Zhihu Site
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replace specific links with text-only spans.
// @description:en  Replace specific links with text-only spans.
// @author       aspen138
// @match        *://www.zhihu.com/*
// @grant        none
// @license      MIT
// ==/UserScript==


// test case:
// 有哪些历史上著名的合影? - 施耐泽的回答 - 知乎 https://www.zhihu.com/question/23674814/answer/25403040



(function() {
    'use strict';

    // Variable to track the script's enabled/disabled state
    let scriptEnabled = true;

    // Function to replace links with spans
    function replaceLinks(node) {
        if (!scriptEnabled) return;
        // Select all <a> elements with the specific classes within the node
        const links = node.querySelectorAll('a.RichContent-EntityWord.css-b7erz1');

        links.forEach(function(link) {
            // Create a new <span> element
            const span = document.createElement('span');

            // Copy the inner content from the link to the span
            span.innerHTML = link.innerHTML;

            // Replace the <a> element with the new <span> element
            link.parentNode.replaceChild(span, link);
        });
    }

    // Function to hide star SVG icons
    function hideStarSVGs(node) {
        if (!scriptEnabled) return;
        // Select all <svg> elements with the specific classes within the node
        const svgs = node.querySelectorAll('svg.ZDI--FourPointedStar16');

        svgs.forEach(function(svg) {
            // Remove the SVG element from the DOM
            svg.parentNode.removeChild(svg);
            // Alternatively, you can hide the SVG by setting its display to none
            // svg.style.display = 'none';
        });
    }

    // Function to process both links and SVGs
    function processNode(node) {
        replaceLinks(node);
        hideStarSVGs(node);
    }

    // Initial processing on page load
    processNode(document);

    // Create a MutationObserver to monitor the DOM for changes
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                // Only process element nodes
                if (node.nodeType === Node.ELEMENT_NODE) {
                    // Process links and SVGs in the newly added node
                    processNode(node);
                }
            });
        });
    });

    // Start observing the document body for added nodes
    observer.observe(document.body, { childList: true, subtree: true });

    // Function to add the status button
    function addStatusButton() {
        // Wait for the existing buttons to be loaded
        const observerForButtons = new MutationObserver(function(mutations, obs) {
            const cornerFlex = document.querySelector('.CornerAnimayedFlex');
            if (cornerFlex) {
                // Stop observing once the element is found
                obs.disconnect();

                // Create the status button
                const statusButton = document.createElement('button');
                statusButton.id = 'toggle-script-button';
                statusButton.type = 'button';
                statusButton.className = 'Button CornerButton Button--plain css-4lspwd';
                statusButton.style.marginBottom = '10px'; // Add some spacing

                // Set the initial tooltip and label
                statusButton.setAttribute('data-tooltip', '关闭脚本功能');
                statusButton.setAttribute('data-tooltip-position', 'left');
                statusButton.setAttribute('aria-label', '关闭脚本功能');

                // Create an SVG icon for the button (optional)
                const svgNS = 'http://www.w3.org/2000/svg';
                const svg = document.createElementNS(svgNS, 'svg');
                svg.setAttribute('width', '24');
                svg.setAttribute('height', '24');
                svg.setAttribute('viewBox', '0 0 24 24');
                svg.setAttribute('fill', 'currentColor');
                // Add custom SVG path for the icon
                const path = document.createElementNS(svgNS, 'path');
                path.setAttribute('d', 'M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zM11 14h2v2h-2v-2zm0-8h2v6h-2V6z');
                svg.appendChild(path);

                statusButton.appendChild(svg);

                // Add click event listener to toggle the script's functionality
                statusButton.addEventListener('click', function() {
                    scriptEnabled = !scriptEnabled;
                    if (scriptEnabled) {
                        statusButton.setAttribute('data-tooltip', '关闭脚本功能');
                        statusButton.setAttribute('aria-label', '关闭脚本功能');
                        // Optionally, change the icon to indicate "on" state
                        path.setAttribute('d', 'M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zM11 14h2v2h-2v-2zm0-8h2v6h-2V6z');
                        // Re-process the document to apply changes
                        processNode(document);
                    } else {
                        statusButton.setAttribute('data-tooltip', '开启脚本功能');
                        statusButton.setAttribute('aria-label', '开启脚本功能');
                        // Optionally, change the icon to indicate "off" state
                        path.setAttribute('d', 'M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm-1 14h2v2h-2v-2zm0-8h2v6h-2V8z');
                        // Optionally, restore the original content (refresh the page or implement reverse logic)
                    }
                });

                // Insert the status button above the existing buttons
                cornerFlex.insertBefore(statusButton, cornerFlex.firstChild);
            }
        });

        // Start observing the document body to find the target element
        observerForButtons.observe(document.body, { childList: true, subtree: true });
    }

    // Call the function to add the status button
    addStatusButton();

})();

QingJ © 2025

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