-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticle-ads.js
241 lines (220 loc) · 7.8 KB
/
article-ads.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Browser processor
*/
const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
/**
* @type {import("../src/config")}
*/
const hexoAdsenseConfig = JSON.parse(document.getElementById('hexo-adsense-config').textContent);
//console.log(hexoAdsenseConfig);
/**
* Insert after element
* @param {HTMLElement} newElement
* @param {HTMLElement} oldElement
*/
function insertAfter(newElement, oldElement) {
if (oldElement && newElement) {
let parent = oldElement.parentNode;
if (parent.lastChild == oldElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, oldElement.nextSibling);
}
} else {
console.error('cannot insert element');
}
}
/**
* Replace elements with new
* @param {HTMLElement} newElement
* @param {HTMLElement} oldElement
*/
function replaceWith(newElement, oldElement) {
if (!oldElement.parentNode) {
console.log(oldElement, 'parent null');
let d = document.createElement('div');
d.appendChild(oldElement);
} else {
//console.log(oldElement.parentNode.tagName);
oldElement.parentNode.replaceChild(newElement, oldElement);
}
/*
try {
oldElement.parentNode.replaceChild(newElement, oldElement);
} catch (e) {}
*/
}
let createElementFromHTML = function (htmlString) {
if (htmlString instanceof HTMLElement) {
return htmlString;
}
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
// Change this to div.childNodes to support multiple top-level nodes
return div.firstChild;
};
function newMethod() {
console.log('new method initialize');
const adshide = document.getElementById('hexo-adsense-hidden');
let adscont = Array.from(adshide.querySelectorAll('[hexo-adsense="ads-content"]'));
let article = Array.from(document.querySelectorAll('article'));
if (!article.length) {
const post = document.querySelector('#post');
if (post) article = [post];
}
if (article.length > 0 && adscont.length > 0) {
/**
* @type {HTMLElement}
*/
let ads;
if (article.length == 1) {
console.log('webpage is post');
let targetArticle = article[0];
// prioritize hexo-adsense-fill before auto ads on other elements
const ads_fill = targetArticle.querySelectorAll('*[hexo-adsense-fill]');
if (ads_fill.length > 0) {
console.log('found hexo-adsense-fill', ads_fill.length);
for (let index = 0; index < ads_fill.length; index++) {
const toFill = ads_fill[index];
if (typeof adscont[index] !== 'undefined') {
toFill.appendChild(adscont[index]);
}
}
}
// the rest of the ads will show automatically after headers elements
adscont = Array.from(adshide.querySelectorAll('[hexo-adsense="ads-content"]'));
if (adscont.length > 0) {
console.log('Iterating headers', adscont.length, 'ads left');
const headers = targetArticle.querySelectorAll('h1,h2,h3,h4,h5,h6');
if (headers.length > 0) {
// generate index of headers
let headers_index = Array.apply(null, { length: headers.length }).map(Number.call, Number);
//console.log(headers_index);
for (let index = 0; index < adscont.length; index++) {
// ads = adscont[index];
ads = adscont.shift();
const rheaders = array_shuffle(headers_index);
// pick a random index
const rheader = rheaders.next().value;
if (typeof rheader === 'number') {
const header = headers.item(rheader);
insertAfter(createElementFromHTML(ads), header);
}
}
}
}
if (adscont.length > 0) {
console.log('Iterating pre code', adscont.length, 'ads left');
// Select all <pre> elements that are not inside a <td>
const preElements = document.querySelectorAll('pre:not(td > pre)');
if (preElements.length > 0) {
// generate index of headers
let preElements_index = Array.apply(null, { length: preElements.length }).map(Number.call, Number);
//console.log(headers_index);
for (let index = 0; index < adscont.length; index++) {
// ads = adscont[index];
ads = adscont.shift();
const rPreElements = array_shuffle(preElements_index);
// pick a random index
const rPre = rPreElements.next().value;
if (typeof rPre === 'number') {
const header = preElements.item(rPre);
insertAfter(createElementFromHTML(ads), header);
}
}
}
}
// the rest of the ads will show automatically to linebreak elements
adscont = adshide.querySelectorAll('[hexo-adsense="ads-content"]');
if (adscont.length > 0) {
const linebreaks = targetArticle.querySelectorAll('br,hr');
if (linebreaks.length > 0) {
// generate index of linebreaks
let linebreaks_index = Array.apply(null, { length: linebreaks.length }).map(Number.call, Number);
//console.log(linebreaks_index);
// randomize linebreaks index
const rlinebreaks = array_shuffle(linebreaks_index);
for (let index = 0; index < adscont.length; index++) {
ads = adscont[index];
// pick a random index
const rlinebreak = rlinebreaks.next().value;
if (typeof rlinebreak == 'number') {
const linebreak = linebreaks.item(rlinebreak);
if (
['blockquote', 'img', 'a', 'pre', 'code', 'em', 'strong', 'i', 'u'].includes(
linebreak.parentNode.tagName.toLowerCase()
)
) {
index--;
continue;
}
//console.log(linebreak.tagName);
replaceWith(createElementFromHTML(ads), linebreak);
}
}
}
}
} else {
console.log('webpage is not post');
if (!article.length) {
article = document.querySelectorAll('[class*="recent-post-item"]');
}
// generate index of articles
let articles_index = Array.apply(null, { length: article.length }).map(Number.call, Number);
// randomize linebreaks index
const rArticles = array_shuffle(articles_index);
for (let index = 0; index < adscont.length; index++) {
ads = adscont[index];
// pick a random index
const rArticle = rArticles.next().value;
if (typeof rArticle == 'number') {
//console.log("adsense display to article index", rArticle);
const pickArticle = article.item(rArticle);
pickArticle.appendChild(createElementFromHTML(ads));
}
}
}
// summon adsbygoogle.push()
adsensePush();
}
}
/**
* Next generation of non-repeated randomizer
* @see {@link shuffleArr}
* @param {any[]} array
*/
function* array_shuffle(array) {
var i = array.length;
while (i--) {
yield array.splice(Math.floor(Math.random() * (i + 1)), 1)[0];
}
}
function adsensePush() {
for (let index = 0; index < document.querySelectorAll('[hexo-adsense="ads-content"]').length; index++) {
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: hexoAdsenseConfig.pub
});
}
}
if (!isBrowser()) {
module.exports = {
replaceWith,
insertAfter
};
} else {
window.__tcfapi = (command, parameter, callback) => {
if (command === 'checkConsent') {
callback(true);
}
if (command === 'addEventListener') {
callback({ eventStatus: 'tcloaded', gdprApplies: false }, true);
}
};
if (typeof document.addEventListener == 'function') {
document.addEventListener('DOMContentLoaded', newMethod);
} else if (typeof window.attachEvent == 'function') {
window.attachEvent('onload', newMethod);
} else {
window.onload = newMethod;
}
}