nodebb-plugin-ezoic-infinite 1.0.13 → 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 +66 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "1.0.13",
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
@@ -272,6 +272,61 @@
272
272
  });
273
273
  }
274
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
+
275
330
  async function run() {
276
331
  if (inFlight) { rerunRequested = true; return; }
277
332
  inFlight = true;
@@ -287,19 +342,22 @@
287
342
  const cfg = await fetchConfig();
288
343
  if (!cfg || cfg.excluded) return;
289
344
 
345
+
290
346
  if (isTopicPage()) {
291
- 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
+ }
292
354
  } else {
293
- // category/topic list pages may render after ajaxify end; retry a few times
294
- const hasList = document.querySelectorAll('li[component="category/topic"]').length > 0;
355
+ const hasList = getTopicListItemsFast() > 0;
295
356
  if (hasList) {
296
357
  injectBetweenTopics(cfg);
297
358
  } else {
298
- window.__ezoicCatRetry = window.__ezoicCatRetry || 0;
299
- if (window.__ezoicCatRetry < 20) {
300
- window.__ezoicCatRetry++;
301
- setTimeout(run, 250);
302
- }
359
+ observeUntilTargets('category', function () { run(); });
360
+ scheduleRetry('category:' + getPageKey());
303
361
  }
304
362
  }
305
363
  } catch (e) {