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 e6054e2 commit b23df6d
Showing 1 changed file with 93 additions and 15 deletions.
108 changes: 93 additions & 15 deletions src/components/CheckAdBlocked.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,104 @@
return;
}
// check ad block init
// if (window._AdBlockInit === undefined) {
// adBlockDetected = true;
// return;
// }
// 方法1: 检查广告容器是否被隐藏
const adContainer = document.querySelector('.wwads-cn');
if (adContainer && (window.getComputedStyle(adContainer).display === 'none' || !adContainer.offsetParent)) {
adBlockDetected = true;
return;
}
// 方法2: 检查广告相关变量
if (typeof window._AdBlockInit !== 'undefined' || typeof window.__WWADS === 'undefined') {
adBlockDetected = true;
return;
}
// 方法3: 创建多个诱饵元素检测
const baitClasses = [
'ad-unit ad adsbox ad-space', // 常见广告类名
'banner_ad ad-banner', // 横幅广告
'adsbygoogle', // Google Ads
'ad-sidebar sponsorship', // 侧边栏广告
'pub_300x250 pub_300x250m', // 特定尺寸广告
'textads textads-text', // 文字广告
'banner-ads banner_ads', // 另一种横幅广告
'ad-zone ad-space', // 广告区域
'ad-placeholder adbadge', // 广告占位符
'inner-ad-space-holder' // 内部广告占位
];
const baitIds = [
'ad-slot',
'ad-banner-top',
'sponsored-content',
'advertisement',
'google_ads_frame'
];
// 创建多个诱饵 div
const baits = [];
// 使用不同类名创建诱饵
baitClasses.forEach((className, index) => {
const bait = document.createElement('div');
bait.setAttribute('class', className);
bait.style.cssText = `position:absolute;top:-${9999 + index}px;width:1px;height:1px;`;
document.body.appendChild(bait);
baits.push(bait);
});
// 使用不同 ID 创建诱饵
baitIds.forEach((id, index) => {
const bait = document.createElement('div');
bait.id = id;
bait.style.cssText = `position:absolute;top:-${8999 + index}px;width:1px;height:1px;`;
document.body.appendChild(bait);
baits.push(bait);
});
// 检查诱饵是否被屏蔽
setTimeout(() => {
for (const bait of baits) {
if (bait.offsetParent === null ||
window.getComputedStyle(bait).display === 'none' ||
window.getComputedStyle(bait).visibility === 'hidden' ||
window.getComputedStyle(bait).opacity === '0') {
adBlockDetected = true;
break;
}
}
// 清理诱饵元素
baits.forEach(bait => {
if (bait.parentNode) {
bait.parentNode.removeChild(bait);
}
});
}, 100);
// check script banned
// 方法4: 检查广告 JS 是否被拦截
checkAdScriptBanned('https://wwads.cn/js/makemoney.js');
checkAdScriptBanned('https://cdn.wwads.cn/js/makemoney.js');
}
function checkAdScriptBanned(scriptUrl) {
fetch(scriptUrl)
.then(response => {
if (!response.ok) {
adBlockDetected = true;
}
})
.catch(() => {
adBlockDetected = true;
});
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);
}
});
}, 5000);
}
function closeNotice() {
Expand Down

0 comments on commit b23df6d

Please sign in to comment.