nodebb-plugin-ezoic-infinite 0.9.5 → 0.9.6

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 +42 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
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
@@ -92,19 +92,50 @@ function pickNextId(pool) {
92
92
  }
93
93
  return null;
94
94
  }
95
+ function callEzoic(ids) {
96
+ if (!ids || !ids.length) return;
95
97
 
96
- function callEzoic(ids) {
97
- if (!ids || !ids.length) return;
98
+ // de-dupe rapid identical calls
99
+ const key = ids.slice().sort((a, b) => a - b).join(',');
100
+ const now = Date.now();
101
+ if (window.__ezoicLastShowKey === key && now - (window.__ezoicLastShowAt || 0) < 1200) return;
102
+ window.__ezoicLastShowKey = key;
103
+ window.__ezoicLastShowAt = now;
98
104
 
99
- window.ezstandalone = window.ezstandalone || {};
100
- window.ezstandalone.cmd = window.ezstandalone.cmd || [];
101
-
102
- const run = function () {
103
105
  try {
104
- if (typeof window.ezstandalone.showAds === 'function') {
105
- window.ezstandalone.showAds.apply(window.ezstandalone, ids);
106
- return true;
107
- }
106
+ window.ezstandalone = window.ezstandalone || {};
107
+ window.ezstandalone.cmd = window.ezstandalone.cmd || [];
108
+
109
+ const run = function () {
110
+ try {
111
+ // SPA/infinite scroll: reset any previously-defined placeholders before using these IDs
112
+ if (typeof window.ezstandalone.destroyPlaceholders === 'function') {
113
+ ids.forEach(function (id) {
114
+ try { window.ezstandalone.destroyPlaceholders(id); } catch (e) {}
115
+ });
116
+ }
117
+
118
+ if (typeof window.ezstandalone.showAds === 'function') {
119
+ window.ezstandalone.showAds.apply(window.ezstandalone, ids);
120
+ return true;
121
+ }
122
+ } catch (e) {}
123
+ return false;
124
+ };
125
+
126
+ // Ensure destroy->show runs AFTER ezstandalone is ready
127
+ window.ezstandalone.cmd.push(function () { run(); });
128
+
129
+ // retries in case ez loads late
130
+ let tries = 0;
131
+ const tick = function () {
132
+ tries++;
133
+ if (run() || tries >= 10) return;
134
+ setTimeout(tick, 800);
135
+ };
136
+ setTimeout(tick, 800);
137
+ } catch (e) {}
138
+ }
108
139
  } catch (e) {}
109
140
  return false;
110
141
  };
@@ -249,29 +280,4 @@ function debounceRefresh() {
249
280
 
250
281
  $(document).ready(debounceRefresh);
251
282
  $(window).on('action:ajaxify.end action:posts.loaded action:topic.loaded', debounceRefresh);
252
- setTimeout(debounceRefresh, 1800); function removeExistingPlaceholder(id) {
253
- const el = document.getElementById('ezoic-pub-ad-placeholder-' + id);
254
- if (!el) return;
255
- const wrap = el.closest('.ezoic-ad');
256
- if (wrap) {
257
- try { $(wrap).remove(); } catch (e) { wrap.remove(); }
258
- } else {
259
- el.remove();
260
- }
261
- }
262
-
263
- function preDestroyPools() {
264
- try {
265
- const poolA = parsePool(settings && settings.placeholderIds);
266
- const poolB = parsePool(settings && settings.messagePlaceholderIds);
267
- const ids = Array.from(new Set([].concat(poolA, poolB)));
268
- if (!ids.length) return;
269
- ids.forEach(function (id) {
270
- // If Ezoic has already defined the placeholder in this SPA session, destroy it before we re-use it
271
- destroyPlaceholder(id);
272
- removeExistingPlaceholder(id);
273
- });
274
- } catch (e) {}
275
- }
276
-
277
-
283
+ setTimeout(debounceRefresh, 1800);