Spaces:
Running
Running
Delete static/shorts_fresh.js with huggingface_hub
Browse files- static/shorts_fresh.js +0 -222
static/shorts_fresh.js
DELETED
|
@@ -1,222 +0,0 @@
|
|
| 1 |
-
/**
|
| 2 |
-
* VNEWS Shorts & Wall 24h Filter - v8.1
|
| 3 |
-
* Runs after app_v2.js to fix Short AI/Wall display
|
| 4 |
-
*/
|
| 5 |
-
(function() {
|
| 6 |
-
'use strict';
|
| 7 |
-
|
| 8 |
-
const MAX_AGE_MS = 86400000; // 24h
|
| 9 |
-
|
| 10 |
-
function toast(msg) {
|
| 11 |
-
let t = document.getElementById('short-progress-toast');
|
| 12 |
-
if (!t) {
|
| 13 |
-
t = document.createElement('div');
|
| 14 |
-
t.id = 'short-progress-toast';
|
| 15 |
-
t.style.cssText = 'position:fixed;bottom:70px;left:50%;transform:translateX(-50%);background:#2d8659;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;z-index:99998;box-shadow:0 4px 12px rgba(0,0,0,.4);display:none;white-space:nowrap';
|
| 16 |
-
document.body.appendChild(t);
|
| 17 |
-
}
|
| 18 |
-
t.textContent = msg; t.style.display = 'block';
|
| 19 |
-
setTimeout(() => { t.style.display = 'none'; }, 4000);
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
function esc(s) {
|
| 23 |
-
return String(s||'').replace(/[&<>"']/g, m => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[m]));
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
function proxyImg(url) {
|
| 27 |
-
if (!url || typeof url !== 'string') return '';
|
| 28 |
-
if (url.startsWith('http') && !url.includes(location.host)) {
|
| 29 |
-
return '/api/proxy/img?url=' + encodeURIComponent(url);
|
| 30 |
-
}
|
| 31 |
-
return url;
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
// Filter posts by 24h
|
| 35 |
-
function filter24h(posts) {
|
| 36 |
-
const now = Date.now();
|
| 37 |
-
return posts.filter(p => {
|
| 38 |
-
const created = p.created || 0;
|
| 39 |
-
// Handle both ms and s timestamps
|
| 40 |
-
const ts = created > 1000000000000 ? created : created * 1000;
|
| 41 |
-
return (now - ts) <= MAX_AGE_MS;
|
| 42 |
-
});
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
// Fetch shorts from RSS (freshest)
|
| 46 |
-
async function fetchShorts() {
|
| 47 |
-
try {
|
| 48 |
-
const r = await fetch('/api/shorts/rss?t=' + Date.now());
|
| 49 |
-
if (r.ok) {
|
| 50 |
-
const data = await r.json();
|
| 51 |
-
if (data.shorts && data.shorts.length > 0) {
|
| 52 |
-
return data.shorts.filter(s => s.channel && (s.channel === 'baodantri7941' || s.channel === 'baosuckhoedoisongboyte'));
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
} catch(e) {}
|
| 56 |
-
|
| 57 |
-
// Fallback to /api/shorts
|
| 58 |
-
try {
|
| 59 |
-
const r = await fetch('/api/shorts?t=' + Date.now());
|
| 60 |
-
if (r.ok) {
|
| 61 |
-
const data = await r.json();
|
| 62 |
-
if (Array.isArray(data)) return data;
|
| 63 |
-
}
|
| 64 |
-
} catch(e) {}
|
| 65 |
-
|
| 66 |
-
return [];
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
function interleaveShorts(shorts) {
|
| 70 |
-
const dt = shorts.filter(s => s.channel === 'baodantri7941');
|
| 71 |
-
const sk = shorts.filter(s => s.channel === 'baosuckhoedoisongboyte');
|
| 72 |
-
const result = []; let i = 0, j = 0;
|
| 73 |
-
while (i < dt.length || j < sk.length) {
|
| 74 |
-
if (i < dt.length) result.push(dt[i++]);
|
| 75 |
-
if (j < sk.length) result.push(sk[j++]);
|
| 76 |
-
}
|
| 77 |
-
return result;
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
function renderShortsSlide(shorts, afterEl) {
|
| 81 |
-
// Find existing slide or create new
|
| 82 |
-
let existingSlide = document.getElementById('shorts-live-slide');
|
| 83 |
-
|
| 84 |
-
const mixed = interleaveShorts(shorts);
|
| 85 |
-
if (!mixed.length) return;
|
| 86 |
-
|
| 87 |
-
const wrap = document.createElement('div');
|
| 88 |
-
wrap.className = 'slider-wrap';
|
| 89 |
-
wrap.id = 'shorts-live-slide';
|
| 90 |
-
let h = '<div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS (24h)</span><span class="slider-note">🟢 vừa cập nhật</span></div><div class="slider-track">';
|
| 91 |
-
mixed.slice(0, 25).forEach((a, i) => {
|
| 92 |
-
const badge = a.channel === 'baosuckhoedoisongboyte' ? 'SKĐS' : 'Dân trí';
|
| 93 |
-
h += `<div class="slider-item shorts-item" onclick="openYTShortsFeed(${i})"><div class="slider-thumb shorts-thumb">${a.img ? `<img src="${esc(proxyImg(a.img))}" loading="lazy">` : ''}<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">${badge}</span> ${esc(a.title)}</div></div>`;
|
| 94 |
-
});
|
| 95 |
-
h += '</div>';
|
| 96 |
-
wrap.innerHTML = h;
|
| 97 |
-
|
| 98 |
-
const homeEl = document.getElementById('view-home');
|
| 99 |
-
if (!existingSlide && homeEl && afterEl) {
|
| 100 |
-
// Insert after afterEl (which is #home-after-wc)
|
| 101 |
-
homeEl.insertBefore(wrap, afterEl.nextSibling);
|
| 102 |
-
}
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
function renderWall24h(afterEl) {
|
| 106 |
-
if (!window._wallPosts || !window._wallPosts.length || !afterEl) return;
|
| 107 |
-
|
| 108 |
-
// Filter to 24h
|
| 109 |
-
const posts = filter24h(window._wallPosts).slice(0, 20);
|
| 110 |
-
if (!posts.length) return;
|
| 111 |
-
|
| 112 |
-
// Short AI (videos)
|
| 113 |
-
const aiShorts = posts.filter(p => p.video);
|
| 114 |
-
if (aiShorts.length) {
|
| 115 |
-
let wrap = document.getElementById('short-ai-section');
|
| 116 |
-
if (wrap) wrap.remove();
|
| 117 |
-
|
| 118 |
-
wrap = document.createElement('div');
|
| 119 |
-
wrap.className = 'slider-wrap';
|
| 120 |
-
wrap.id = 'short-ai-section';
|
| 121 |
-
let h = '<div class="slider-header"><span class="slider-label">🎬 Short AI (24h)</span></div><div class="slider-track">';
|
| 122 |
-
aiShorts.forEach((p, i) => {
|
| 123 |
-
h += `<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
|
| 124 |
-
});
|
| 125 |
-
h += '</div>';
|
| 126 |
-
wrap.innerHTML = h;
|
| 127 |
-
afterEl.parentNode.insertBefore(wrap, afterEl);
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
// AI Wall (text posts)
|
| 131 |
-
wrap = document.getElementById('ai-wall-wrap');
|
| 132 |
-
if (wrap) wrap.remove();
|
| 133 |
-
|
| 134 |
-
wrap = document.createElement('div');
|
| 135 |
-
wrap.className = 'slider-wrap';
|
| 136 |
-
wrap.id = 'ai-wall-wrap';
|
| 137 |
-
let h = '<div class="slider-header"><span class="slider-label">🧱 Tường AI (24h)</span></div><div class="slider-track" id="ai-wall-track">';
|
| 138 |
-
posts.forEach((p, i) => {
|
| 139 |
-
h += `<div class="wall-item" id="wall-item-${esc(p.id)}"><div class="wall-thumb">${p.img ? `<img src="${proxyImg(p.img)}">` : (p.video ? `<video src="${esc(p.video)}" muted></video>` : '')}</div><div class="wall-title">${esc(p.title)}</div></div>`;
|
| 140 |
-
});
|
| 141 |
-
h += '</div>';
|
| 142 |
-
wrap.innerHTML = h;
|
| 143 |
-
afterEl.parentNode.insertBefore(wrap, afterEl);
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
// Override refreshShorts
|
| 147 |
-
window.refreshShorts = async function() {
|
| 148 |
-
let all = await fetchShorts();
|
| 149 |
-
const seen = new Set();
|
| 150 |
-
const fresh = all.filter(s => seen.has(s.id) ? false : (seen.add(s.id), true));
|
| 151 |
-
|
| 152 |
-
if (fresh.length > 0) {
|
| 153 |
-
window._shortsData = fresh;
|
| 154 |
-
const afterEl = document.getElementById('home-after-wc');
|
| 155 |
-
if (afterEl) renderShortsSlide(fresh, afterEl);
|
| 156 |
-
localStorage.setItem('vnews_shorts', JSON.stringify({ ts: Date.now(), data: fresh }));
|
| 157 |
-
toast('✓ ' + fresh.length + ' shorts (24h)!');
|
| 158 |
-
}
|
| 159 |
-
};
|
| 160 |
-
|
| 161 |
-
// Patch prependWallPost to enforce 24h
|
| 162 |
-
const origPrepend = window.prependWallPost;
|
| 163 |
-
if (origPrepend) {
|
| 164 |
-
window.prependWallPost = function(post) {
|
| 165 |
-
const now = Date.now();
|
| 166 |
-
const created = post.created || Date.now();
|
| 167 |
-
const ts = created > 1000000000000 ? created : created * 1000;
|
| 168 |
-
if ((now - ts) > MAX_AGE_MS) return; // Don't add old posts
|
| 169 |
-
origPrepend.call(this, post);
|
| 170 |
-
};
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
-
// Wait for loadHome to complete, then patch wall
|
| 174 |
-
const origLoadHome = window.loadHome;
|
| 175 |
-
if (origLoadHome) {
|
| 176 |
-
window.loadHome = async function() {
|
| 177 |
-
await origLoadHome.call(this);
|
| 178 |
-
|
| 179 |
-
// Now patch wall rendering
|
| 180 |
-
setTimeout(() => {
|
| 181 |
-
const afterEl = document.getElementById('home-after-wc');
|
| 182 |
-
if (afterEl && window._wallPosts) {
|
| 183 |
-
renderWall24h(afterEl);
|
| 184 |
-
}
|
| 185 |
-
}, 1000);
|
| 186 |
-
};
|
| 187 |
-
}
|
| 188 |
-
|
| 189 |
-
// Also run on DOM ready (backup)
|
| 190 |
-
function initWallFix() {
|
| 191 |
-
if (window._wallPosts && window._wallPosts.length > 0) {
|
| 192 |
-
const afterEl = document.getElementById('home-after-wc');
|
| 193 |
-
if (afterEl) {
|
| 194 |
-
renderWall24h(afterEl);
|
| 195 |
-
toast('🧱 Tường AI đã lọc 24h');
|
| 196 |
-
}
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
// Load cache
|
| 201 |
-
try {
|
| 202 |
-
const c = JSON.parse(localStorage.getItem('vnews_shorts') || '{}');
|
| 203 |
-
if (c.data && c.data.length > 0 && (Date.now() - c.ts) < 7200000) {
|
| 204 |
-
const filtered = filter24h(c.data);
|
| 205 |
-
if (filtered.length > 0) {
|
| 206 |
-
window._shortsData = filtered;
|
| 207 |
-
setTimeout(() => {
|
| 208 |
-
const afterEl = document.getElementById('home-after-wc');
|
| 209 |
-
if (afterEl) renderShortsSlide(filtered, afterEl);
|
| 210 |
-
}, 2000);
|
| 211 |
-
}
|
| 212 |
-
}
|
| 213 |
-
} catch(e) {}
|
| 214 |
-
|
| 215 |
-
// Fetch fresh after load
|
| 216 |
-
setTimeout(() => {
|
| 217 |
-
window.refreshShorts().catch(() => {});
|
| 218 |
-
initWallFix();
|
| 219 |
-
}, 4000);
|
| 220 |
-
|
| 221 |
-
console.log('[Shorts] Fetcher v8.1 - 24h filter active');
|
| 222 |
-
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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