Discussions » Creation Requests

需要写一个知乎脚本,望知乎大佬看过来

§
Posted: 17-06-2024
Edited: 17-06-2024

功能需求:
全自动加载所有回答,并收起所有回答,然后F12查找对应用户名前的某一个字符串,显示在用户名后面即可,具体操作方法不方便在此处公开

§
Posted: 20-06-2024

一定要加载所有回答,再往昵称后加入某个字符串吗?监听页面加载然后实时添加如何?

§
Posted: 25-06-2024

一定要加载所有回答,再往昵称后加入某个字符串吗?监听页面加载然后实时添加如何?

也可以的,我手动只能是一直往下拉,一直拉到自己指定的用户名,才能查看到具体数据,如果能用python或者其他代码直接爬出排名更好

§
Posted: 25-06-2024

也可以的,我手动只能是一直往下拉,一直拉到自己指定的用户名,才能查看到具体数据,如果能用python或者其他代码直接爬出排名更好

排名是指「赞同」数吗?

§
Posted: 25-06-2024

也可以的,我手动只能是一直往下拉,一直拉到自己指定的用户名,才能查看到具体数据,如果能用python或者其他代码直接爬出排名更好

排名是指「赞同」数吗?

不是赞同数,打开知乎某个问题后,点击全部回答,有一个默认排序,一个时间排序,需要的是默认排序,然后查询自己的账号排名情况

§
Posted: 25-06-2024

不是赞同数,打开知乎某个问题后,点击全部回答,有一个默认排序,一个时间排序,需要的是默认排序,然后查询自己的账号排名情况

「自己的账号」是指 https://www.zhihu.com/question/[问题号]/answer/[回答号] 回答的号,还是需要指定某个帐号?

§
Posted: 25-06-2024

手动查找就是如图,比较繁琐,如果能爬出字段就能直接获取了

§
Posted: 25-06-2024

代码写好了,我可把它以 MulanPSL-2.0 开源在我的 GitLink 仓库并在这公开代码吗?不行的话可以通过 CPlayerCHN@163.com 交付代码。

§
Posted: 25-06-2024

代码写好了,我可把它以 MulanPSL-2.0 开源在我的 GitLink 仓库并在这公开代码吗?不行的话可以通过 CPlayerCHN@163.com 交付代码。

开源还是保密,作为求助者,您能在百忙之中帮忙写出来已经很幸运了,我无条件支持您的决定,非常感谢您的辛勤付出,已经邮箱联系您

§
Posted: 25-06-2024
// ==UserScript==
// @name               自研 - 知乎 - pgdhao123 定制脚本-240625
// @name:en_US         Self-made - zhihu - Function
// @description        pgdhao123 在 https://greasyfork.org/en/discussions/requests/248224 定制的脚本。
// @description:en_US  Custom script requested by pgdhao123 on https://greasyfork.org/en/discussions/requests/248224.
// @version            1.0.0-Alpha
// @author             CPlayerCHN
// @license            MulanPSL-2.0
// @namespace          https://www.gitlink.org.cn/CPlayerCHN
// @grant              GM_setValue
// @grant              GM_getValue
// @match              https://www.zhihu.com/question/*
// @run-at             document-end
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // 当页面是特定回答页时定义「用户名」「回答页面」数据,并打开对应完整问题回答页。
    if(location.pathname.match("answer") !== null) {

        GM_setValue("username", document.querySelector(".AnswerCard .AuthorInfo-head .UserLink-link").textContent);
        GM_setValue("question", document.querySelector(".QuestionPage meta[itemprop='url']").content);
        location.href = GM_getValue("question");

    // 不然就…
    }else {

        // 定义「找到回答」「建议」函数。
        let found = false,
            advice = false;

        // 定义「元素监听」变量及其回调函数。
        const observer = new MutationObserver(() => {

            // 回答数超过 50 个建议中止运行。
            if(!advice && Number.parseInt(document.querySelector("#QuestionAnswers-answers .List-headerText span").textContent.split(" ")[0].replaceAll(/,/g, "")) >= 50) {

                if(confirm("回答超过或等于 50 条可能需要较长时间才能找到指定回答,需要中止吗?")) {

                    alert("脚本已停止运行。");
                    advice = true;
                    observer.disconnect();
                    return;

                }else {

                    advice = true;

                }

            };

            // 遍历所有回答项的回答者
            document.querySelectorAll(".AuthorInfo-head .UserLink-link").forEach((elm, index) => {

                // 如果找到「回答页面」匹配且找到对应「用户名」,就定义「找到回答」为真、将页面移动到特定回答、弹框提醒排名信息并停止循环。
                if(GM_getValue("question") === location.href && elm.textContent === GM_getValue("username")) {

                    found = true;
                    window.scrollTo(0, (elm.getBoundingClientRect().top + window.pageYOffset - 80));
                    alert(`排名为:${index}`);

                    return;

                // 不然如果「找到回答」为假且没有加载完整回答就,加载更多回答。
                }else if(!found && document.querySelectorAll("#QuestionAnswers-answers .List-item").length < Number.parseInt(document.querySelector("#QuestionAnswers-answers .List-headerText span").textContent.split(" ")[0].replaceAll(/,/g, ""))) {

                    window.scrollTo(0, 0);
                    window.scrollTo(0, (document.body.scrollHeight - 32));

                }

            });

            // 如果「回答页面」不匹配或「找到回答」为真就停止「元素监听」。
            if(GM_getValue("question") !== location.href || found) {

                observer.disconnect();

            };

        });

        // 配置「元素监听」需要监听的元素和参数。
        observer.observe(document.querySelector("#QuestionAnswers-answers"), {
            "childList": true,
            "subtree": true
        });

    };


})();
§
Posted: 25-06-2024
// ==UserScript==
// @name               自研 - 知乎 - pgdhao123 定制脚本-240625
// @name:en_US         Self-made - zhihu - pgdhao123 Custom Script-240625
// @description        pgdhao123 在 https://greasyfork.org/en/discussions/requests/248224 定制的脚本。
// @description:en_US  Custom script requested by pgdhao123 on https://greasyfork.org/en/discussions/requests/248224.
// @version            1.0.1
// @author             CPlayerCHN
// @license            MulanPSL-2.0
// @namespace          https://www.gitlink.org.cn/CPlayerCHN
// @grant              GM_setValue
// @grant              GM_getValue
// @match              https://www.zhihu.com/question/*
// @run-at             document-end
// @noframes
// ==/UserScript==

