|
1 | | -// adblock-detector.js |
| 1 | +// adblock-detector.js - Enhanced Multi-Method Detection |
2 | 2 | (function() { |
3 | 3 | 'use strict'; |
4 | 4 |
|
5 | | - // Function to check if AdBlock is enabled |
6 | | - function detectAdBlock() { |
7 | | - // Create a bait element that ad blockers typically block |
8 | | - const bait = document.createElement('div'); |
9 | | - bait.className = 'adsbox ad ads banner-ad'; |
10 | | - bait.style.cssText = 'position: absolute; top: -1px; left: -1px; width: 1px; height: 1px;'; |
11 | | - document.body.appendChild(bait); |
12 | | - |
13 | | - // Check if the element was blocked |
14 | | - setTimeout(() => { |
15 | | - const isBlocked = bait.offsetHeight === 0 || |
16 | | - bait.offsetWidth === 0 || |
17 | | - window.getComputedStyle(bait).display === 'none' || |
18 | | - window.getComputedStyle(bait).visibility === 'hidden'; |
19 | | - |
20 | | - document.body.removeChild(bait); |
| 5 | + let adBlockDetected = false; |
21 | 6 |
|
22 | | - if (isBlocked) { |
23 | | - showAdBlockWarning(); |
| 7 | + // Method 1: Bait Element Detection |
| 8 | + function checkBaitElement() { |
| 9 | + return new Promise((resolve) => { |
| 10 | + const bait = document.createElement('div'); |
| 11 | + bait.className = 'pub_300x250 pub_300x250m pub_728x90 text-ad textAd text_ad text_ads text-ads text-ad-links advertisement'; |
| 12 | + bait.style.cssText = 'width: 1px !important; height: 1px !important; position: absolute !important; left: -10000px !important; top: -1000px !important;'; |
| 13 | + document.body.appendChild(bait); |
| 14 | + |
| 15 | + setTimeout(() => { |
| 16 | + const detected = bait.offsetParent === null || |
| 17 | + bait.offsetHeight === 0 || |
| 18 | + bait.offsetLeft === 0 || |
| 19 | + bait.offsetTop === 0 || |
| 20 | + bait.offsetWidth === 0 || |
| 21 | + bait.clientHeight === 0 || |
| 22 | + bait.clientWidth === 0; |
| 23 | + |
| 24 | + try { |
| 25 | + const computedStyle = window.getComputedStyle(bait); |
| 26 | + const isHidden = computedStyle.display === 'none' || |
| 27 | + computedStyle.visibility === 'hidden' || |
| 28 | + computedStyle.opacity === '0'; |
| 29 | + |
| 30 | + document.body.removeChild(bait); |
| 31 | + resolve(detected || isHidden); |
| 32 | + } catch (e) { |
| 33 | + document.body.removeChild(bait); |
| 34 | + resolve(detected); |
| 35 | + } |
| 36 | + }, 100); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + // Method 2: Google Ads Detection |
| 41 | + function checkGoogleAds() { |
| 42 | + return new Promise((resolve) => { |
| 43 | + if (typeof window.google_ad_client === 'undefined' || |
| 44 | + window.google_ad_client === null) { |
| 45 | + resolve(false); |
| 46 | + return; |
24 | 47 | } |
25 | | - }, 100); |
| 48 | + |
| 49 | + const script = document.createElement('script'); |
| 50 | + script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; |
| 51 | + script.onerror = () => resolve(true); |
| 52 | + script.onload = () => resolve(false); |
| 53 | + |
| 54 | + setTimeout(() => resolve(false), 1000); |
| 55 | + document.head.appendChild(script); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + // Method 3: AdSense Detection |
| 60 | + function checkAdSense() { |
| 61 | + return new Promise((resolve) => { |
| 62 | + const ads = document.createElement('ins'); |
| 63 | + ads.className = 'adsbygoogle'; |
| 64 | + ads.style.cssText = 'display:inline-block;width:1px;height:1px;position:absolute;left:-10000px;'; |
| 65 | + ads.setAttribute('data-ad-client', 'ca-pub-1234567890123456'); |
| 66 | + ads.setAttribute('data-ad-slot', '1234567890'); |
| 67 | + document.body.appendChild(ads); |
| 68 | + |
| 69 | + setTimeout(() => { |
| 70 | + const detected = ads.innerHTML.length === 0 || |
| 71 | + ads.clientHeight === 0; |
| 72 | + document.body.removeChild(ads); |
| 73 | + resolve(detected); |
| 74 | + }, 100); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + // Method 4: Fetch-based Detection |
| 79 | + function checkFetchBlock() { |
| 80 | + return new Promise((resolve) => { |
| 81 | + fetch('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', { |
| 82 | + method: 'HEAD', |
| 83 | + mode: 'no-cors', |
| 84 | + cache: 'no-store' |
| 85 | + }) |
| 86 | + .then(() => resolve(false)) |
| 87 | + .catch(() => resolve(true)); |
| 88 | + |
| 89 | + setTimeout(() => resolve(false), 2000); |
| 90 | + }); |
26 | 91 | } |
27 | 92 |
|
28 | | - // Function to show the blocking overlay |
| 93 | + // Function to show the blocking overlay (NO DISMISS OPTION) |
29 | 94 | function showAdBlockWarning() { |
30 | | - // Check if user has already dismissed (optional - remove if you want to always show) |
31 | | - const dismissedTime = localStorage.getItem('adblock-warning-dismissed'); |
32 | | - if (dismissedTime) { |
33 | | - const hoursSinceDismiss = (Date.now() - parseInt(dismissedTime)) / (1000 * 60 * 60); |
34 | | - if (hoursSinceDismiss < 24) { |
35 | | - return; // Don't show again for 24 hours |
36 | | - } |
| 95 | + if (document.getElementById('adblock-overlay')) { |
| 96 | + return; // Already shown |
37 | 97 | } |
38 | 98 |
|
39 | | - // Create overlay |
40 | 99 | const overlay = document.createElement('div'); |
41 | 100 | overlay.id = 'adblock-overlay'; |
42 | 101 | overlay.innerHTML = ` |
43 | 102 | <div class="adblock-modal"> |
44 | 103 | <div class="adblock-icon"> |
45 | | - <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
| 104 | + <svg width="80" height="80" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> |
46 | 105 | <circle cx="12" cy="12" r="10"></circle> |
47 | | - <line x1="12" y1="8" x2="12" y2="12"></line> |
48 | | - <line x1="12" y1="16" x2="12.01" y2="16"></line> |
| 106 | + <line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line> |
49 | 107 | </svg> |
50 | 108 | </div> |
51 | | - <h2>AdBlock Detected</h2> |
52 | | - <p>We noticed you're using an ad blocker. We rely on ads to keep Skill Maps free for everyone.</p> |
53 | | - <p><strong>Please disable your ad blocker and refresh the page to continue.</strong></p> |
| 109 | + <h2>🚫 AdBlock Detected</h2> |
| 110 | + <p>We've detected that you're using an ad blocker.</p> |
| 111 | + <p><strong>Please disable your ad blocker to access Skill Maps.</strong></p> |
| 112 | + <p class="support-text">We rely on ads to keep our platform free and accessible to everyone.</p> |
54 | 113 | <div class="adblock-instructions"> |
55 | | - <h3>How to disable AdBlock:</h3> |
| 114 | + <h3>📋 How to disable your AdBlock:</h3> |
56 | 115 | <ol> |
57 | | - <li>Click on your ad blocker extension icon in your browser</li> |
58 | | - <li>Select "Pause on this site" or "Disable on this site"</li> |
59 | | - <li>Refresh the page</li> |
| 116 | + <li><strong>Click</strong> on your ad blocker extension icon (usually in the top-right corner of your browser)</li> |
| 117 | + <li><strong>Select</strong> "Pause on this site" or "Disable on skill-maps.com"</li> |
| 118 | + <li><strong>Refresh</strong> this page</li> |
60 | 119 | </ol> |
61 | 120 | </div> |
62 | | - <button id="adblock-refresh-btn" class="refresh-btn">I've Disabled AdBlock - Refresh</button> |
63 | | - <button id="adblock-dismiss-btn" class="dismiss-btn">Dismiss for 24 hours</button> |
| 121 | + <button id="adblock-refresh-btn" class="refresh-btn"> |
| 122 | + <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-right: 8px;"> |
| 123 | + <polyline points="23 4 23 10 17 10"></polyline> |
| 124 | + <polyline points="1 20 1 14 7 14"></polyline> |
| 125 | + <path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path> |
| 126 | + </svg> |
| 127 | + I've Disabled AdBlock - Refresh Now |
| 128 | + </button> |
| 129 | + <p class="help-text">Still having trouble? Make sure to completely turn off all ad blocking extensions.</p> |
64 | 130 | </div> |
65 | 131 | `; |
66 | 132 |
|
67 | 133 | document.body.appendChild(overlay); |
68 | 134 |
|
69 | | - // Add event listeners |
| 135 | + // Add event listener for refresh button |
70 | 136 | document.getElementById('adblock-refresh-btn').addEventListener('click', () => { |
71 | | - window.location.reload(); |
| 137 | + window.location.reload(true); // Hard reload |
72 | 138 | }); |
73 | 139 |
|
74 | | - document.getElementById('adblock-dismiss-btn').addEventListener('click', () => { |
75 | | - localStorage.setItem('adblock-warning-dismissed', Date.now().toString()); |
76 | | - overlay.remove(); |
77 | | - }); |
78 | | - |
79 | | - // Prevent scrolling when overlay is shown |
| 140 | + // Prevent scrolling |
80 | 141 | document.body.style.overflow = 'hidden'; |
| 142 | + document.documentElement.style.overflow = 'hidden'; |
| 143 | + |
| 144 | + // Prevent closing with ESC key |
| 145 | + document.addEventListener('keydown', preventEscape); |
| 146 | + } |
| 147 | + |
| 148 | + function preventEscape(e) { |
| 149 | + if (e.key === 'Escape') { |
| 150 | + e.preventDefault(); |
| 151 | + e.stopPropagation(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Run all detection methods |
| 156 | + async function detectAdBlock() { |
| 157 | + try { |
| 158 | + const results = await Promise.all([ |
| 159 | + checkBaitElement(), |
| 160 | + checkAdSense(), |
| 161 | + checkFetchBlock() |
| 162 | + ]); |
| 163 | + |
| 164 | + // If any method detects ad blocker |
| 165 | + adBlockDetected = results.some(result => result === true); |
| 166 | + |
| 167 | + if (adBlockDetected) { |
| 168 | + console.warn('AdBlock detected - showing warning'); |
| 169 | + showAdBlockWarning(); |
| 170 | + } else { |
| 171 | + console.log('No AdBlock detected'); |
| 172 | + } |
| 173 | + } catch (error) { |
| 174 | + console.error('Error during AdBlock detection:', error); |
| 175 | + // Optionally show warning on error |
| 176 | + // showAdBlockWarning(); |
| 177 | + } |
81 | 178 | } |
82 | 179 |
|
83 | 180 | // Run detection when DOM is ready |
|
86 | 183 | } else { |
87 | 184 | detectAdBlock(); |
88 | 185 | } |
| 186 | + |
| 187 | + // Also check again after page load (some ad blockers load later) |
| 188 | + window.addEventListener('load', () => { |
| 189 | + if (!adBlockDetected) { |
| 190 | + setTimeout(detectAdBlock, 500); |
| 191 | + } |
| 192 | + }); |
89 | 193 | })(); |
0 commit comments