🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Hacker News Comment Search Filter

Hides comments on a page via regular expression search fields

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name          Hacker News Comment Search Filter
// @description   Hides comments on a page via regular expression search fields
// @version       1.0.0
// @author        sdca
// @namespace     sdca.hncsf
// @include       https://news.ycombinator.com/item?id=*
// @grant         GM_addStyle
// ==/UserScript==

(function(){

    // Delay comment count threshold
    var filter_delay_min_comments = 300;
    // Delay time in seconds
    var filter_delay_time = 0.5;

    if(document.querySelector('.comment-tree')) {
        var filter_count = 0;
        var filter_delay;
        if(filter_delay_min_comments >= (document.querySelectorAll('.athing').length - 1)) {
            filter_delay_time = 0;
        }
        GM_addStyle('#fc-wrapper{margin:0 20px;} .filter-comments{width:100%;} .fid{display:none;}');
        document.querySelector('.comment-tree').insertAdjacentHTML('beforebegin', '<div id="fc-wrapper"><form id="fc-form"><input id="filter-comments-add" type="button" value="+"/></form></div>');
        document.getElementById('filter-comments-add').addEventListener('click', filter_add, false);
        document.getElementById("fc-form").addEventListener("submit", function(event){
            event.preventDefault();
        });
        filter_add();
    }

    function filter_add() {
        filter_count++;
        document.getElementById('filter-comments-add').insertAdjacentHTML('beforebegin', '<input id="filter-comment-'+filter_count+'" class="filter-comments" placeholder="Search Comments..." />');
    }

    function filter_comments() {
        var re = new RegExp();
        var st = this.value;
        var ex = false;
        var fid = this.id.replace(/^filter-comment-/, '');

        if(this.value.charAt(0) == '-') {
            ex = true;
            st = st.replace(/^-/, '');
            if(this.value == '-') {
                ex = false;
            }
        }

        try {
            re = new RegExp(st, 'i');
        } catch(err){}

        if(re !== 'undefined') {
            clearTimeout(filter_delay);
            filter_delay = setTimeout(function() {

                Array.prototype.forEach.call(document.querySelectorAll('.athing'), function(el, i){
                    if (!i) return;

                    var comment = el.querySelector('.default').textContent;
                    var filter_remove = false;
                    if(ex === false) {
                        if(re.test(comment)) {
                            el.classList.remove('fid'+fid);
                            filter_remove = true;
                        } else {
                            el.classList.add('fid','fid'+fid);
                        }
                    } else {
                        if(!re.test(comment)) {
                            el.classList.remove('fid'+fid);
                            filter_remove = true;
                        } else {
                            el.classList.add('fid','fid'+fid);
                        }
                    }

                    if(filter_remove === true && el.classList.contains('fid')) {
                        var filter_remain = false;
                        for (var filter = 0; filter < filter_count; filter++) {
                            if(el.classList.contains('fid'+filter)) {
                                filter_remain = true;
                            }
                        }
                        if(!filter_remain) {
                            el.classList.remove('fid');
                        }
                    }
                });
            }, filter_delay_time * 1000);
        }
    }

    document.addEventListener('input', function (event) {
        var el = event.target, index = -1;
        while (el && ((index = Array.prototype.indexOf.call(document.querySelectorAll('.filter-comments'), el)) === -1)) {
            el = el.parentElement;
        }
        if (index > -1) {
            filter_comments.call(el, event);
        }
    });
    document.addEventListener('keydown', function (event) {
        var el = event.target, index = -1;
        while (el && ((index = Array.prototype.indexOf.call(document.querySelectorAll('.filter-comments'), el)) === -1)) {
            el = el.parentElement;
        }
        if (index > -1) {
            if(event.keyCode == 27) {
                el.value = '';
                filter_comments.call(el, event);
            }
        }
    });

})();