lihkg_rotom / LIHKG 自動不推文

// ==UserScript==
// @name         LIHKG 自動不推文
// @namespace    lihkg_nopush
// @version      191116
// @description  於某主題開啟或關閉不推文回覆後,會保留該設定一個月或直至你手動關閉
// @author       洛托姆圖鑑
// @license      MIT
// @match        https://lihkg.com/*
// @icon         https://lihkg.com/assets/faces/pig/shocked.gif
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_info
// ==/UserScript==

(function () {
    "use strict";
    var real_click = false;

    var current_version = parseInt(GM_info.script.version);
    var storage_version = GM_getValue('version');
    if (!storage_version) {
        GM_setValue('version', current_version);
    }
    if (current_version > storage_version) {

    }

    const url_match = /^\/thread\/(\d+)/;
    const saveload = function (mode, thread) {
        var t = Math.round((new Date()).getTime() / 1000);
        var v = GM_listValues();
        switch (mode){
            case 'save':
                GM_setValue(thread, JSON.stringify([t, document.querySelector('._2k_IfadJWjcLJlSKkz_R2->span').innerText, true]));
                v.forEach(function (i) {
                    if ((i != 'version') && (t - JSON.parse(GM_getValue(i))[0] >= 2592000)) {
                        GM_deleteValue(i);
                    }
                });
                break;
            case 'load':
                return GM_getValue(thread)?JSON.parse(GM_getValue(thread))[2]:false;
                break;
            case 'del':
                GM_setValue(thread, JSON.stringify([-1, '', false]));
                GM_deleteValue(thread);
                break;
            case 'nuke':
                v.forEach(function (i) {
                    if (i != 'version') {
                        GM_deleteValue(i);
                    }
                });
                break;
        }
    };

    const mousecheck = function (e) {
        real_click = (e.type == 'mouseover');
    };

    const pagecheck = function (mutationsList, observer) {
        for (let m of mutationsList) {
            switch (m.type) {
                case 'childList':
                    if ((m.addedNodes.length == 1) && (url_match.test(window.location.pathname)) && (m.addedNodes[0] instanceof Element) && (m.addedNodes[0].matches('.Vo7qCfQ1zcxviGmeMySZl._2GqETuCmp5bDDgJPTevkJA'))) {
                        var nopush_node = m.addedNodes[0].querySelector('span._337HrkzqN4vkh_p53bjhoP');
                        nopush_node.addEventListener("mouseover", mousecheck, false);
                        nopush_node.addEventListener("mouseout", mousecheck, false);
                        console.log(GM_listValues());
                        if (saveload('load', window.location.pathname.match(url_match)[1]) && (!nopush_node.querySelector('div._31jibBLy2jbrUM_9tsuJLF'))) {
                            nopush_node.click();
                        }
                    }
                    break;
                case 'attributes':
                    if (real_click && (url_match.test(window.location.pathname)) && (m.target.matches('._2drZNBHt6Bl5RefoNQnRuQ._1gja7VDiu2-gBZwXAb6XVN'))) {
                        saveload(m.target.matches('._31jibBLy2jbrUM_9tsuJLF')?'save':'del', window.location.pathname.match(url_match)[1]);
                    }
                    break;
            }
        }
    };

    const observer = new MutationObserver(pagecheck);
    observer.observe(document.querySelector('div#app'), {
        attributes: true,
        attributeOldValue: true,
        attributeFilter: ['class'],
        childList: true,
        characterData: false,
        subtree: true
    });
})();