nodebb-plugin-ezoic-infinite 0.5.8 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/public/client.js +24 -46
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "0.5.8",
3
+ "version": "0.6.0",
4
4
  "description": "Ezoic ads with infinite scroll using a pool of placeholder IDs",
5
5
  "main": "library.js",
6
6
  "license": "MIT",
package/public/client.js CHANGED
@@ -28,24 +28,28 @@ function parsePool(raw) {
28
28
  ));
29
29
  }
30
30
 
31
+ function isTopicTemplate() {
32
+ return !!(ajaxify && ajaxify.data && (ajaxify.data.template === 'topic' || ajaxify.data.template === 'topicEvents'));
33
+ }
34
+
35
+ function isCategoryTemplate() {
36
+ return !!(ajaxify && ajaxify.data && ajaxify.data.template === 'category');
37
+ }
38
+
31
39
  function getTopicPosts() {
40
+ if (!isTopicTemplate()) return $();
32
41
  const $primary = $('[component="post"][data-pid]');
33
42
  if ($primary.length) return $primary.not('.ezoic-ad-post');
34
-
35
- const $top = $('[data-pid]').filter(function () {
43
+ return $('[data-pid]').filter(function () {
36
44
  const $el = $(this);
37
45
  const hasContent = $el.find('[component="post/content"]').length > 0;
38
46
  const nested = $el.parents('[data-pid]').length > 0;
39
47
  return hasContent && !nested;
40
- });
41
- if ($top.length) return $top.not('.ezoic-ad-post');
42
-
43
- return $('.posts .post').not('.ezoic-ad-post');
48
+ }).not('.ezoic-ad-post');
44
49
  }
45
50
 
46
51
  function getCategoryTopicItems() {
47
- // Only on category topic list pages
48
- if (!ajaxify || !ajaxify.data || ajaxify.data.template !== 'category') return $();
52
+ if (!isCategoryTemplate()) return $();
49
53
  return $('li[component="category/topic"]').not('.ezoic-ad-topic');
50
54
  }
51
55
 
@@ -54,10 +58,10 @@ function tagName($el) {
54
58
  }
55
59
 
56
60
  function removeExistingEzoicNodes() {
61
+ // Remove only the wrappers we created. Their inner placeholder divs go away with them.
57
62
  $('.ezoic-ad-post').remove();
58
63
  $('.ezoic-ad-between').remove();
59
64
  $('.ezoic-ad-topic').remove();
60
- $('[id^="ezoic-pub-ad-placeholder-"]').remove();
61
65
  }
62
66
 
63
67
  function computeWindowSlots(totalItems, interval, poolSize) {
@@ -118,31 +122,13 @@ function insertAdMessagesBetweenReplies($posts, ids, interval) {
118
122
  return activeIds;
119
123
  }
120
124
 
121
- function uniqueConcat(a, b) {
122
- const seen = new Set();
123
- const out = [];
124
- [...a, ...b].forEach((x) => {
125
- if (!seen.has(x)) {
126
- seen.add(x);
127
- out.push(x);
128
- }
129
- });
130
- return out;
131
- }
132
-
133
- function ezoicCall(ids) {
125
+ function ezoicShow(ids) {
134
126
  if (!ids || !ids.length) return;
135
127
 
128
+ // Ezoic supports showAds(101,102,103) and recommends a single call with all ids.
136
129
  window.ezstandalone = window.ezstandalone || {};
137
130
  window.ezstandalone.cmd = window.ezstandalone.cmd || [];
138
-
139
131
  window.ezstandalone.cmd.push(function () {
140
- try {
141
- if (typeof window.ezstandalone.destroyPlaceholders === 'function') {
142
- window.ezstandalone.destroyPlaceholders.apply(window.ezstandalone, ids);
143
- }
144
- } catch (e) {}
145
-
146
132
  try {
147
133
  if (typeof window.ezstandalone.showAds === 'function') {
148
134
  window.ezstandalone.showAds.apply(window.ezstandalone, ids);
@@ -188,37 +174,29 @@ async function refreshAds() {
188
174
 
189
175
  removeExistingEzoicNodes();
190
176
 
191
- const combinedUnique = uniqueConcat(betweenPool, messagePool);
192
177
  const activeIds = [];
193
178
 
194
- if (!$posts.length && $topicItems.length) {
179
+ // Category pages: BETWEEN only (between topics)
180
+ if ($topicItems.length) {
195
181
  if (cfg.enableBetweenAds && betweenPool.length) {
196
- activeIds.push(...insertBetweenGeneric($topicItems, combinedUnique.slice(0, betweenPool.length), betweenInterval, 'ezoic-ad-topic'));
182
+ activeIds.push(...insertBetweenGeneric($topicItems, betweenPool, betweenInterval, 'ezoic-ad-topic'));
197
183
  }
184
+ ezoicShow(activeIds);
185
+ return;
198
186
  }
199
187
 
188
+ // Topic pages: MESSAGE only (between replies)
200
189
  if ($posts.length) {
201
- let cursor = 0;
202
-
203
- if (cfg.enableBetweenAds && betweenPool.length) {
204
- const idsForBetween = combinedUnique.slice(cursor, cursor + betweenPool.length);
205
- cursor += idsForBetween.length;
206
- activeIds.push(...insertBetweenGeneric($posts, idsForBetween, betweenInterval, 'ezoic-ad-between'));
207
- }
208
-
209
190
  if (cfg.enableMessageAds && messagePool.length) {
210
- const idsForMessage = combinedUnique.slice(cursor, cursor + messagePool.length);
211
- cursor += idsForMessage.length;
212
- activeIds.push(...insertAdMessagesBetweenReplies($posts, idsForMessage, messageInterval));
191
+ activeIds.push(...insertAdMessagesBetweenReplies($posts, messagePool, messageInterval));
213
192
  }
193
+ ezoicShow(activeIds);
214
194
  }
215
-
216
- ezoicCall(activeIds);
217
195
  } finally {
218
196
  inFlight = false;
219
197
  if (rerunRequested) {
220
198
  rerunRequested = false;
221
- setTimeout(refreshAds, 50);
199
+ setTimeout(refreshAds, 80);
222
200
  }
223
201
  }
224
202
  }