hipda-德高望重

有时候,你只想看到德高望重的用户,除非TA是楼主

目前為 2022-06-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name         hipda-德高望重
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  有时候,你只想看到德高望重的用户,除非TA是楼主
// @author       屋大维
// @license      MIT
// @match        https://www.hi-pda.com/forum/*
// @match        https://www.4d4y.com/forum/*
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @icon         https://www.iconninja.com/files/976/19/674/mature-avatar-man-grandfather-male-person-old-icon.png
// ==/UserScript==

(function() {
    'use strict';
    // CONST
    const ENABLE = true; // true or false
    const THRESHOLD = 0; //
    const TOGGLE_KEY = 'alt+P';
    // Your code here...
    // helpers
    function getKeys(e){           // keycode 转换
        var codetable={'96':'Numpad 0','97':'Numpad 1','98':'Numpad 2','99':'Numpad 3','100':'Numpad 4','101':'Numpad 5','102':'Numpad 6','103':'Numpad 7','104':'Numpad 8','105':'Numpad 9','106':'Numpad *','107':'Numpad +','108':'Numpad Enter','109':'Numpad -','110':'Numpad .','111':'Numpad /','112':'F1','113':'F2','114':'F3','115':'F4','116':'F5','117':'F6','118':'F7','119':'F8','120':'F9','121':'F10','122':'F11','123':'F12','8':'BackSpace','9':'Tab','12':'Clear','13':'Enter','16':'Shift','17':'Ctrl','18':'Alt','20':'Cape Lock','27':'Esc','32':'Spacebar','33':'Page Up','34':'Page Down','35':'End','36':'Home','37':'←','38':'↑','39':'→','40':'↓','45':'Insert','46':'Delete','144':'Num Lock','186':';:','187':'=+','188':',<','189':'-_','190':'.>','191':'/?','192':'`~','219':'[{','220':'\|','221':']}','222':'"'};
        var Keys = '';
        e.shiftKey && (e.keyCode != 16) && (Keys += 'shift+');
        e.ctrlKey && (e.keyCode != 17) && (Keys += 'ctrl+');
        e.altKey && (e.keyCode != 18) && (Keys += 'alt+');
        return Keys + (codetable[e.keyCode] || String.fromCharCode(e.keyCode) || '');
    };
    function addHotKey(codes,func){// 监视并执行快捷键对应的函数
        document.addEventListener('keydown', function(e){
            if ((e.target.tagName != 'INPUT') && (e.target.tagName != 'TEXTAREA') && getKeys(e) == codes){
                func();
                e.preventDefault();
                e.stopPropagation();
            }
        }, false);
    };

    // classes
    class HpThread {
        constructor() {
        }

        getThreadTid() {
            return location.href.match(/tid=(\d+)/) ? parseInt(location.href.match(/tid=(\d+)/)[1]) : -999;
        }

        getUserUid() {
            return parseInt($("cite > a").attr("href").split("uid=")[1]);
        }

        getThreadTitle() {
            let l = $('#nav').text().split(" » ");
            return l[l.length - 1];
        }

        getHpPosts() {
            let threadTid = this.getThreadTid();
            let threadTitle = this.getThreadTitle();
            let divs = $('#postlist > div').get();
            return divs.map(d => new HpPost(threadTid, threadTitle, d));
        }
    }

    class HpPost {
        constructor(threadTid, threadTitle, postDiv) {
            this.threadTid = threadTid;
            this.threadTitle = threadTitle;
            this._post_div = postDiv;
            this.hidden = false;
        }

        isMainPost() {
            return this.getPostPid() === new HpPost(this.threadTid, this.threadTitle, $('#postlist > div').first().get()).getPostPid();
        }

        getPostAuthorName() {
            return $(this._post_div).find("div.postinfo > a").first().text();
        }

        getPostAuthorUid() {
            return parseInt($(this._post_div).find("div.postinfo > a").first().attr("href").split("uid=")[1]);
        }

        getPostPid() {
            return parseInt($(this._post_div).attr("id").split("_")[1]);
        }

        getGotoUrl() {
            return `https://www.hi-pda.com/forum/redirect.php?goto=findpost&ptid=${this.threadTid}&pid=${this.getPostPid()}`;
        }
        getPostAuthorCredit() {
            return parseInt($(this._post_div).find("dl.profile > dd:nth-child(6)").text());
        }
        hidePost() {
            $(this._post_div).hide();
            this.hidden = true;
        }
        showPost() {
            $(this._post_div).show();
            this.hidden = false;
        }
        highlightPost() {
            $(this._post_div).css("background-color", "rgba(255, 255, 0, 0.1)");
        }
        toggleDisplay() {
            if (this.hidden) {
                this.showPost();
                return;
            }
            if (this.getPostAuthorCredit() < THRESHOLD && !this.isMainPost()) {
                this.hidePost();
            }
        }
        register() {
            addHotKey(TOGGLE_KEY, this.toggleDisplay.bind(this));
            if (this.getPostAuthorCredit() < THRESHOLD) {
                this.highlightPost();
            }
            if (ENABLE) {
                this.toggleDisplay();
            }
        }
    }


    function main() {
        // get a thread object
        var THIS_THREAD = new HpThread();
        var hp_posts = THIS_THREAD.getHpPosts();
        for (let i=0; i<hp_posts.length; i++) {
            let hp_post = hp_posts[i];
            try {
                hp_post.register();
            } catch(e) {
                // deleted post, simply pass it
                console.log("unable to parse the post, pass");
            }

        }
    }

    main();


})();