知乎答主MCN信息显示

通过一个按钮,让知乎用户的信息栏显示MCN机构信息

目前为 2024-10-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         知乎答主MCN信息显示
// @namespace    https://www.lslby.com/
// @version      1.0
// @description  通过一个按钮,让知乎用户的信息栏显示MCN机构信息
// @author       伯Yeah
// @match        *://www.zhihu.com/question/*
// @match        *://www.zhihu.com/people/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_openInTab
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 添加按钮样式
    GM_addStyle(`
        .mcn-button {
            margin-left: 8px;
            padding: 2px 8px;
            font-size: 12px;
            color: #8590a6;
            background: none;
            border: 1px solid #8590a6;
            border-radius: 3px;
            cursor: pointer;
        }
        .mcn-button:hover {
            color: #76839b;
            border-color: #76839b;
        }
        .mcn-info {
            color: #999;
            font-size: 14px;
            margin-left: 5px;
        }
    `);

    // 存储正在处理的用户ID,防止重复获取
    const processingUsers = new Set();

    // 获取MCN信息并更新显示
    async function updateMCNDisplay(userId) {
        const mcnInfo = GM_getValue(userId);
        if (!mcnInfo) return;

        // 查找所有包含该用户ID的回答
        const answers = document.querySelectorAll('.List-item');
        answers.forEach(answer => {
            const urlMeta = answer.querySelector('meta[itemprop="url"]');
            if (!urlMeta || !urlMeta.content.includes(userId)) return;

            const nameElement = answer.querySelector('.AuthorInfo-name');
            if (!nameElement) return;

            // 移除旧的MCN信息(如果存在)
            const oldMcnInfo = nameElement.querySelector('.mcn-info');
            if (oldMcnInfo) oldMcnInfo.remove();

            // 添加新的MCN信息
            if (mcnInfo) {
                const mcnElement = document.createElement('span');
                mcnElement.className = 'mcn-info';
                mcnElement.textContent = `(MCN: ${mcnInfo})`;
                nameElement.appendChild(mcnElement);
            }
        });
    }

    // 获取MCN信息的函数
    async function fetchMCNInfo(userId, mcnButton) {
        // 如果正在处理该用户,返回
        if (processingUsers.has(userId)) {
            return;
        }

        // 标记该用户正在处理中
        processingUsers.add(userId);
        mcnButton.textContent = '获取中...';
        mcnButton.disabled = true;

        const tab = GM_openInTab(`https://www.zhihu.com/people/${userId}?autoOpened=true`, {
            active: false,
            insert: true
        });

        // 创建一个定时器检查MCN信息是否已更新
        const checkInterval = setInterval(() => {
            const mcnInfo = GM_getValue(userId);
            if (mcnInfo !== undefined) {
                clearInterval(checkInterval);
                processingUsers.delete(userId);
                mcnButton.textContent = '记录MCN';
                mcnButton.disabled = false;
                updateMCNDisplay(userId);
            }
        }, 500);

        // 设置超时,防止页面加载失败
        setTimeout(() => {
            clearInterval(checkInterval);
            processingUsers.delete(userId);
            mcnButton.textContent = '记录MCN';
            mcnButton.disabled = false;
        }, 10000);
    }

    // 在用户页面获取并保存MCN信息
    async function handlePeoplePage() {
        if (!window.location.pathname.startsWith('/people/')) {
            return;
        }

        const userId = window.location.pathname.split('/').pop();

        // 检查是否是通过脚本打开的页面
        const urlParams = new URLSearchParams(window.location.search);
        const isAutoOpened = urlParams.get('autoOpened') === 'true';

        // 等待页面加载完成
        setTimeout(async () => {
            // 查找展开详细资料的按钮
            const expandButton = document.querySelector('.ProfileHeader-expandButton');
            if (expandButton) {
                expandButton.click();
            }

            // 等待MCN信息加载
            setTimeout(() => {
                const mcnElements = document.querySelectorAll('.ProfileHeader-detailItem');
                let mcnInfo = '';

                for (const element of mcnElements) {
                    if (element.textContent.includes('MCN 机构')) {
                        const mcnValue = element.querySelector('.ProfileHeader-detailValue');
                        if (mcnValue) {
                            mcnInfo = mcnValue.textContent.trim();
                            break;
                        }
                    }
                }

                // 保存MCN信息
                GM_setValue(userId, mcnInfo);

                // 只有在页面是通过脚本自动打开的情况下才关闭页面
                if (isAutoOpened) {
                    window.close();
                }
            }, 1000);
        }, 1000);
    }

    // 在问题页面处理回答
    async function handleQuestionPage() {
        if (!window.location.pathname.startsWith('/question/')) {
            return;
        }

        // 处理单个回答
        function processAnswer(answer) {
            if (answer.classList.contains('processed-mcn')) {
                return;
            }

            const authorInfo = answer.querySelector('.AuthorInfo');
            if (!authorInfo) {
                return;
            }

            const urlMeta = authorInfo.querySelector('meta[itemprop="url"]');
            if (!urlMeta) {
                return;
            }

            const userId = urlMeta.content.split('/').pop();
            answer.classList.add('processed-mcn');

            const nameElement = authorInfo.querySelector('.AuthorInfo-name');
            if (nameElement && !nameElement.querySelector('.mcn-button')) {
                // 创建MCN按钮
                const mcnButton = document.createElement('button');
                mcnButton.className = 'mcn-button';
                mcnButton.textContent = '记录MCN';
                mcnButton.onclick = () => fetchMCNInfo(userId, mcnButton);
                nameElement.appendChild(mcnButton);

                // 如果已有MCN信息,显示出来
                const mcnInfo = GM_getValue(userId);
                if (mcnInfo) {
                    const mcnElement = document.createElement('span');
                    mcnElement.className = 'mcn-info';
                    mcnElement.textContent = `(MCN: ${mcnInfo})`;
                    nameElement.appendChild(mcnElement);
                }
            }
        }

        // 处理已有的回答
        const initialAnswers = document.querySelectorAll('.List-item');
        for (const answer of initialAnswers) {
            processAnswer(answer);
        }

        // 监听新加载的回答
        const observer = new MutationObserver((mutations) => {
            const answers = document.querySelectorAll('.List-item:not(.processed-mcn)');
            answers.forEach(processAnswer);
        });

        observer.observe(document.querySelector('.List') || document.body, {
            childList: true,
            subtree: true
        });
    }

    // 根据页面类型执行相应的处理
    if (window.location.pathname.startsWith('/people/')) {
        handlePeoplePage();
    } else if (window.location.pathname.startsWith('/question/')) {
        handleQuestionPage();
    }
})();

QingJ © 2025

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