Greasy Fork is available in English.

广告屏蔽

自用,屏蔽一些网站的广告,如果也符合你的需求就下载吧(=・ω・=) 具体屏蔽列表看简介,不需要的屏蔽可以注释掉。

< Feedback on 广告屏蔽

Review: Good - script works

§
Posted: 08.07.2021

感谢作者的分享!
但是在我的Chrome浏览器上访问樱花动漫时广告屏蔽没起作用,我就看了下控制台和源码,发现报错document.head是null,所以是dom还没解析完成啦,加一个window的load事件回调函数就行了。
修改后的代码如下:

// ==UserScript==
// @name 广告屏蔽
// @namespace http://tampermonkey.net/
// @version 1.1.7
// @update 2021.05.17
// @description 自用,屏蔽一些网站的广告,如果也符合你的需求就下载吧(=・ω・=) 具体屏蔽列表看简介,不需要的屏蔽可以注释掉。
// @author charghet
// @run-at document-start
// @license GPL
// @include http*://www.imomoe.la/*
// @include http*://api.xiaomingming.org/cloud/mp6.php*
// @include
// @include http*://www.bilibili.com/
// @include http*://live.bilibili.com/*
// @include http*://www.bilibili.com/video/*
// @include http*://www.baidu.com/
// @include http*://fanyi.baidu.com/*
// @include http*://forum.xda-developers.com/*
// ==/UserScript==

//屏蔽列表
//樱花动漫
var imomoe = RegExp("http.*://www.imomoe.la/.*");
var imomoe2 = RegExp("http.*://api.xiaomingming.org/cloud/mp6.php.*");
//哔哩哔哩
var bilibili = RegExp("http.*://www.bilibili.com/$");
var bilibili2 = RegExp("http.*://live.bilibili.com/.*");
var bilibili3 = RegExp("http.*://www.bilibili.com/video/.*");
//百度首页
var baidu = RegExp("http.*://www.baidu.com/$");
//百度翻译
var baidufanyi = RegExp("http.*://fanyi.baidu.com/.*");
//XDA
var xda = RegExp("http.*://forum.xda-developers.com/.*");

function callback() {
if(imomoe.test(location.href)){//樱花动漫
//右下角屏蔽
document.head.insertAdjacentHTML('beforeend','

');
//左右横幅屏蔽
document.head.insertAdjacentHTML('beforeend','

');
//底部横幅
document.head.insertAdjacentHTML('beforeend','

');
//不新建标签,直接打开链接
document.onreadystatechange = function(){
if(document.readyState == "interactive"){
var list = document.getElementsByTagName('a');
for(let i = 0;i < list.length;i++){
list[i].target = '_self';
}
}
}
}else if(imomoe2.test(location.href)){
//暂停页面屏蔽
document.head.insertAdjacentHTML('beforeend','

');

}else if(bilibili.test(location.href)){//哔哩哔哩
//主页横幅屏蔽
document.head.insertAdjacentHTML('beforeend','

');
}else if(bilibili2.test(location.href)){//哔哩哔哩直播
//开头,勿注释
var s = '

';
document.head.insertAdjacentHTML('beforeend',s);
}else if(bilibili3.test(location.href)){//哔哩哔哩视频
//右下角、横幅屏蔽
document.head.insertAdjacentHTML('beforeend','

');
}else if(baidu.test(location.href)){//百度首页
//搜索框底部新闻屏蔽
document.head.insertAdjacentHTML('beforeend','

');
}else if(baidufanyi.test(location.href)){//百度翻译
//右边和底部广告
document.head.insertAdjacentHTML('beforeend','

');
}else if(xda.test(location.href)){//XDA
document.head.insertAdjacentHTML('beforeend','

');
}else{
console.log('广告屏蔽:无匹配');
}
};

window.addEventListener('load', callback);

charghetAuthor
§
Posted: 18.07.2021

感谢您的反馈。我测试了一下,确实有时候会出现在head元素生成之前就注入脚本的情况,导致无法在head中插入css。这是由于我将脚本的注入时期设置为document-start,即脚本会尽快被注入,这就会出现如上情况。我已将脚本的注入时刻改为document-body,会在body元素生成后注入脚本,就不会出现head标签为null的情况。您的改进方案是在load事件中注入css,load事件是在所有资源加载完毕后才会被执行,也就是会将广告图片加载完才插入css,事实上这会导致广告一闪而过的情况,网络差的情况下可能会更久,建议您更新最新版本,我已在新版本中修复了此问题。

Post reply

Sign in to post a reply.