nodebb-plugin-ezoic-infinite 1.6.91 → 1.6.92

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 +58 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "1.6.91",
3
+ "version": "1.6.92",
4
4
  "description": "Production-ready Ezoic infinite ads integration for NodeBB 4.x",
5
5
  "main": "library.js",
6
6
  "license": "MIT",
package/public/client.js CHANGED
@@ -9,8 +9,9 @@
9
9
 
10
10
  let config = null;
11
11
  let isInternalChange = false;
12
+ let activeIdsOnPage = new Set();
12
13
  let pendingIds = new Set();
13
- let refreshTimer = null;
14
+ let triggerTimer = null;
14
15
 
15
16
  function getPool() {
16
17
  let p = document.getElementById(POOL_ID);
@@ -30,16 +31,21 @@
30
31
  const idsToProcess = Array.from(pendingIds);
31
32
  pendingIds.clear();
32
33
 
33
- // On enregistre les IDs
34
+ // 1. Définir les IDs (requis par Ezoic avant l'affichage)
34
35
  window.ezstandalone.define(idsToProcess);
35
36
 
36
- // On appelle UNIQUEMENT refresh.
37
- // Si Ezoic n'est pas "enabled", il ignorera l'appel sans erreur bloquante.
38
- try {
39
- window.ezstandalone.refresh();
40
- } catch (e) {
41
- console.debug('[Ezoic] Refresh deferred');
37
+ if (!window.ezstandalone.enabled) {
38
+ // PREMIER CHARGEMENT
39
+ window.ezstandalone.enable();
40
+ window.ezstandalone.showAds(); // Appelle tout ce qui est présent
41
+ } else {
42
+ // NOUVEAU CONTENU (INFINITE SCROLL)
43
+ // La doc dit : ezstandalone.showAds(104, 105);
44
+ window.ezstandalone.showAds.apply(null, idsToProcess);
42
45
  }
46
+
47
+ // On garde trace pour le nettoyage futur
48
+ idsToProcess.forEach(id => activeIdsOnPage.add(id));
43
49
  });
44
50
  }
45
51
 
@@ -47,29 +53,42 @@
47
53
  const pid = parseInt(id, 10);
48
54
  if (isNaN(pid)) return;
49
55
  pendingIds.add(pid);
56
+
57
+ // On attend un peu pour grouper si plusieurs pubs arrivent en même temps
58
+ clearTimeout(triggerTimer);
59
+ triggerTimer = setTimeout(triggerEzoic, 200);
60
+ }
50
61
 
51
- clearTimeout(refreshTimer);
52
- refreshTimer = setTimeout(triggerEzoic, 300);
62
+ // "Changing Pages" : Nettoyage complet lors de la navigation NodeBB
63
+ function onPageChangeStart() {
64
+ if (typeof window.ezstandalone !== 'undefined') {
65
+ window.ezstandalone.cmd.push(function() {
66
+ // On nettoie tout avant de charger la nouvelle page
67
+ if (typeof window.ezstandalone.destroyAll === 'function') {
68
+ window.ezstandalone.destroyAll();
69
+ }
70
+ activeIdsOnPage.clear();
71
+ });
72
+ }
53
73
  }
54
74
 
