nodebb-plugin-ezoic-infinite 1.0.10 → 1.0.11

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 +32 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
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
@@ -121,18 +121,38 @@
121
121
  function callShowAdsSingle(id) {
122
122
  if (!id) return;
123
123
 
124
+ // Normalize id key
125
+ const key = String(id);
126
+
127
+ // Ensure we never call showAds twice for the same id within a short window
124
128
  const now = Date.now();
125
129
  window.__ezoicLastSingle = window.__ezoicLastSingle || {};
126
- const last = window.__ezoicLastSingle[id] || 0;
130
+ const last = window.__ezoicLastSingle[key] || 0;
127
131
  if (now - last < 4000) return;
128
- window.__ezoicLastSingle[id] = now;
132
+
133
+ // If showAds is ready now, call once and exit (no cmd + no retry)
134
+ try {
135
+ window.ezstandalone = window.ezstandalone || {};
136
+ if (typeof window.ezstandalone.showAds === 'function') {
137
+ window.__ezoicLastSingle[key] = now;
138
+ window.ezstandalone.showAds(id);
139
+ return;
140
+ }
141
+ } catch (e) {}
142
+
143
+ // Otherwise: schedule a single pending call via cmd + retries until it succeeds
144
+ window.__ezoicPending = window.__ezoicPending || {};
145
+ if (window.__ezoicPending[key]) return;
146
+ window.__ezoicPending[key] = true;
129
147
 
130
148
  window.ezstandalone = window.ezstandalone || {};
131
149
  window.ezstandalone.cmd = window.ezstandalone.cmd || [];
132
150
 
133
- const run = function () {
151
+ const tryRun = function () {
134
152
  try {
135
153
  if (typeof window.ezstandalone.showAds === 'function') {
154
+ window.__ezoicLastSingle[key] = Date.now();
155
+ delete window.__ezoicPending[key];
136
156
  window.ezstandalone.showAds(id);
137
157
  return true;
138
158
  }
@@ -140,12 +160,17 @@
140
160
  return false;
141
161
  };
142
162
 
143
- window.ezstandalone.cmd.push(function () { run(); });
163
+ // When Ezoic finishes loading, it will drain cmd; run once there
164
+ window.ezstandalone.cmd.push(function () { tryRun(); });
144
165
 
166
+ // Retry a few times in case cmd doesn't fire (race conditions)
145
167
  let tries = 0;
146
168
  (function tick() {
147
169
  tries++;
148
- if (run() || tries >= 8) return;
170
+ if (tryRun() || tries >= 8) {
171
+ if (tries >= 8) delete window.__ezoicPending[key];
172
+ return;
173
+ }
149
174
  setTimeout(tick, 800);
150
175
  })();
151
176
  }
@@ -266,7 +291,7 @@
266
291
  injectBetweenTopics(cfg);
267
292
  } else {
268
293
  window.__ezoicCatRetry = window.__ezoicCatRetry || 0;
269
- if (window.__ezoicCatRetry < 10) {
294
+ if (window.__ezoicCatRetry < 20) {
270
295
  window.__ezoicCatRetry++;
271
296
  setTimeout(run, 250);
272
297
  }
@@ -293,7 +318,7 @@ function bindNodeBBEvents() {
293
318
  if (window.jQuery) {
294
319
  const $w = window.jQuery(window);
295
320
  $w.off('.ezoicInfinite');
296
- $w.on('action:ajaxify.end.ezoicInfinite', scheduleRun);
321
+ $w.on('action:ajaxify.end.ezoicInfinite', function(){ scheduleRun(); setTimeout(scheduleRun, 600); });
297
322
  $w.on('action:posts.loaded.ezoicInfinite', scheduleRun);
298
323
  $w.on('action:topic.loaded.ezoicInfinite', scheduleRun);
299
324
  $w.on('action:topics.loaded.ezoicInfinite', scheduleRun);