@@ -225,25 +225,87 @@ export const environment = {
225225 getAppVersion ( ) {
226226 return appVersion ;
227227 } ,
228+ inspectAdElement ( el ) {
229+ if ( ! el ) return { rendered : false , hasSize : false } ;
230+
231+ const iframe = el . querySelector ( 'iframe' ) ;
232+ const rect = el . getBoundingClientRect ( ) ;
233+
234+ const rendered = ! ! iframe ;
235+ const hasSize = rect . width > 0 && rect . height > 0 ;
236+
237+ return { rendered, hasSize } ;
238+ } ,
239+ async waitForAds ( els , timeout = 4000 ) {
240+ const start = Date . now ( ) ;
241+
242+ return new Promise ( ( resolve ) => {
243+ const interval = setInterval ( ( ) => {
244+ for ( const el of els ) {
245+ const { rendered, hasSize } = environment . inspectAdElement ( el ) ;
246+
247+ if ( rendered && hasSize ) {
248+ clearInterval ( interval ) ;
249+ return resolve ( 'filled' ) ;
250+ }
251+ }
252+
253+ if ( Date . now ( ) - start > timeout ) {
254+ clearInterval ( interval ) ;
255+ resolve ( 'suspected_blocked' ) ;
256+ }
257+ } , 200 ) ;
258+ } ) ;
259+ } ,
228260 testUrl ( url ) {
229261 return new Promise ( ( resolve ) => {
262+ let done = false ;
263+
230264 const script = document . createElement ( 'script' ) ;
231265
232- script . src = url ;
266+ const finish = ( result ) => {
267+ if ( done ) return ;
268+ done = true ;
269+ script . remove ( ) ;
270+ resolve ( result ) ;
271+ } ;
233272
234- script . onload = ( ) => resolve ( true ) ;
235- script . onerror = ( ) => resolve ( false ) ;
273+ script . src = url ;
274+ script . onload = ( ) => finish ( true ) ;
275+ script . onerror = ( ) => finish ( false ) ;
236276
237277 document . head . appendChild ( script ) ;
238278
239- setTimeout ( ( ) => resolve ( false ) , 2000 ) ;
279+ setTimeout ( ( ) => finish ( false ) , 2000 ) ;
240280 } ) ;
241281 } ,
242282 async isAdBlocked ( ) {
243- const fundingOk = await environment . testUrl (
283+ if ( window . location . hostname === 'localhost' ) {
284+ return false ;
285+ }
286+
287+ const els = document . querySelectorAll ( '.adsbygoogle' ) ;
288+ if ( ! els . length ) return false ;
289+
290+ const state = await environment . waitForAds ( els ) ;
291+
292+ if ( state === 'filled' ) {
293+ return false ;
294+ }
295+
296+ const googlesyndication = await environment . testUrl (
297+ 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
298+ ) ;
299+
300+ if ( ! googlesyndication ) {
301+ return true ;
302+ }
303+
304+ const fundingchoicesmessages = await environment . testUrl (
244305 'https://fundingchoicesmessages.google.com/i/pub-5145928155833172?ers=1'
245306 ) ;
246- return ! fundingOk ;
307+
308+ return ! fundingchoicesmessages ;
247309 }
248310} ;
249311
0 commit comments