nodebb-plugin-ezoic-infinite 1.0.15 → 1.0.16

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 +23 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
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
@@ -186,16 +186,32 @@
186
186
  }
187
187
 
188
188
  function recycle(fifo, usedSet, selector) {
189
+ // Recycle only ads that are safely outside the viewport (to avoid ads “disappearing” while reading)
190
+ const margin = 1200; // px
191
+ const vpTop = -margin;
192
+
189
193
  fifo.sort((a, b) => a.after - b.after);
190
- while (fifo.length) {
191
- const old = fifo.shift();
194
+
195
+ // Prefer recycling ads far ABOVE the current viewport (user has already passed them)
196
+ for (let i = 0; i < fifo.length; i++) {
197
+ const old = fifo[i];
192
198
  const el = document.querySelector(selector(old));
193
- if (!el) continue;
194
- el.remove();
195
- usedSet.delete(old.id);
196
- destroyPlaceholder(old.id);
197
- return old.id;
199
+ if (!el) {
200
+ fifo.splice(i, 1);
201
+ i--;
202
+ continue;
203
+ }
204
+ const r = el.getBoundingClientRect();
205
+ if (r.bottom < vpTop) {
206
+ fifo.splice(i, 1);
207
+ el.remove();
208
+ usedSet.delete(old.id);
209
+ destroyPlaceholder(old.id);
210
+ return old.id;
211
+ }
198
212
  }
213
+
214
+ // If nothing is safely above, do NOT recycle.
199
215
  return null;
200
216
  }
201
217