nodebb-plugin-ezoic-infinite 1.0.11 → 1.0.14

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 +79 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "1.0.11",
3
+ "version": "1.0.14",
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
@@ -2,6 +2,7 @@
2
2
 
3
3
  /* globals ajaxify */
4
4
  (function () {
5
+ try {
5
6
  if (window.ezoicInfiniteLoaded) return;
6
7
  window.ezoicInfiniteLoaded = true;
7
8
 
@@ -23,16 +24,20 @@
23
24
 
24
25
  function getPageKey() {
25
26
  try {
26
- if (ajaxify && ajaxify.data) {
27
- if (ajaxify.data.tid) return 'topic:' + ajaxify.data.tid;
28
- if (ajaxify.data.cid) return 'cid:' + ajaxify.data.cid + ':' + window.location.pathname;
27
+ const ax = window.ajaxify;
28
+ if (ax && ax.data) {
29
+ if (ax.data.tid) return 'topic:' + ax.data.tid;
30
+ if (ax.data.cid) return 'cid:' + ax.data.cid + ':' + window.location.pathname;
29
31
  }
30
32
  } catch (e) {}
31
33
  return window.location.pathname;
32
34
  }
33
35
 
34
36
  function isTopicPage() {
35
- try { return !!(ajaxify && ajaxify.data && ajaxify.data.tid); } catch (e) {}
37
+ try {
38
+ const ax = window.ajaxify;
39
+ return !!(ax && ax.data && ax.data.tid);
40
+ } catch (e) {}
36
41
  return /^\/topic\//.test(window.location.pathname);
37
42
  }
38
43
 
@@ -267,6 +272,61 @@
267
272
  });
268
273
  }
269
274
 
275
+
276
+ function getTopicListItemsFast() {
277
+ return document.querySelectorAll('li[component="category/topic"]').length;
278
+ }
279
+
280
+ function getPostItemsFast() {
281
+ return document.querySelectorAll('[component="post"][data-pid]').length;
282
+ }
283
+
284
+ function observeUntilTargets(pageType, cb) {
285
+ // pageType: 'topic' or 'category'
286
+ const key = pageType + ':' + getPageKey();
287
+ window.__ezoicObs = window.__ezoicObs || {};
288
+ if (window.__ezoicObs[key]) return;
289
+
290
+ const check = function () {
291
+ if (pageType === 'topic') return getPostItemsFast() > 0;
292
+ return getTopicListItemsFast() > 0;
293
+ };
294
+
295
+ if (check()) {
296
+ cb();
297
+ return;
298
+ }
299
+
300
+ const obs = new MutationObserver(function () {
301
+ if (check()) {
302
+ try { obs.disconnect(); } catch (e) {}
303
+ delete window.__ezoicObs[key];
304
+ cb();
305
+ }
306
+ });
307
+
308
+ window.__ezoicObs[key] = obs;
309
+ try {
310
+ obs.observe(document.body, { childList: true, subtree: true });
311
+ } catch (e) {
312
+ // ignore
313
+ }
314
+
315
+ // hard stop after 6s
316
+ setTimeout(function () {
317
+ try { obs.disconnect(); } catch (e) {}
318
+ delete window.__ezoicObs[key];
319
+ }, 6000);
320
+ }
321
+
322
+ function scheduleRetry(flagKey) {
323
+ window.__ezoicRetry = window.__ezoicRetry || {};
324
+ window.__ezoicRetry[flagKey] = window.__ezoicRetry[flagKey] || 0;
325
+ if (window.__ezoicRetry[flagKey] >= 24) return;
326
+ window.__ezoicRetry[flagKey]++;
327
+ setTimeout(run, 250);
328
+ }
329
+
270
330
  async function run() {
271
331
  if (inFlight) { rerunRequested = true; return; }
272
332
  inFlight = true;
@@ -282,19 +342,22 @@
282
342
  const cfg = await fetchConfig();
283
343
  if (!cfg || cfg.excluded) return;
284
344
 
345
+
285
346
  if (isTopicPage()) {
286
- injectBetweenMessages(cfg);
347
+ const hasPosts = getPostItemsFast() > 0;
348
+ if (hasPosts) {
349
+ injectBetweenMessages(cfg);
350
+ } else {
351
+ observeUntilTargets('topic', function () { run(); });
352
+ scheduleRetry('topic:' + getPageKey());
353
+ }
287
354
  } else {
288
- // category/topic list pages may render after ajaxify end; retry a few times
289
- const hasList = document.querySelectorAll('li[component="category/topic"]').length > 0;
355
+ const hasList = getTopicListItemsFast() > 0;
290
356
  if (hasList) {
291
357
  injectBetweenTopics(cfg);
292
358
  } else {
293
- window.__ezoicCatRetry = window.__ezoicCatRetry || 0;
294
- if (window.__ezoicCatRetry < 20) {
295
- window.__ezoicCatRetry++;
296
- setTimeout(run, 250);
297
- }
359
+ observeUntilTargets('category', function () { run(); });
360
+ scheduleRetry('category:' + getPageKey());
298
361
  }
299
362
  }
300
363
  } catch (e) {
@@ -348,4 +411,8 @@ function bindNodeBBEvents() {
348
411
  setTimeout(run, 1200);
349
412
  });
350
413
  }
414
+ ;
415
+ } catch (e) {
416
+ // fail silently to avoid breaking NodeBB
417
+ }
351
418
  })();