55
75
  function redistribute() {
56
76
  if (!config || config.excluded) return;
57
77
 
58
- // 1. Accueil (Catégories)
59
- const categoryItems = document.querySelectorAll('.category-item, [component="categories/category"]');
60
- // 2. Liste Topics
61
- const topicItems = document.querySelectorAll('li[component="category/topic"]');
62
- // 3. Messages
63
- const postItems = document.querySelectorAll('[component="post"]');
78
+ const selectors = {
79
+ 'home': '.category-item, [component="categories/category"]',
80
+ 'topic-list': 'li[component="category/topic"]',
81
+ 'message': '[component="post"]'
82
+ };
64
83
 
65
- if (categoryItems.length > 0 && config.enableCategoryAds) {
66
- process(Array.from(categoryItems), 'home', config.intervalCategories, config.showFirstCategoryAd);
84
+ if (config.enableCategoryAds) {
85
+ process(Array.from(document.querySelectorAll(selectors.home)), 'home', config.intervalCategories, config.showFirstCategoryAd);
67
86
  }
68
- if (topicItems.length > 0 && config.enableBetweenAds) {
69
- process(Array.from(topicItems), 'topic-list', config.intervalPosts, config.showFirstTopicAd);
87
+ if (config.enableBetweenAds) {
88
+ process(Array.from(document.querySelectorAll(selectors['topic-list'])), 'topic-list', config.intervalPosts, config.showFirstTopicAd);
70
89
  }
71
- if (postItems.length > 0 && config.enableMessageAds) {
72
- process(Array.from(postItems), 'message', config.messageIntervalPosts, config.showFirstMessageAd);
90
+ if (config.enableMessageAds) {
91
+ process(Array.from(document.querySelectorAll(selectors.message)), 'message', config.messageIntervalPosts, config.showFirstMessageAd);
73
92
  }
74
93
  }
75
94
 
@@ -79,14 +98,16 @@
79
98
  const pos = index + 1;
80
99
  const shouldHaveAd = (pos === 1 && showFirst) || (pos % int === 0);
81
100
  const next = item.nextElementSibling;
101
+
82
102
  if (shouldHaveAd && !(next && next.classList.contains(WRAP_CLASS))) {
83
103
  const pool = getPool();
84
104
  const available = pool.querySelector(`.${WRAP_CLASS}[data-kind="${kind}"]`);
105
+
85
106
  if (available) {
86
107
  isInternalChange = true;
87
108
  item.parentNode.insertBefore(available, item.nextSibling);
88
109
  callEzoic(available.getAttribute('data-placeholder-id'));
89
- setTimeout(() => { isInternalChange = false; }, 100);
110
+ setTimeout(() => { isInternalChange = false; }, 50);
90
111
  }
91
112
  }
92
113
  });
@@ -101,17 +122,20 @@
101
122
  const setup = (raw, kind) => {
102
123
  if (!raw) return;
103
124
  raw.split(/[\s,]+/).filter(Boolean).forEach(id => {
104
- const d = document.createElement('div');
105
- d.className = WRAP_CLASS;
106
- d.setAttribute('data-kind', kind);
107
- d.setAttribute('data-placeholder-id', id);
108
- d.innerHTML = `<div id="ezoic-pub-ad-placeholder-${id}"></div>`;
109
- pool.appendChild(d);
125
+ if (!pool.querySelector(`[data-placeholder-id="${id}"]`)) {
126
+ const d = document.createElement('div');
127
+ d.className = WRAP_CLASS;
128
+ d.setAttribute('data-kind', kind);
129
+ d.setAttribute('data-placeholder-id', id);
130
+ d.innerHTML = `<div id="ezoic-pub-ad-placeholder-${id}"></div>`;
131
+ pool.appendChild(d);
132
+ }
110
133
  });
111
134
  };
112
135
  setup(config.categoryPlaceholderIds, 'home');
113
136
  setup(config.placeholderIds, 'topic-list');
114
137
  setup(config.messagePlaceholderIds, 'message');
138
+
115
139
  redistribute();
116
140
 
117
141
  const observer = new MutationObserver(() => {
@@ -121,7 +145,11 @@
121
145
  });
122
146
  }
123
147
 
124
- window.addEventListener('action:ajaxify.end', () => setTimeout(redistribute, 500));
148
+ // Événements NodeBB
149
+ window.addEventListener('action:ajaxify.start', onPageChangeStart);
150
+ window.addEventListener('action:ajaxify.end', () => {
151
+ setTimeout(redistribute, 500);
152
+ });
125
153
 
126
154
  if (document.readyState === 'loading') {
127
155
  document.addEventListener('DOMContentLoaded', init);