Skip to content

Commit

Permalink
anti adblock
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyang0507 committed Nov 24, 2024
1 parent 2c72df4 commit 7652c09
Showing 1 changed file with 58 additions and 20 deletions.
78 changes: 58 additions & 20 deletions src/components/CheckAdBlocked.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,34 @@
return;
}
// 方法3: 创建 wwads 相关的诱饵元素检测
// 方法3: 创建多个诱饵元素检测
const baitClasses = [
// wwads 相关
'wwads-cn',
'wwads-horizontal',
'wwads-vertical',
'wwads-content'
'wwads-content',
// 常见广告类名
'ad-unit',
'ad-box',
'ad-banner',
'adsbox',
'ad-space',
'ad-placeholder',
'adsbygoogle',
'textads',
'banner_ad',
'sponsored-ad',
'carbon-ads',
'adview'
];
const baitIds = [
'ad-unit',
'ad-box',
'carbon-ad',
'wwads-container',
'sponsor-ad'
];
// 创建诱饵 div
Expand All @@ -42,19 +64,42 @@
bait.setAttribute('class', className);
// 添加特殊标记用于识别诱饵元素
bait.setAttribute('data-bait', 'true');
bait.style.cssText = `position:absolute;top:-${9999 + index}px;width:1px;height:1px;`;
bait.style.cssText = `position:absolute;top:-${9999 + index}px;width:1px;height:1px;background:transparent;`;
document.body.appendChild(bait);
baits.push(bait);
});
// 使用不同 ID 创建诱饵
baitIds.forEach((id, index) => {
const bait = document.createElement('div');
bait.id = id;
bait.setAttribute('data-bait', 'true');
bait.style.cssText = `position:absolute;top:-${8999 + index}px;width:1px;height:1px;background:transparent;`;
document.body.appendChild(bait);
baits.push(bait);
});
// 创建一些内容诱饵
const contentBait = document.createElement('div');
contentBait.setAttribute('data-bait', 'true');
contentBait.style.cssText = 'position:absolute;top:-9999px;width:1px;height:1px;background:transparent;';
contentBait.innerHTML = `
<div class="ad-content">Advertisement</div>
<div class="adtext">Sponsored Content</div>
<div class="ad-badge">AD</div>
`;
document.body.appendChild(contentBait);
baits.push(contentBait);
// 检查诱饵是否被屏蔽
setTimeout(() => {
// 只检查带有特殊标记的诱饵元素
const baitsToCheck = document.querySelectorAll('[data-bait="true"]');
for (const bait of baitsToCheck) {
if (bait.offsetParent === null ||
window.getComputedStyle(bait).display === 'none' ||
window.getComputedStyle(bait).visibility === 'hidden') {
window.getComputedStyle(bait).visibility === 'hidden' ||
window.getComputedStyle(bait).opacity === '0') {
adBlockDetected = true;
break;
}
Expand All @@ -67,29 +112,22 @@
});
}, 100);
// 方法4: 检查 wwads JS 是否被拦截
// 方法4: 检测 wwads JS 是否被拦截
checkAdScriptBanned('https://wwads.cn/js/makemoney.js');
checkAdScriptBanned('https://cdn.wwads.cn/js/makemoney.js');
}
function checkAdScriptBanned(scriptUrl) {
const script = document.createElement('script');
script.src = scriptUrl;
// 添加一个特殊标记,用于识别检测用的脚本
script.setAttribute('data-detect', 'true');
script.onerror = () => {
adBlockDetected = true;
};
document.head.appendChild(script);
// 5秒后只移除带有检测标记的script标签
setTimeout(() => {
const scripts = document.querySelectorAll('script[data-detect="true"]');
scripts.forEach(s => {
if (s.parentNode) {
s.parentNode.removeChild(s);
// 使用 fetch 检测资源是否可访问
fetch(scriptUrl, { method: 'HEAD' })
.then(response => {
if (!response.ok) {
adBlockDetected = true;
}
})
.catch(() => {
adBlockDetected = true;
});
}, 5000);
}
function closeNotice() {
Expand Down

0 comments on commit 7652c09

Please sign in to comment.