pixiv 自動查看差分和評論

pixiv 自動點擊圖片的查看全部和評論的瀏覽更多按鈕

// ==UserScript==
// @name         pixiv 自動查看差分和評論
// @namespace    pixiv 自動查看差分和評論
// @version      7.7.5
// @description  pixiv 自動點擊圖片的查看全部和評論的瀏覽更多按鈕
// @author       fmnijk
// @match        https://www.pixiv.net/*
// @icon         https://www.google.com/s2/favicons?domain=pixiv.net
// @grant        none
// @license      MIT
// ==/UserScript==

/* $ and $$ */
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);

/*----force listen to locationchange work start----*/
history.pushState = ( f => function pushState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('pushstate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.pushState);

history.replaceState = ( f => function replaceState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('replacestate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.replaceState);

window.addEventListener('popstate',()=>{
    window.dispatchEvent(new Event('locationchange'))
});
/*----force listen to locationchange work end----*/

function keeptrying(times, delay) {
    if(times == 0){
        return false;
    }

    /*插圖*/
    //$('.sc-emr523-0.guczbC > .sc-emr523-2.drFRmD')?.click();
    let e = $('.sc-emr523-0.guczbC > .sc-emr523-2.drFRmD');
    let E = ['查看全部', '모두 보기', 'See all', 'すべて見る'];
    if(E.includes(e?.innerText)){
        e?.click();
    }

    function getTextContent(selector) {
        const elements = document.querySelectorAll(selector);
        const texts = Array.from(elements)
        .map(elem => elem.textContent)
        .join('');

        console.log(texts);
    }
    //getTextContent('.sc-eyxzap-2 > .sc-eyxzap-3');
    //getTextContent('.sc-4a5gah-0 > .sc-4a5gah-1');

    /*方法一*/
    const keywords = ['閱讀後續', '瀏覽更多', '查看回復', '查看歷史',
                      '查看后续', '浏览更多', '查看回复', '查看历史'];
    function clickIfContainsKeywords(selector) {
        const elements = document.querySelectorAll(selector);

        elements.forEach(element => {
            const text = element.textContent.toLowerCase();
            if (keywords.some(keyword => text.includes(keyword.toLowerCase()))) {
                element.click();
            }
        });
    }
    clickIfContainsKeywords('.sc-eyxzap-2 > .sc-eyxzap-3');
    clickIfContainsKeywords('.sc-4a5gah-0 > .sc-4a5gah-1');

    /*方法二*/
    /*簡介*/
    $('.sc-eyxzap-2.dWySmx > .sc-eyxzap-3.cVAEeh')?.click();
    /*評論*/
    $('.sc-4a5gah-0.EdYfe > .sc-4a5gah-1.kHyYuA')?.click();
    /*超長評論(閱讀後續)*/
    $('.sc-eyxzap-2.hiQjnA > .sc-eyxzap-3.dcYRFc')?.click();
    /*評論的評論*/
    $('.sc-4a5gah-0.iuLyik > .sc-4a5gah-1.kHyYuA')?.click();

    setTimeout(( () => keeptrying(times - 1, delay) ), delay);
}

(window.onload = function () {
    'use strict';

    keeptrying(100, 50);

    window.addEventListener('locationchange', function (){
        keeptrying(100, 50);
    })
})();