移除广告内嵌脚本(自用)
// ==UserScript==
// @name 移除广告内嵌脚本(自用)
// @namespace https://greasyfork.org/zh-CN/users/1373566
// @version 1.4.9.2.2
// @license MIT
// @description 这是一个由AI生成的脚本,通过关键词匹配来移除网页中的内嵌广告脚本。不能保证100%成功,可以在脚本菜单中管理排除的网页和关键词,脚本已经内置一些常见的关键词,若还有广告,可以自行添加Math.random,platform,navigator,new Function,new Date()尝试去除。
// @author copilot & cheatgpt
// @match http*://*/*
// @exclude *://*.github.com/*
// @exclude *://github.com/*
// @exclude *://*.google.*/*
// @exclude *://x.com/*
// @exclude *://twitter.com/*
// @exclude *://*.bing.*/*
// @exclude *://*.yandex.*/*
// @exclude *://*.iqiyi.com/*
// @exclude *://*.qq.com/*
// @exclude *://*.v.qq.com/*
// @exclude *://*.sohu.com/*
// @exclude *://*.mgtv.com/*
// @exclude *://*.ifeng.com/*
// @exclude *://*.pptv.com/*
// @exclude *://*.sina.com.cn/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const REMOVE_AD_SCRIPTS_KEYWORDS_KEY = 'removeAdScriptsKeywords';
const DEFAULT_KEYWORDS = [
'.substr(10)',
'.substr(22)',
'htmlAds',
'ads_codes',
'{return void 0!==b[a]?b[a]:a}).join("")}',
'-${scripts[randomIndex]}',
'/image/202${scripts[Math.random()',
'"https://"+Date.parse(new Date())+',
'"https://"+(new Date().getDate())+',
'new Function(t)()',
'new Function(b)()',
'new Function(\'d\',e)',
'Math.floor(2147483648 * Math.random());',
'Math.floor(Math.random()*url.length)',
'&&navigator[',
'=navigator;',
'navigator.platform){setTimeout(function',
'¥ 666:/',
'disableDebugger',
'sojson.v',
'<\\/\'+\'s\'+\'c\'+\'ri\'+\'pt\'+\'>\');',
'</\'+\'s\'+\'c\'+\'ri\'+\'pt\'+\'>\');',
];
let storage = GM_getValue(REMOVE_AD_SCRIPTS_KEYWORDS_KEY, {});
const currentDomain = window.location.hostname;
let removedScriptsInfo = [];
if (!storage[currentDomain]) {
storage[currentDomain] = {
custom: [],
useDefault: true
};
GM_setValue(REMOVE_AD_SCRIPTS_KEYWORDS_KEY, storage);
}
function getEffectiveKeywords() {
const config = storage[currentDomain];
return [
...(config.useDefault ? DEFAULT_KEYWORDS : []),
...config.custom
];
}
function removeSpecificScript() {
const keywords = getEffectiveKeywords();
document.querySelectorAll('script').forEach(script => {
const matched = keywords.filter(k => script.innerHTML.includes(k));
if (matched.length) {
removedScriptsInfo.push({
keywords: matched,
content: script.innerHTML
});
script.remove();
}
});
}
function saveStorage() {
GM_setValue(REMOVE_AD_SCRIPTS_KEYWORDS_KEY, storage);
}
function showInlineScripts() {
const scripts = Array.from(document.querySelectorAll('script'))
.filter(s => s.innerHTML.trim().length > 0);
const displayContent = scripts.map((s, i) =>
`脚本 ${i+1}:\n${s.innerHTML.slice(0, 200).replace(/[\n\t]/g, '')}...`
).join('\n\n') || '无内嵌脚本';
alert(displayContent);
}
function showRemovedScriptsInfo() {
alert(removedScriptsInfo.map((info, i) =>
`#${i+1} 已移除含有关键词:${info.keywords.join(',')}\n${info.content.slice(0,100)}...`
).join('\n\n') || '无移除记录');
}
function manageKeywords() {
const menu = [
'1. 添加关键词(当前域名)',
'2. 恢复全局默认关键词',
'3. 查看当前关键词',
'4. 清空所有关键词',
'5. 查看内嵌脚本'
].join('\n');
const choice = prompt(`请选择操作(当前域名:${currentDomain}):\n${menu}`);
if (choice === null) return;
const current = storage[currentDomain];
switch (choice) {
case '1':
const input = prompt('请输入关键词(多个用逗号分隔):');
if (input) {
const newKeys = input.split(/[,,]/g)
.map(k => k.trim())
.filter(k => k && !current.custom.includes(k));
if (newKeys.length) {
current.custom.push(...newKeys);
saveStorage();
alert(`已添加 ${newKeys.length} 个关键词到 ${currentDomain}`);
removeSpecificScript();
}
}
break;
case '2':
if (confirm('⚠️ 将重置所有配置为默认状态?')) {
storage[currentDomain] = {
custom: [],
useDefault: true
};
saveStorage();
alert('已恢复全局默认配置');
removeSpecificScript();
}
break;
case '3':
const effective = getEffectiveKeywords();
alert([
`当前生效关键词(共 ${effective.length} 个):`,
...effective.map((k, i) => `${i+1}. ${k}`),
'\n配置状态:',
`- 使用默认关键词:${current.useDefault ? '是' : '否'}`,
`- 自定义关键词:${current.custom.length} 个`
].join('\n'));
break;
case '4':
if (confirm('⚠️ 将清空所有关键词配置?')) {
current.custom = [];
current.useDefault = false;
saveStorage();
alert('已禁用并清空所有关键词');
removeSpecificScript();
}
break;
case '5':
showInlineScripts();
break;
default:
alert('无效操作');
}
manageKeywords();
}
new MutationObserver(mutations => {
mutations.forEach(m => {
m.addedNodes.forEach(n => {
if (n.tagName === 'SCRIPT') removeSpecificScript();
});
});
}).observe(document.documentElement, {
childList: true,
subtree: true
});
removeSpecificScript();
GM_registerMenuCommand('🔑 管理关键词', () => {
manageKeywords();
});
GM_registerMenuCommand('📜 移除日志', () => {
showRemovedScriptsInfo();
manageKeywords();
});
GM_registerMenuCommand('🔍 内嵌脚本', () => {
showInlineScripts();
manageKeywords();
});
})();