做了个观网黑名单油猴脚本,过滤某些人发布的文章或评论

某些人的文章和评论真的是污眼睛,所以趁春节没事做了个油猴脚本用于过滤这些人。有愿意尝试的可以自己添加一下试试。

注意:需要一些基础编程知识,理论上适用于所有chromium内核浏览器,包括新Edge、chrome等,仅在新Edge中做过测试,其它浏览器请自行测试。

使用方式:

1. 先添加Tampemonkey插件;

2. 然后在Tampemonkey中新建脚本后添加以下脚本

// ==UserScript==

// @name         guancha_BlackList

// @namespace    http://tampermonkey.net/

// @version      0.1

// @description  try to filter some user.

// @author       路在脚下

// @match        https://user.guancha.cn/*

// @match        https://www.guancha.cn/*

// @icon         https://i.guancha.cn/images/favorite.ico

// @grant        none

// @run-at  document-end

// ==/UserScript==

(function() {

    'use strict';

    let blackusers=['229396', '228166', '1172090'];// 添加所有要屏蔽的用户id到此处

    let config = { attributes: true };

    let nodes;

    if (document.domain == 'user.guancha.cn'){

        //过滤风闻文章

        let ops =document.querySelector('.index-list');

        filter(ops);

        ops.addEventListener("DOMNodeInserted", function(event) {

            filter(ops)

        });

    }

    if (document.domain == 'www.guancha.cn'){

        // 过滤注释

        filter_comment();

        let opsComment = document.querySelector('#comments-container');

        if(opsComment) {

            console.log('添加改变事件侦听器!');

            opsComment.addEventListener("DOMNodeInserted", function(event) {

                filter_comment()

            });

        }

        // 过滤首页风闻热点文章

        let opsHot = document.querySelector('.module-fengwen-hot')

        if(opsHot) {

            console.log('添加改变事件侦听器 ops hot !');

            opsHot.addEventListener("DOMNodeInserted", function(event) {

                filter_hot(opsHot)

            });

        }

        // 过滤首页文章

        let lists = document.querySelector('.fengwen-list')

        if (lists){

            let items =lists.children

            for(let item of items){

                let url = item.querySelector("a.fengwen-list-user");

                if(url){

                    let regexp = /\d+/g;

                    let user_id = url.href.match(regexp);

                    for (let id of blackusers){

                        if(id==user_id[0]){

                            item.remove();

                            console.log('删除"' + id + '的帖子')

                        }

                    }

                }

            }

        }

    }

    function filter_comment(){

        for (let id of blackusers){

            let str = '[data-uid="' + id + '"]';

            let item = document.querySelector(str)

            if (item){

                item.remove();

                console.log('删除"' + id + '的评论')

            }

        }

    }

    function filter_hot(hot_root){

        console.log('过滤热点')

        let hotlists = hot_root.querySelectorAll('.module-fengwen-hot-list');

        if (hotlists){

            for(let subhotlists of hotlists){

                let items =subhotlists.children

                for(let item of items){

                    let sub_item = item.querySelector('.module-fengwen-hot-user');

                    let url = sub_item.querySelector("a");

                    if(url){

                        let regexp = /\d+/g;

                        let user_id = url.href.match(regexp);

                        for (let id of blackusers){

                            if(id==user_id[0]){

                                item.remove();

                                console.log('删除"' + id + '的帖子')

                            }

                        }

                    }

                }

            }

        }

    }

    // 过滤文章

    function filter(root){

        nodes = root.getElementsByClassName('index-list-item');

        for (let entry of nodes) {

            let url = entry.querySelector("li > div.user-box.user-info-box.clearfix > div.user-main.list-user-main > h4 > a");

            let regexp = /\d+/g;

            let user_id = url.href.match(regexp);

            for (let id of blackusers){

                if(id==user_id[0]){

                    entry.remove();

                    console.log('删除"' + id + '的帖子')

                }

            }

        }

    }

})();

// 脚本结束

3. 添加想屏蔽的用户id到脚本中这行“let blackusers=['229396', '228166', '1172090'];// 添加所有要屏蔽的用户id到此处”,注意id前后用英文单引号包裹。脚本中放了3个id作为演示。用户id可以点击用户名,进入用户个人主页,此时地址栏链接中最后一串数字就是用户id。例子如下:

https://user.guancha.cn/user/personal-homepage?uid=1117319 加黑部分即为用户id。

4. 保存后,在Tampemonkey中启用脚本“guancha_BlackList”即可。访问或刷新观网即可看到效果。

5. 本脚本是随手做的,所以后续维护不做承诺,更大可能是发完就不管了。

6. 本脚本可以随意拷贝使用,不做限制。转帖的话愿意加个链接就链接到这篇文章,不愿意加也无所谓。

全部专栏