知乎段落引用和图片引用

在知乎段落和图片后添加灰色数字,用于复制引用和引用数

// ==UserScript==
// @name         知乎段落引用和图片引用
// @namespace    https://gf.qytechs.cn/scripts/499296
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAFo0lEQVR4nJWXT4hlVxHGf9/tJyYuzJtxIziQN8RBFDEtIWB0MW+Mi4CIk4UuAtqvAxPcqAkJRNxMshDUTc8s3TjdO3c9QbIKod+o4CIuZtRFDEi3EIgEM/02jpPIPZ+LqnPv6TeB4IHLPfeeP1X11VdV54i2vewtOhaITcQDdEgdthAdINAGNkgd0IEVbwR17rAmx1TniSVil6e0V0UKgJc8pWdfG5yvwqRhs1FQIyQ3trvcpxsVGd4xf/gelO24yV0usK1VB6APOMDMKSADBShIBWNwj1WwjClAzLEL0CMc86jj9cl/LgRs8ZjCJvexH5r/xAuJa260TatG6DosIee4lHO0ZnWDXH5bgYQ8uql122Iis2Xnj4JtRH675AIHMAQKuAsdrLV3dYXTwQ7Bir1jrzJ+Wyw698yVkLmgxgVg+PGjcPw8zD6JKcgFUcD94AoNa0eXkO600lVqXOPqPrPZNT6tjylYBRZfgp0n4GgFR7dDkEieeNys6ZPCBq4MCicXdFLWdIKBHtMFfC4JY8Hf/nxEyY0jmD+IM7Cgy77gxtto+jF4+NMj8+tY84YOLd8JnZJPpiDxI7shUigimJ2Gwxf5yLb9aryvffOj5+pXKbzJDxOVoIw7REkLhedno7+6CzffgdkpmE3z+5/Nrq7JJMfejc3bNj+TnT4tTyRtmLiAlARKliK09Uis2fsTPPcqvvwNdPlxuP4mbO+P4aQObz0cG958F77+mxMJh9kDcPh0IpCcUIerqycYuc8FGX7zczB/CI6O4cofAA9GjuRjUFhN2I0hmISlNFCUzCGMSkxqfFJw3XD6cXjuOt57A1YfpM/KSSEqY9zLo4w0psLsVnknyhkVYgNPMlRQh+qE63+GSsaBoK2VJcdTKTcKDLmhA/o1BDKFKzKl3aOJcoNBu3SHAAe4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制引用和引用数
// @author       wzj042
// @match        *://www.zhihu.com/question/*
// @match        *://zhuanlan.zhihu.com/p/*
// @grant        GM_setClipboard
// @require      https://code.jquery.com/jquery-3.6.0.min.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to show toast message
    function showToast(message) {
        const toast = $('<div>').text(message).css({
            'position': 'fixed',
            'bottom': '20px',
            'left': '50%',
            'transform': 'translateX(-50%)',
            'background-color': 'rgba(0, 0, 0, 0.7)',
            'color': 'white',
            'padding': '10px 20px',
            'border-radius': '5px',
            'z-index': '1000',
            'font-size': '14px'
        }).hide().fadeIn(400);

        $('body').append(toast);

        setTimeout(() => {
            toast.fadeOut(400, () => {
                toast.remove();
            });
        }, 2000);
    }

    function addReferenceNumbers() {
        $('.RichContent, .Post-RichTextContainer').each(function() {
            let refIndex = 1;

            $(this).find('.RichText p:not(.ztext-empty-paragraph), .RichText figure, .RichText h1, .RichText h2, .RichText h3, .RichText blockquote, .RichText ol, .RichText ul').each(function() {
                if ($(this).is('p') && $(this).text().trim().length === 0) {
                    return; // Skip empty paragraphs
                }
                if (!$(this).find('.ref-number').length) {
                    var refNumber = $('<span>').text(` [${refIndex}]`).css({
                        'color': 'gray',
                        'cursor': 'pointer',
                        'user-select': 'none',
                        'margin-left': '5px'
                    }).addClass('ref-number');

                    refNumber.click(function() {
                        var refIndexText = $(this).text().match(/\d+/)[0];
                        var paragraphText;
                        if ($(this).parent().is('figure')) {
                            paragraphText = `图片[${refIndexText}]`;
                        } else {
                            paragraphText = $(this).parent().text().trim();
                        }
                        var copyText = `> ${paragraphText}\n`;
                        GM_setClipboard(copyText);
                        showToast('已复制引用:\n' + copyText);
                    });

                    $(this).append(refNumber);
                    refIndex++;
                }
            });
        });
    }

    // Use MutationObserver to watch for changes in the RichContent element
    function observeContentChanges() {
        const targetNodes = document.querySelectorAll('.RichContent, .Post-RichTextContainer');
        if (!targetNodes.length) return;

        const config = { childList: true, subtree: true };

        const callback = function(mutationsList, observer) {
            for (let mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    addReferenceNumbers();
                }
            }
        };

        targetNodes.forEach((node) => {
            const observer = new MutationObserver(callback);
            observer.observe(node, config);
        });

        // Initial call to add numbers to already existing content
        addReferenceNumbers();
    }

    $(document).ready(function() {
        observeContentChanges();
    });

})();

QingJ © 2025

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