/* 测试页面
 * https://www.zhihu.com/question/656283575/answer/3501423499 第 35 个回答。
 * https://www.zhihu.com/question/403747458/answer/1432550426 第 28 个回答。
 * */

(function() {
    'use strict';

    // 当页面是特定回答页时定义「用户名」「回答页面」数据,并打开对应完整问题回答页。
    if(location.pathname.match("answer") !== null) {

        GM_setValue("username", document.querySelector(".AnswerCard .AuthorInfo-head .UserLink-link").textContent);
        GM_setValue("question", document.querySelector(".QuestionPage meta[itemprop='url']").content);
        location.href = GM_getValue("question");

    // 不然就…
    }else {

        // 定义「找到回答」函数。
        let found = false;

        // 定义「元素监听」变量及其回调函数。
        const observer = new MutationObserver(() => {

            // 遍历所有回答项的回答者
            document.querySelectorAll(".AuthorInfo-head .UserLink-link").forEach((elm, index) => {

                // 如果找到「回答页面」匹配且找到对应「用户名」,就定义「找到回答」为真、将页面移动到特定回答、弹框提醒排名信息并停止循环。
                if(GM_getValue("question") === location.href && elm.textContent === GM_getValue("username")) {

                    found = true;
                    window.scrollTo(0, (elm.getBoundingClientRect().top + window.pageYOffset - 80));
                    alert(`排名为:${index + 1}`);

                    return;

                // 不然如果「找到回答」为假、排名在 30 以内且没有加载完整回答就,加载更多回答。
                }else if(!found && index <= 30 && document.querySelectorAll("#QuestionAnswers-answers .List-item").length < Number.parseInt(document.querySelector("#QuestionAnswers-answers .List-headerText span").textContent.split(" ")[0].replaceAll(/,/g, ""))) {

                    window.scrollTo(0, 0);
                    window.scrollTo(0, (document.body.scrollHeight - 32));

                }

                // 如果排名超过 30 就停止运行,并弹框提醒。
                if(index >= 30) {

                    observer.disconnect();
                    alert("排名在规定值以外,脚本已停止运行。");
                    return;

                }

            });

            // 如果「回答页面」不匹配或「找到回答」为真就停止「元素监听」。
            if(GM_getValue("question") !== location.href || found) {

                observer.disconnect();

            };

        });

        // 配置「元素监听」需要监听的元素和参数。
        observer.observe(document.querySelector("#QuestionAnswers-answers"), {
            "childList": true,
            "subtree": true
        });

    };


})();

更具要求,移除「超过 50 条回答」建议,增加「超过 30 条」就停止脚本功能,修正排名显示弹框数值错误(确实错了,不该嘴犟的 ; )。

§
Posted: 25-06-2024

非常感谢大佬,膜拜

