【tampermonkey】使用油猴脚本过滤评论信息

廖大网站的时候,有水友希望屏蔽部分不想看到的评论信息,正好油猴功能强大,支持用户自行编写脚本。用户屏蔽功能其实可以借助前端脚本实现,通过开发者工具调试可以看到教程页面是Ajax请求获取评论的,所以我们需要拦截请求😊,ajax-hook是个很好的选择。

首先我们新建一个脚本

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://i.cnblogs.com/posts/edit
// @icon         https://www.google.com/s2/favicons?sz=64&domain=cnblogs.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
})();

指定匹配的网站

// @match        https://www.liaoxuefeng.com/*

引入ajax-hook

// @require      https://unpkg.com/ajax-hook@2.1.3/dist/ajaxhook.min.js

编写过滤方法

ah.proxy({
    //请求成功后进入
    onResponse: (response, handler) => {
        //console.log(response)
        if (response.config.url.startsWith('/api/ref/')) {
            //console.log(response.response)
            handler.next(response)
            filterUsers();
        }
    }
});

function filterUsers(){
    //需要过滤的用户id数组
    var users = ["567test123"];
    //移除指定用户元素
    for(var i=0;i<users.length;i++){
        $('a[href="/user/'+users+'"]').closest('li').remove()
    }
}

如何获取需要过滤的用户id,可以通过右键头像附近,点击检查在a标签的href中找到,多个用户id可以用逗号隔开

 

//需要过滤的用户id数组
var users = ["test123456","yahaha"];

 

完整的脚本如下

// ==UserScript==
// @name         廖雪峰的官方网站用户过滤脚本
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.liaoxuefeng.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=liaoxuefeng.com
// @require      https://unpkg.com/ajax-hook@2.1.3/dist/ajaxhook.min.js
// @grant        none

// ==/UserScript==
/* globals ah,$ */
(function() {
    'use strict';
    //过滤地址

})


ah.proxy({
    //请求成功后进入
    onResponse: (response, handler) => {
        //console.log(response)
        if (response.config.url.startsWith('/api/ref/')) {
            //console.log(response.response)
            handler.next(response)
            filterUsers();
        }
    }
});

function filterUsers(){
    //需要过滤的用户id数组
    var users = ["567test123"];
    //移除指定用户元素
    for(var i=0;i<users.length;i++){
        $('a[href="/user/'+users+'"]').closest('li').remove()
    }
}

 

posted @   一尺灯光  阅读(559)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示