nodebb-plugin-ezoic-infinite 0.7.7 → 0.7.9

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 +36 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "0.7.7",
3
+ "version": "0.7.9",
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
@@ -142,15 +142,19 @@ function removeOldestCategoryAd() {
142
142
  function callEzoic(ids) {
143
143
  if (!ids || !ids.length) return;
144
144
 
145
+ // Prevent duplicate calls (observer/poller can fire multiple times)
146
+ const key = Array.isArray(ids) ? ids.join(',') : String(ids);
147
+ const now = Date.now();
148
+ if (window.__ezoicLastShowKey === key && (now - (window.__ezoicLastShowAt || 0)) < 2500) {
149
+ return;
150
+ }
151
+ window.__ezoicLastShowKey = key;
152
+ window.__ezoicLastShowAt = now;
153
+
145
154
  window.ezstandalone = window.ezstandalone || {};
146
155
  window.ezstandalone.cmd = window.ezstandalone.cmd || [];
147
156
 
148
157
  const run = function () {
149
- try {
150
- if (typeof window.ezstandalone.destroyPlaceholders === 'function') {
151
- window.ezstandalone.destroyPlaceholders.apply(window.ezstandalone, ids);
152
- }
153
- } catch (e) {}
154
158
  try {
155
159
  if (typeof window.ezstandalone.showAds === 'function') {
156
160
  window.ezstandalone.showAds.apply(window.ezstandalone, ids);
@@ -160,14 +164,29 @@ function callEzoic(ids) {
160
164
  return false;
161
165
  };
162
166
 
167
+ setupAdAutoHeight();
163
168
  window.ezstandalone.cmd.push(function () { run(); });
164
169
 
170
+ // Retry only if showAds isn't available yet
165
171
  let tries = 0;
166
172
  const maxTries = 6;
167
- const timer = setInterval(function () {
173
+ const retry = function () {
168
174
  tries++;
169
- if (run() || tries >= maxTries) clearInterval(timer);
170
- }, 800);
175
+ const ok = run();
176
+ if (ok) return;
177
+ try {
178
+ if (typeof window.ezstandalone.showAds === 'function') return;
179
+ } catch (e) {}
180
+ if (tries < maxTries) setTimeout(retry, 800);
181
+ };
182
+
183
+ try {
184
+ if (typeof window.ezstandalone.showAds !== 'function') {
185
+ setTimeout(retry, 800);
186
+ }
187
+ } catch (e) {
188
+ setTimeout(retry, 800);
189
+ }
171
190
  }
172
191
 
173
192
  function getPostNumber($post) {
@@ -260,6 +279,11 @@ function injectCategoryBetweenAds($items, pool, interval) {
260
279
  }
261
280
 
262
281
  async function refreshAds() {
282
+ const now = Date.now();
283
+ if (window.__ezoicLastRefreshAt && now - window.__ezoicLastRefreshAt < 400) {
284
+ return;
285
+ }
286
+ window.__ezoicLastRefreshAt = now;
263
287
  const key = getPageKey();
264
288
  if (pageKey !== key) {
265
289
  pageKey = key;
@@ -324,20 +348,19 @@ setTimeout(debounceRefresh, 2200);
324
348
  (function setupEzoicObserver() {
325
349
  if (window.__ezoicInfiniteObserver) return;
326
350
  try {
327
- const trigger = function () { debounceRefresh(); };
328
-
329
- // MutationObserver: trigger when new posts/topics are added anywhere in the page
330
351
  const obs = new MutationObserver(function (mutations) {
331
352
  for (const m of mutations) {
332
353
  if (!m.addedNodes || !m.addedNodes.length) continue;
333
354
  for (const n of m.addedNodes) {
334
355
  if (!n || n.nodeType !== 1) continue;
356
+ // direct match
335
357
  if (n.matches && (n.matches('[component="post"][data-pid]') || n.matches('li[component="category/topic"]'))) {
336
- trigger();
358
+ debounceRefresh();
337
359
  return;
338
360
  }
361
+ // descendant match
339
362
  if (n.querySelector && (n.querySelector('[component="post"][data-pid]') || n.querySelector('li[component="category/topic"]'))) {
340
- trigger();
363
+ debounceRefresh();
341
364
  return;
342
365
  }
343
366
  }
@@ -345,20 +368,5 @@ setTimeout(debounceRefresh, 2200);
345
368
  });
346
369
  obs.observe(document.body, { childList: true, subtree: true });
347
370
  window.__ezoicInfiniteObserver = obs;
348
-
349
- // Poller fallback: if some renders bypass observers/events, detect count changes
350
- let lastPostCount = 0;
351
- let lastTopicCount = 0;
352
- setInterval(function () {
353
- try {
354
- const posts = document.querySelectorAll('[component="post"][data-pid]').length;
355
- const topics = document.querySelectorAll('li[component="category/topic"]').length;
356
- if (posts !== lastPostCount || topics !== lastTopicCount) {
357
- lastPostCount = posts;
358
- lastTopicCount = topics;
359
- trigger();
360
- }
361
- } catch (e) {}
362
- }, 1500);
363
371
  } catch (e) {}
364
372
  })();