Skip to content

Commit f5be2bb

Browse files
committed
Improve googlesyndication_adsbygoogle shim
1 parent 3bc6c9e commit f5be2bb

1 file changed

Lines changed: 46 additions & 29 deletions

File tree

src/web_accessible_resources/googlesyndication_adsbygoogle.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,54 @@
2020
*/
2121

2222
(function() {
23-
'use strict';
24-
const init = ( ) => {
25-
window.adsbygoogle = {
26-
loaded: true,
27-
push: function() {
28-
}
29-
};
30-
const phs = document.querySelectorAll('.adsbygoogle');
31-
const css = 'height:1px!important;max-height:1px!important;max-width:1px!important;width:1px!important;';
32-
for ( let i = 0; i < phs.length; i++ ) {
33-
const id = `aswift_${i}`;
34-
if ( document.querySelector(`iframe#${id}`) !== null ) { continue; }
35-
const fr = document.createElement('iframe');
36-
fr.id = id;
37-
fr.style = css;
38-
const cfr = document.createElement('iframe');
39-
cfr.id = `google_ads_frame${i}`;
40-
fr.appendChild(cfr);
41-
const ph = phs[i];
42-
ph.appendChild(fr);
43-
ph.setAttribute('data-adsbygoogle-status', 'done');
23+
self.adsbygoogle = self.adsbygoogle || {
24+
loaded: true,
25+
push: function() {
26+
;
4427
}
4528
};
46-
if (
47-
document.querySelectorAll('.adsbygoogle').length === 0 &&
48-
document.readyState === 'loading'
49-
) {
50-
window.addEventListener('DOMContentLoaded', init, { once: true });
51-
} else {
52-
init();
53-
}
29+
let adCount = 1;
30+
const setupAd = (placeholder) => {
31+
const fr = document.createElement('iframe');
32+
fr.id = `aswift_${adCount}`;
33+
fr.setAttribute('name', fr.id);
34+
adCount += 1;
35+
placeholder.dataset.adsbygoogleStatus = 'loading';
36+
placeholder.dataset.adStatus = 'loading';
37+
placeholder.appendChild(fr);
38+
fr.addEventListener('load', ( ) => {
39+
placeholder.dataset.adsbygoogleStatus = 'done';
40+
placeholder.dataset.adStatus = 'filled';
41+
fr.dataset.loadComplete = 'true';
42+
}, { once: true });
43+
fr.contentWindow.location = 'https://googleads.g.doubleclick.net.ublock-origin.invalid/pagead/ads?';
44+
};
45+
const process = ( ) => {
46+
const phs = document.querySelectorAll('.adsbygoogle:not([data-ad-status][data-adsbygoogle-status])');
47+
for ( const ph of phs ) {
48+
setupAd(ph);
49+
}
50+
};
51+
process();
52+
53+
let observer = new MutationObserver(( ) => {
54+
if ( process.timer !== undefined ) { return; }
55+
process.timer = self.requestAnimationFrame(( ) => {
56+
process.timer = undefined;
57+
process();
58+
},);
59+
});
60+
observer.observe(document, {
61+
attributes: true,
62+
attributeFilter: [ 'class' ],
63+
childList: true,
64+
subtree: true,
65+
});
66+
67+
setTimeout(( ) => {
68+
observer.disconnect();
69+
observer = undefined;
70+
}, 20000);
5471
})();
5572

5673
/*

0 commit comments

Comments
 (0)