nodebb-plugin-ezoic-infinite 0.4.5 → 0.5.0

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 +37 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-ezoic-infinite",
3
- "version": "0.4.5",
3
+ "version": "0.5.0",
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
@@ -16,26 +16,34 @@ async function fetchConfig() {
16
16
  function parsePool(raw) {
17
17
  if (!raw) return [];
18
18
  return Array.from(new Set(
19
- raw.split(/[\n,;\s]+/)
19
+ String(raw).split(/[\n,;\s]+/)
20
20
  .map(x => parseInt(x, 10))
21
21
  .filter(n => Number.isFinite(n) && n > 0)
22
22
  ));
23
23
  }
24
24
 
25
- function isTopicPage() {
26
- return ajaxify?.data?.template?.name === 'topic' || $('body').hasClass('page-topic');
25
+ /**
26
+ * Harmony (topic page) selector:
27
+ * - Posts: [component="post"]
28
+ */
29
+ function getTopicPosts() {
30
+ const $p = $('[component="post"]').not('.ezoic-ad-post');
31
+ if ($p.length) return $p;
32
+ // fallback
33
+ return $('.posts .post').not('.ezoic-ad-post');
27
34
  }
28
35
 
29
36
  function removePlaceholdersByPool(pool) {
30
37
  pool.forEach(id => $('#ezoic-pub-ad-placeholder-' + id).remove());
31
38
  }
32
39
 
33
- function removeAdMessageWrappers() {
40
+ function removeAdWrappers() {
34
41
  $('.ezoic-ad-post').remove();
42
+ $('.ezoic-ad-between').remove();
35
43
  }
36
44
 
37
- function computeWindowSlots(totalPosts, interval, poolSize) {
38
- const slots = Math.floor(totalPosts / interval);
45
+ function computeWindowSlots(totalItems, interval, poolSize) {
46
+ const slots = Math.floor(totalItems / interval);
39
47
  if (slots <= 0) return [];
40
48
  const start = Math.max(1, slots - poolSize + 1);
41
49
  const out = [];
@@ -43,13 +51,8 @@ function computeWindowSlots(totalPosts, interval, poolSize) {
43
51
  return out;
44
52
  }
45
53
 
46
- function insertBetweenPosts(pool, interval) {
47
- if (!isTopicPage()) return [];
48
-
49
- const $posts = $('.posts .post').not('.ezoic-ad-post');
54
+ function insertBetweenPosts($posts, pool, interval) {
50
55
  const total = $posts.length;
51
- if (!total) return [];
52
-
53
56
  const slotsToRender = computeWindowSlots(total, interval, pool.length);
54
57
  if (!slotsToRender.length) return [];
55
58
 
@@ -57,23 +60,18 @@ function insertBetweenPosts(pool, interval) {
57
60
  for (let i = 0; i < slotsToRender.length; i++) {
58
61
  const slotNumber = slotsToRender[i];
59
62
  const id = pool[i];
60
- const postIndex = slotNumber * interval - 1;
61
- const $target = $posts.eq(postIndex);
63
+ const index = slotNumber * interval - 1;
64
+ const $target = $posts.eq(index);
62
65
  if (!$target.length) continue;
63
66
 
64
- $target.after('<div id="ezoic-pub-ad-placeholder-' + id + '"></div>');
67
+ $target.after('<div class="ezoic-ad-between" id="ezoic-pub-ad-placeholder-' + id + '"></div>');
65
68
  activeIds.push(id);
66
69
  }
67
70
  return activeIds;
68
71
  }
69
72
 
70
- function insertAdMessages(pool, interval) {
71
- if (!isTopicPage()) return [];
72
-
73
- const $posts = $('.posts .post').not('.ezoic-ad-post');
73
+ function insertAdMessagesBetweenReplies($posts, pool, interval) {
74
74
  const total = $posts.length;
75
- if (!total) return [];
76
-
77
75
  const slotsToRender = computeWindowSlots(total, interval, pool.length);
78
76
  if (!slotsToRender.length) return [];
79
77
 
@@ -81,8 +79,8 @@ function insertAdMessages(pool, interval) {
81
79
  for (let i = 0; i < slotsToRender.length; i++) {
82
80
  const slotNumber = slotsToRender[i];
83
81
  const id = pool[i];
84
- const postIndex = slotNumber * interval - 1;
85
- const $target = $posts.eq(postIndex);
82
+ const index = slotNumber * interval - 1;
83
+ const $target = $posts.eq(index);
86
84
  if (!$target.length) continue;
87
85
 
88
86
  const html =
@@ -101,7 +99,6 @@ function insertAdMessages(pool, interval) {
101
99
  async function refreshAds() {
102
100
  const cfg = await fetchConfig();
103
101
  if (!cfg || cfg.excluded) return;
104
- if (!isTopicPage()) return;
105
102
 
106
103
  const betweenPool = parsePool(cfg.placeholderIds);
107
104
  const betweenInterval = Math.max(1, parseInt(cfg.intervalPosts, 10) || 6);
@@ -109,24 +106,33 @@ async function refreshAds() {
109
106
  const messagePool = parsePool(cfg.messagePlaceholderIds);
110
107
  const messageInterval = Math.max(1, parseInt(cfg.messageIntervalPosts, 10) || 3);
111
108
 
112
- removeAdMessageWrappers();
109
+ const $posts = getTopicPosts();
110
+ if (!$posts.length) return; // only topic pages
111
+
112
+ // Clean first
113
+ removeAdWrappers();
113
114
  removePlaceholdersByPool(betweenPool);
114
115
  removePlaceholdersByPool(messagePool);
115
116
 
116
117
  const activeIds = [];
117
118
 
119
+ // Between posts
118
120
  if (cfg.enableBetweenAds && betweenPool.length) {
119
- activeIds.push(...insertBetweenPosts(betweenPool, betweenInterval));
121
+ activeIds.push(...insertBetweenPosts($posts, betweenPool, betweenInterval));
120
122
  }
121
123
 
124
+ // "Ad message" between replies
122
125
  if (cfg.enableMessageAds && messagePool.length) {
123
- activeIds.push(...insertAdMessages(messagePool, messageInterval));
126
+ activeIds.push(...insertAdMessagesBetweenReplies($posts, messagePool, messageInterval));
124
127
  }
125
128
 
126
- if (activeIds.length && window.ezstandalone?.destroyPlaceholders) {
127
- window.ezstandalone.destroyPlaceholders();
129
+ // Ezoic render
130
+ if (activeIds.length && window.ezstandalone && typeof window.ezstandalone.destroyPlaceholders === 'function') {
131
+ try { window.ezstandalone.destroyPlaceholders(); } catch (e) {}
128
132
  }
129
- activeIds.forEach(id => window.ezstandalone?.showAds?.(id));
133
+ activeIds.forEach(id => {
134
+ try { window.ezstandalone && typeof window.ezstandalone.showAds === 'function' && window.ezstandalone.showAds(id); } catch (e) {}
135
+ });
130
136
  }
131
137
 
132
138
  function debounceRefresh() {
@@ -135,3 +141,4 @@ function debounceRefresh() {
135
141
  }
136
142
 
137
143
  $(window).on('action:ajaxify.end action:posts.loaded action:topic.loaded', debounceRefresh);
144
+ setTimeout(debounceRefresh, 800);