討論 » 建立請求

请大佬开发知乎站内回答排名显示

§
發表於:2024-05-30

求大佬开发知乎个人回答排名显示插件,之前“新知魔方”可以显示,目前停止运营了,跪求

§
發表於:2024-05-31

大佬何在,急求

§
發表於:2024-05-31
編輯:2024-05-31
// ==UserScript==
// @name               自研 - 知乎 - 按赞同排序回答
// @name:en_US         Self-made - zhihu - Function
// @description        [功能描述]
// @description:en_US  [Functional Description]
// @version            1.0.0-Alpha
// @author             CPlayerCHN
// @license            MulanPSL-2.0
// @namespace          https://www.gitlink.org.cn/CPlayerCHN
// @match              https://www.zhihu.com/question/*
// @exclude            http://*/*
// @updateURL          https://www.gitlink.org.cn/attachments/entries/get_file?download_url=https://www.gitlink.org.cn/api/CPlayerCHN/UserScript/raw/GM%2F[脚本英文名;空格用`+`代替]%2Flastest.user.js?ref=master
// @downloadURL        https://www.gitlink.org.cn/attachments/entries/get_file?download_url=https://www.gitlink.org.cn/api/CPlayerCHN/UserScript/raw/GM%2F[脚本英文名;空格用`+`代替]%2Flastest.user.js?ref=master
// @run-at             document-end
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // 定义「侦测器」变量。
    var observer = new MutationObserver(() => {

        // 排序
        let voteButtons = Array.from(document.querySelectorAll(".VoteButton.VoteButton--up")).map((btn) => {

            // 定义「回复项」变量
            let item = btn;

            // 遍历「按钮元素」直至元素为「回复项」
            do {

                item = item.parentNode;

            } while (item.getAttribute("class") !== "List-item");


            // 定义「投票数」变量
            let voteCount = parseInt(btn.textContent.replace(/\u200B赞同 /, ""));

            // 如果「都票数」为 0,就返回正常的 0,而非 NaN
            if(isNaN(voteCount)) {
                voteCount = 0
            }


            // 返回「元素」「投票数」
            return { element: item, voteCount: voteCount };

        }).sort((a, b) => b.voteCount - a.voteCount);

        document.querySelectorAll("#QuestionAnswers-answers .List > div:nth-child(2) .List-item").forEach((elm) => {
            elm.remove();
        });

        // 替换现有回答
        voteButtons.forEach((data, index) => {

            document.querySelector("#QuestionAnswers-answers .List > div:nth-child(2)").insertAdjacentHTML("beforeend", `<div class="List-item" tabindex="0">${data.element.innerHTML}</div>`);

            // 直接加载懒加载图片
            document.querySelectorAll("#QuestionAnswers-answers .List > div:nth-child(2) .List-item img").forEach((elm) => {
                elm.src = elm.getAttribute("data-actualsrc");
            })

        });

    });

    // 配置「侦测器」侦测目标节点。
    observer.observe(document.querySelector("#QuestionAnswers-answers .List > div:nth-child(2) .List-item"), {
        // "subtree": true,
        "childList": true
    });

})();

目前这个版本observer.observe()语句有点问题,如果配置中启用subtree页面会卡死,不启用就不会触发回调。我不知道怎么改它了。

發表回覆

登入以回復