Spaces:
Running
Running
Upload static/hot_multi.js
Browse files- static/hot_multi.js +41 -12
static/hot_multi.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
// === OVERRIDE: loadHotTopics loads from MULTIPLE hashtags ===
|
| 2 |
// This file loaded AFTER app_v2.js, overrides the function
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
async function loadHotTopics(){
|
| 5 |
const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));
|
| 6 |
const el=document.getElementById('hot-topics');if(!el)return;
|
|
@@ -9,9 +13,9 @@ async function loadHotTopics(){
|
|
| 9 |
const topicText=t.topic||t.label.replace(/^#/,'');
|
| 10 |
return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;
|
| 11 |
}).join('');
|
| 12 |
-
// Load tin HOT = tổng hợp từ TOP
|
| 13 |
if(topics.length>=2){
|
| 14 |
-
loadMultiHashtag(topics.slice(0,
|
| 15 |
}else if(topics.length){
|
| 16 |
searchTopic(topics[0].topic||topics[0].label.replace(/^#/,''));
|
| 17 |
}
|
|
@@ -33,17 +37,42 @@ async function loadMultiHashtag(topicList){
|
|
| 33 |
if(src&&src.url&&!seen.has(src.url)){seen.add(src.url);src._topic=topicList[j];all.push(src);}
|
| 34 |
}
|
| 35 |
}
|
|
|
|
|
|
|
| 36 |
if(!all.length){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#888;padding:8px">Đang cập nhật...</div></div>`;return;}
|
| 37 |
-
|
| 38 |
-
all.slice(0,12).forEach((s,i)=>{
|
| 39 |
-
h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${i}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')} · <span style="color:#f0c040;font-size:9px">#${esc(s._topic||'')}</span></div></div></div>`;
|
| 40 |
-
});
|
| 41 |
-
h+=`</div><div style="display:flex;gap:4px;flex-wrap:wrap;margin-top:8px">`;
|
| 42 |
-
topicList.forEach(t=>{h+=`<button class="hot-chip" onclick="searchTopic('${t.replace(/'/g,"\\'")}')" style="font-size:10px">🔍 #${esc(t)}</button>`;});
|
| 43 |
-
h+=`</div></div>`;
|
| 44 |
-
box.innerHTML=h;
|
| 45 |
-
// Lazy load images
|
| 46 |
-
all.slice(0,12).forEach((s,i)=>{if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+i);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});
|
| 47 |
_htTopic=topicList[0];
|
| 48 |
}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#e74c3c;padding:8px">Lỗi tải tin</div></div>`;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
|
|
|
| 1 |
// === OVERRIDE: loadHotTopics loads from MULTIPLE hashtags ===
|
| 2 |
// This file loaded AFTER app_v2.js, overrides the function
|
| 3 |
|
| 4 |
+
let _htAll = []; // All articles from multi-hashtag, for lazy loading
|
| 5 |
+
let _htPage = 0;
|
| 6 |
+
const _HT_PER_PAGE = 12;
|
| 7 |
+
|
| 8 |
async function loadHotTopics(){
|
| 9 |
const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));
|
| 10 |
const el=document.getElementById('hot-topics');if(!el)return;
|
|
|
|
| 13 |
const topicText=t.topic||t.label.replace(/^#/,'');
|
| 14 |
return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;
|
| 15 |
}).join('');
|
| 16 |
+
// Load tin HOT = tổng hợp từ TOP 5 hashtag nóng nhất (có AI Thế Giới & AI Việt Nam)
|
| 17 |
if(topics.length>=2){
|
| 18 |
+
loadMultiHashtag(topics.slice(0,5).map(t=>t.topic||t.label.replace(/^#/,'')));
|
| 19 |
}else if(topics.length){
|
| 20 |
searchTopic(topics[0].topic||topics[0].label.replace(/^#/,''));
|
| 21 |
}
|
|
|
|
| 37 |
if(src&&src.url&&!seen.has(src.url)){seen.add(src.url);src._topic=topicList[j];all.push(src);}
|
| 38 |
}
|
| 39 |
}
|
| 40 |
+
_htAll = all;
|
| 41 |
+
_htPage = 0;
|
| 42 |
if(!all.length){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#888;padding:8px">Đang cập nhật...</div></div>`;return;}
|
| 43 |
+
_renderHotPage(box, topicList);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
_htTopic=topicList[0];
|
| 45 |
}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#e74c3c;padding:8px">Lỗi tải tin</div></div>`;}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
function _renderHotPage(box, topicList){
|
| 49 |
+
const start = 0;
|
| 50 |
+
const end = (_htPage + 1) * _HT_PER_PAGE;
|
| 51 |
+
const pageItems = _htAll.slice(0, end);
|
| 52 |
+
let h=`<div class="hashtag-sources"><h3>🔥 Tin HOT tổng hợp <span style="font-size:10px;color:#888">(${_htAll.length} bài · ${topicList.length} chủ đề)</span></h3><div id="ht-list">`;
|
| 53 |
+
pageItems.forEach((s,i)=>{
|
| 54 |
+
h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${i}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')} · <span style="color:#f0c040;font-size:9px">#${esc(s._topic||'')}</span></div></div></div>`;
|
| 55 |
+
});
|
| 56 |
+
h+=`</div><div style="display:flex;gap:4px;flex-wrap:wrap;margin-top:8px">`;
|
| 57 |
+
topicList.forEach(t=>{h+=`<button class="hot-chip" onclick="searchTopic('${t.replace(/'/g,"\\'")}')" style="font-size:10px">🔍 #${esc(t)}</button>`;});
|
| 58 |
+
h+=`</div>`;
|
| 59 |
+
// Nút lazy load - tải thêm tin
|
| 60 |
+
if (_htAll.length > end) {
|
| 61 |
+
h+=`<button class="hashtag-load-more" id="ht-load-more" onclick="_loadMoreHot()">Tải thêm ${Math.min(_HT_PER_PAGE, _htAll.length - end)} tin ▼</button>`;
|
| 62 |
+
}
|
| 63 |
+
h+=`</div>`;
|
| 64 |
+
box.innerHTML=h;
|
| 65 |
+
// Lazy load images
|
| 66 |
+
pageItems.forEach((s,i)=>{if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+i);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
function _loadMoreHot(){
|
| 70 |
+
_htPage++;
|
| 71 |
+
const box = document.getElementById('hashtag-box');
|
| 72 |
+
if(!box)return;
|
| 73 |
+
// Extract topicList from the current buttons
|
| 74 |
+
const chipBtns = box.querySelectorAll('.hot-chip');
|
| 75 |
+
const topicList = Array.from(chipBtns).map(b => b.textContent.replace(/^🔍\s*#/, '').trim());
|
| 76 |
+
_renderHotPage(box, topicList.length ? topicList : [_htTopic]);
|
| 77 |
+
toast('📰 Đã tải thêm tin');
|
| 78 |
}
|