nodebb-plugin-ezoic-infinite 0.6.4 → 0.6.5

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 +27 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
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
@@ -14,7 +14,7 @@ let rerunRequested = false;
14
14
  let pageKey = null;
15
15
  let injectedSlots = new Set(); // slot numbers already injected on this page
16
16
  let usedIds = new Set(); // IDs currently present in DOM
17
- let adsFIFO = []; // [{slot, id, selector}] in insertion order (oldest first)
17
+ let adsFIFO = []; // [{type, slot, id}] oldest first
18
18
 
19
19
  function resetPageState() {
20
20
  injectedSlots = new Set();
@@ -97,6 +97,19 @@ function pickNextId(pool) {
97
97
  return null;
98
98
  }
99
99
 
100
+ function evictOldestOne() {
101
+ const old = adsFIFO.shift();
102
+ if (!old) return false;
103
+
104
+ const sel = '[data-ezoic-slot="' + old.slot + '"][data-ezoic-id="' + old.id + '"]';
105
+ const $el = $(sel);
106
+ if ($el.length) $el.remove();
107
+
108
+ injectedSlots.delete(old.slot);
109
+ usedIds.delete(old.id);
110
+ return true;
111
+ }
112
+
100
113
  function callEzoic(ids) {
101
114
  if (!ids || !ids.length) return;
102
115
 
@@ -124,21 +137,6 @@ function callEzoic(ids) {
124
137
  }, 800);
125
138
  }
126
139
 
127
- function fifoEvictIfNeeded(poolSize) {
128
- // Keep at most poolSize ads in DOM: remove oldest when we exceed pool capacity.
129
- // This makes ads "follow" the scroll (new ads appear, oldest disappear) without jumping positions.
130
- while (adsFIFO.length > poolSize) {
131
- const old = adsFIFO.shift();
132
- if (!old) break;
133
-
134
- const $el = $(old.selector);
135
- if ($el.length) $el.remove();
136
-
137
- injectedSlots.delete(old.slot);
138
- usedIds.delete(old.id);
139
- }
140
- }
141
-
142
140
  function injectBetweenIncremental($items, pool, interval, wrapperClass) {
143
141
  const total = $items.length;
144
142
  const maxSlot = Math.floor(total / interval);
@@ -153,14 +151,15 @@ function injectBetweenIncremental($items, pool, interval, wrapperClass) {
153
151
  const $target = $items.eq(index);
154
152
  if (!$target.length) continue;
155
153
 
156
- const id = pickNextId(pool);
154
+ let id = pickNextId(pool);
157
155
  if (!id) {
158
- // No free IDs right now; stop. Oldest will be evicted once new ads can be created.
159
- break;
156
+ // Pool full: evict oldest, then reuse freed ID
157
+ if (!evictOldestOne()) break;
158
+ id = pickNextId(pool);
159
+ if (!id) break;
160
160
  }
161
161
 
162
162
  const placeholder = '<div id="ezoic-pub-ad-placeholder-' + id + '"></div>';
163
- const selector = '.ezoic-ad-topic[data-ezoic-slot="' + slot + '"][data-ezoic-id="' + id + '"]';
164
163
  const html = makeWrapperLike(
165
164
  $target,
166
165
  wrapperClass,
@@ -172,12 +171,8 @@ function injectBetweenIncremental($items, pool, interval, wrapperClass) {
172
171
 
173
172
  injectedSlots.add(slot);
174
173
  usedIds.add(id);
175
- adsFIFO.push({ slot: slot, id: id, selector: selector });
176
-
174
+ adsFIFO.push({ type: 'between', slot: slot, id: id });
177
175
  newIds.push(id);
178
-
179
- // Enforce capacity immediately (prevents runaway and keeps ads near viewport)
180
- fifoEvictIfNeeded(pool.length);
181
176
  }
182
177
 
183
178
  return newIds;
@@ -197,11 +192,14 @@ function injectMessageIncremental($posts, pool, interval) {
197
192
  const $target = $posts.eq(index);
198
193
  if (!$target.length) continue;
199
194
 
200
- const id = pickNextId(pool);
201
- if (!id) break;
195
+ let id = pickNextId(pool);
196
+ if (!id) {
197
+ if (!evictOldestOne()) break;
198
+ id = pickNextId(pool);
199
+ if (!id) break;
200
+ }
202
201
 
203
202
  const inner = '<div class="content"><div id="ezoic-pub-ad-placeholder-' + id + '"></div></div>';
204
- const selector = '.ezoic-ad-post[data-ezoic-slot="' + slot + '"][data-ezoic-id="' + id + '"]';
205
203
  const html = makeWrapperLike(
206
204
  $target,
207
205
  'post ezoic-ad-post',
@@ -213,11 +211,8 @@ function injectMessageIncremental($posts, pool, interval) {
213
211
 
214
212
  injectedSlots.add(slot);
215
213
  usedIds.add(id);
216
- adsFIFO.push({ slot: slot, id: id, selector: selector });
217
-
214
+ adsFIFO.push({ type: 'message', slot: slot, id: id });
218
215
  newIds.push(id);
219
-
220
- fifoEvictIfNeeded(pool.length);
221
216
  }
222
217
 
223
218
  return newIds;