Pixiv 自动展开评论折叠/回复

自动帮你点击 Pixiv 评论区的 “查看后续” 与 “查看回复” 按钮,解放双手——

当前为 2023-08-16 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Pixiv 自动展开评论折叠/回复
// @namespace    https://blog.sxjeru.top/
// @version      1.0
// @description  自动帮你点击 Pixiv 评论区的 “查看后续” 与 “查看回复” 按钮,解放双手——
// @author       sxjeru
// @icon         https://blog.sxjeru.top/favicon.ico
// @grant        none
// @license      GPL-3.0
// @match        *://www.pixiv.net/*
// ==/UserScript==

(function() {
  'use strict';

    const targetText = '查看回复';

    var observer = new IntersectionObserver(function(entries) { // 自动点击“查看后续”
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                clickButton(entry.target);
            }
        });
    });

    var observer2 = new IntersectionObserver((entries, observer) => { // 自动点击“查看回复”
        entries.forEach(entry => {
            if (entry.isIntersecting && entry.target.textContent === targetText) {
                setTimeout(function() {
                entry.target.parentNode.click();
                }, 300); // 延迟点击
                observer.unobserve(entry.target);
            }
        });
    }, { rootMargin: '0px', threshold: 0.1 });

    function clickButton(element) {
        var button = document.querySelector('button[aria-controls="' + element.id + '"]');
        if (button) {
            setTimeout(function() {
                button.click();
            }, 300); // 延迟点击

            observer.unobserve(element);
        }
    }

    // 创建一个MutationObserver实例来监视DOM的变化
    var mutationObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                var newElements = mutation.target.querySelectorAll('[id^="expandable-paragraph-"], div');

                for (var i = 0; i < newElements.length; i++) {
                    var element = newElements[i];

                    if (element.id && element.id.startsWith('expandable-paragraph-')) {
                        observer.observe(element);
                    } else if (element.textContent === targetText) {
                        observer2.observe(element);
                    }

                    // 立即检查新元素是否已经在可视区域内
                    var rect = element.getBoundingClientRect();
                    if (rect.top >= 0 && rect.left >= 0 && rect.bottom <= window.innerHeight && rect.right <= window.innerWidth) {
                        if (element.id && element.id.startsWith('expandable-paragraph-')) {
                            clickButton(element);
                        } else if (element.textContent === targetText) {
                            element.click();
                        }
                    }
                }
            }
        });
    });

    // 开始监视整个文档的DOM变化
    mutationObserver.observe(document.body, { childList: true, subtree: true });
})();