§
Posted: 25-06-2024

发现两个问题,应该能提高查询效率
1、因为每一个回答都展开了,导致网页加载速度变慢,然后知乎会卡住不动,需要动一下鼠标才会继续往下拉,这个问题可以参考“知乎增强”的默认收起回答,这样或许可以解决卡住的问题
2、超出30个之后的弹窗需要点5次才能点掉

再麻烦大佬更新一下,谢谢,如果太晚了,抽空更新也可以的,不急

§
Posted: 25-06-2024
Edited: 25-06-2024

非常感谢大佬,膜拜

嗯,后续有什么对这个脚本需要可以和我联系。需要其他脚本的话可以发个新帖,我没事就会在这个板块逛。

§
Posted: 25-06-2024

非常感谢大佬,膜拜

嗯,后续有什么对这个脚本需要可以和我联系。需要其他脚本的话可以发个新帖,我没事就会在这个板块逛。

好的,非常感谢

§
Posted: 25-06-2024
// ==UserScript==
// @name               自研 - 知乎 - pgdhao123 定制脚本-240625
// @name:en_US         Self-made - zhihu - pgdhao123 Custom Script-240625
// @description        pgdhao123 在 https://greasyfork.org/en/discussions/requests/248224 定制的脚本。
// @description:en_US  Custom script requested by pgdhao123 on https://greasyfork.org/en/discussions/requests/248224.
// @version            1.0.2
// @author             CPlayerCHN
// @license            MulanPSL-2.0
// @namespace          https://www.gitlink.org.cn/CPlayerCHN
// @grant              GM_setValue
// @grant              GM_getValue
// @match              https://www.zhihu.com/question/*
// @run-at             document-end
// @noframes
// ==/UserScript==

/* 测试页面
 * https://www.zhihu.com/question/656283575/answer/3501423499 第 35 个回答。
 * https://www.zhihu.com/question/403747458/answer/1325630308 第 26 个回答。
 * */

(function() {
    'use strict';

    // 当页面是特定回答页时定义「用户名」「回答页面」数据,并打开对应完整问题回答页。
    if(location.pathname.match("answer") !== null) {

        GM_setValue("username", document.querySelector(".AnswerCard .AuthorInfo-head .UserLink-link").textContent);
        GM_setValue("question", document.querySelector(".QuestionPage meta[itemprop='url']").content);
        location.href = GM_getValue("question");

    // 不然就…
    }else {

        // 定义「找到回答」「建议」函数。
        let found = false,
            advice = false;

        // 定义「元素监听」变量及其回调函数。
        const observer = new MutationObserver(() => {

            // 遍历所有「收起」按钮,如果有「收起按钮」且元素文本内容是「收起」就收起文章内容。
            document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                if(elm.textContent === "收起​") {

                    elm.click();

                }

            });

            // 遍历所有回答项的回答者
            document.querySelectorAll(".AuthorInfo-head .UserLink-link").forEach((elm, index) => {

                // 如果找到「回答页面」匹配且找到对应「用户名」,就展开所有文章内容、定义「找到回答」为真、将页面移动到特定回答、弹框提醒排名信息并停止循环。
                if(GM_getValue("question") === location.href && elm.textContent === GM_getValue("username")) {

                    document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                        if(elm.textContent === "展开阅读全文​") {

                            elm.click();

                        }

                    });

                    found = true;
                    window.scrollTo(0, (elm.getBoundingClientRect().top + window.pageYOffset - 80));
                    alert(`排名为:${index + 1}`);
                    return;

                // 不然如果「找到回答」为假、排名在 30 以内且没有加载完整回答就,加载更多回答。
                }else if(!found && index <= 30 && document.querySelectorAll("#QuestionAnswers-answers .List-item").length < Number.parseInt(document.querySelector("#QuestionAnswers-answers .List-headerText span").textContent.split(" ")[0].replaceAll(/,/g, ""))) {

                    window.scrollTo(0, 0);
                    window.scrollTo(0, (document.body.scrollHeight - 32));

                }

                // 如果没有弹出过「建议」弹框且排名超过 30 就停止运行,就展开所有文章内容、定义「建议」为真并弹框提醒、停止「元素监听」并停止循环。
                if(!advice && index >= 30) {

                    document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                        if(elm.textContent === "展开阅读全文​") {

                            elm.click();

                        }

                    });

                    advice = true;
                    observer.disconnect();
                    alert("排名在规定值以外,脚本已停止运行。");
                    return;

                }

            });

            // 如果「回答页面」不匹配或「找到回答」为真就停止「元素监听」。
            if(GM_getValue("question") !== location.href || found) {

                observer.disconnect();

            };

        });

        // 配置「元素监听」需要监听的元素和参数。
        observer.observe(document.querySelector("#QuestionAnswers-answers"), {
            "childList": true,
            "subtree": true
        });

    };


})();

