油猴脚本
[JavaScript] 纯文本查看 复制代码 // ==UserScript==
// @name 哔哩哔哩首页去广告
// @namespace Violentmonkey Scripts
// @match *://www.bilibili.com/
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
const observer = new MutationObserver((mutationsList, observer) => {
// 遍历每一个发生变化的 mutation
for (let mutation of mutationsList) {
// 检查新增元素
if (mutation.type === 'childList') {
// 遍历新增的节点和子节点
mutation.addedNodes.forEach(node => {
if ($(node).is('.bili-video-card.is-rcmd') || $(node).is('.feed-card')) {
handleNode(node);
}
});
}
}
});
// 要监视的目标节点
const targetNode = document.body;
// 配置并启动 MutationObserver
const config = { childList: true, subtree: true };
observer.observe(targetNode, config);
// 处理新增的节点的函数
function handleNode(node) {
if (node.marking) {
return null
}
let element = $(node).find("div>a")[0]
if (!element.getAttribute('href').includes("www.bilibili.com")) {
node.marking = true
if ($(node.parentElement).is('.feed-card')) {
node.parentElement.parentElement.appendChild(node.parentElement);
} else {
node.parentElement.appendChild(node);
}
console.log("移除广告", node);
}
}
$(
function () {
$('.bili-video-card.is-rcmd').each(function () {
handleNode(this)
});
}
) |