-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-injection.js
33 lines (28 loc) · 1 KB
/
content-injection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.addEventListener("DOMContentLoaded", () => {
if(ad_html) {
if( document.querySelector('.ad-container') ) {
document.querySelector('.ad-container').outerHTML = ad_html;
}
var ad_counter = [0];
document.querySelectorAll('.article-content > *').forEach(element => {
if( element.tagName === 'P' ) {
ad_counter[ad_counter.length - 1]++;
} else {
ad_counter.push(0);
}
})
var ad_max = Math.max(...ad_counter);
if(ad_max >= 5) {
var ad_count = 0;
ad_counter.forEach(ad_item => {
if(ad_item == ad_max) {
ad_count += ad_max / 2 | 0;
} else {
ad_count += ad_item;
}
})
var ad_placement = document.querySelector('.article-content > p:nth-child(' + ad_count + ')');
ad_placement.insertAdjacentHTML('afterend', ad_html);
}
}
})