更具要求,默认折叠所有回答直至脚本停止运行,修正「建议」弹框重复弹出的问题。

§
Posted: 26-06-2024
// ==UserScript==
// @name               自研 - 知乎 - pgdhao123 定制脚本-240625
// @name:en_US         Self-made - zhihu - pgdhao123 Custom Script-240625
// @description        pgdhao123 在 https://greasyfork.org/en/discussions/requests/248224 定制的脚本。
// @description:en_US  Custom script requested by pgdhao123 on https://greasyfork.org/en/discussions/requests/248224.
// @version            1.0.3
// @author             CPlayerCHN
// @license            MulanPSL-2.0
// @namespace          https://www.gitlink.org.cn/CPlayerCHN
// @grant              GM_setValue
// @grant              GM_getValue
// @match              https://www.zhihu.com/question/*
// @run-at             document-end
// @noframes
// ==/UserScript==

/* 测试页面
 * https://www.zhihu.com/question/656283575/answer/3501423499 第 35 个回答。
 * https://www.zhihu.com/question/403747458/answer/1325630308 第 26 个回答。
 * */

(function() {
    'use strict';

    // 当页面是特定回答页时定义「用户名」「回答页面」数据,并打开对应完整问题回答页。
    if(location.pathname.match("answer") !== null) {

        GM_setValue("username", document.querySelector(".AnswerCard .AuthorInfo-head .UserLink-link").textContent);
        GM_setValue("question", document.querySelector(".QuestionPage meta[itemprop='url']").content);
        location.href = GM_getValue("question");

    // 不然就…
    }else {

        // 定义「找到回答」「建议」函数。
        let found = false,
            advice = false;

        // 定义「元素监听」变量及其回调函数。
        const observer = new MutationObserver(() => {

            //  如果找到「回答页面」匹配,就遍历所有「收起」按钮,如果有「收起按钮」且元素文本内容是「收起」就收起文章内容。
            if(GM_getValue("question") === location.href) {

                document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                    if(elm.textContent === "收起​") {

                        elm.click();

                    }

                });

            }

            // 遍历所有回答项的回答者
            document.querySelectorAll(".AuthorInfo-head .UserLink-link").forEach((elm, index) => {

                // 如果找到「回答页面」匹配且找到对应「用户名」,就展开所有文章内容、定义「找到回答」为真、将页面移动到特定回答、弹框提醒排名信息并停止循环。
                if(GM_getValue("question") === location.href && elm.textContent === GM_getValue("username")) {

                    document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                        if(elm.textContent === "展开阅读全文​") {

                            elm.click();

                        }

                    });

                    found = true;
                    window.scrollTo(0, (elm.getBoundingClientRect().top + window.pageYOffset - 80));
                    alert(`排名为:${index + 1}`);
                    return;

                // 不然如果「找到回答」为假、排名在 30 以内且没有加载完整回答就,加载更多回答。
                }else if(!found && index <= 30 && document.querySelectorAll("#QuestionAnswers-answers .List-item").length < Number.parseInt(document.querySelector("#QuestionAnswers-answers .List-headerText span").textContent.split(" ")[0].replaceAll(/,/g, ""))) {

                    window.scrollTo(0, 0);
                    window.scrollTo(0, (document.body.scrollHeight - 32));

                }

                // 如果没有弹出过「建议」弹框且排名超过 30 就停止运行,就展开所有文章内容、定义「建议」为真并弹框提醒、停止「元素监听」并停止循环。
                if(!advice && index >= 30) {

                    document.querySelectorAll(".List-item .ContentItem-rightButton").forEach((elm) => {

                        if(elm.textContent === "展开阅读全文​") {

                            elm.click();

                        }

                    });

                    advice = true;
                    observer.disconnect();
                    alert("排名在规定值以外,脚本已停止运行。");
                    return;

                }

            });

            // 如果「回答页面」不匹配或「找到回答」为真就停止「元素监听」。
            if(GM_getValue("question") !== location.href || found) {

                observer.disconnect();

            };

        });

        // 配置「元素监听」需要监听的元素和参数。
        observer.observe(document.querySelector("#QuestionAnswers-answers"), {
            "childList": true,
            "subtree": true
        });

    };


})();

修正在所有回答页都会收起回答的问题。

§
Posted: 26-06-2024

感谢大佬再次更新,谢谢

§
Posted: 26-06-2024

感谢大佬再次更新,谢谢

嗯。好再来。

Post reply

Sign in to post a reply.