Spaces:
Running
Running
| // Fix rewriteArticle - call correct endpoint | |
| // This file patches the rewriteArticle function to use /api/rewrite_slide instead of /api/rewrite_share | |
| (function(){ | |
| // Override rewriteArticle to call /api/rewrite_slide | |
| const origRewrite = window.rewriteArticle; | |
| window.rewriteArticle = async function(){ | |
| const url = _currentArticle?.url; | |
| if(!url) return; | |
| toast('⏳ Đang tạo slide tóm tắt...'); | |
| try { | |
| const r = await fetch('/api/rewrite_slide', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({url, context: document.querySelector('.article-view')?.innerText?.slice(0,14000) || ''}) | |
| }); | |
| const j = await r.json(); | |
| if (!r.ok || j.error) throw new Error(j.error); | |
| toast('✅ Đã đăng Tường AI!'); | |
| if (j.post) prependWallPost(j.post); | |
| // Navigate to the new post on Tường AI (home). Slide overlay (if any) stays on top. | |
| if (j.post && typeof goToWallPost === 'function') goToWallPost(j.post.id); | |
| // Show slides preview | |
| if (j.slides && j.slides.length) { | |
| showSlidePreview(j.slides, j.post?.title || ''); | |
| } | |
| } catch(e) { | |
| // Fallback: try /api/rewrite_share (old endpoint from ai_ext) | |
| try { | |
| const r2 = await fetch('/api/rewrite_share', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({url, context: document.querySelector('.article-view')?.innerText?.slice(0,14000) || ''}) | |
| }); | |
| const j2 = await r2.json(); | |
| if (r2.ok && !j2.error) { | |
| toast('✅ Đã đăng Tường AI!'); | |
| if (j2.post) prependWallPost(j2.post); | |
| if (j2.post && typeof goToWallPost === 'function') goToWallPost(j2.post.id); | |
| return; | |
| } | |
| } catch(e2) {} | |
| toast('❌ ' + e.message); | |
| } | |
| }; | |
| // Show slides as fullscreen overlay | |
| window.showSlidePreview = function(slides, title) { | |
| if (!slides || !slides.length) return; | |
| const overlay = document.createElement('div'); | |
| overlay.id = 'slide-preview'; | |
| overlay.style.cssText = 'position:fixed;inset:0;background:#000;z-index:99999;display:flex;flex-direction:column;overflow:hidden'; | |
| let currentSlide = 0; | |
| function renderSlide(idx) { | |
| const s = slides[idx]; | |
| overlay.innerHTML = ` | |
| <div style="position:absolute;top:10px;left:10px;right:10px;display:flex;justify-content:space-between;align-items:center;z-index:2"> | |
| <button onclick="document.getElementById('slide-preview').remove()" style="background:rgba(0,0,0,.6);border:0;color:#fff;padding:8px 14px;border-radius:20px;font-size:12px;cursor:pointer">✕ Đóng</button> | |
| <span style="color:#fff;font-size:11px;background:rgba(0,0,0,.6);padding:4px 10px;border-radius:10px">${idx+1}/${slides.length}</span> | |
| </div> | |
| <div style="flex:1;display:flex;align-items:center;justify-content:center;padding:20px"> | |
| ${s.image ? `<img src="${esc(s.image)}" style="max-width:100%;max-height:60vh;border-radius:10px;object-fit:contain" onerror="this.style.display='none'">` : ''} | |
| </div> | |
| <div style="padding:16px 20px;background:linear-gradient(transparent,rgba(0,0,0,.9));min-height:100px"> | |
| <p style="color:#fff;font-size:14px;line-height:1.6">${esc(s.text)}</p> | |
| </div> | |
| <div style="display:flex;gap:10px;padding:10px 20px 20px;justify-content:center"> | |
| <button onclick="prevSlide()" style="background:#333;border:0;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;cursor:pointer" ${idx===0?'disabled style="opacity:.3"':''}>← Trước</button> | |
| <button onclick="nextSlide()" style="background:#2d8659;border:0;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;cursor:pointer" ${idx===slides.length-1?'disabled style="opacity:.3"':''}>Tiếp →</button> | |
| </div> | |
| `; | |
| } | |
| window.nextSlide = function() { if (currentSlide < slides.length - 1) { currentSlide++; renderSlide(currentSlide); } }; | |
| window.prevSlide = function() { if (currentSlide > 0) { currentSlide--; renderSlide(currentSlide); } }; | |
| renderSlide(0); | |
| document.body.appendChild(overlay); | |
| // Swipe support | |
| let startX = 0; | |
| overlay.addEventListener('touchstart', e => { startX = e.touches[0].clientX; }); | |
| overlay.addEventListener('touchend', e => { | |
| const diff = e.changedTouches[0].clientX - startX; | |
| if (diff < -50) nextSlide(); | |
| else if (diff > 50) prevSlide(); | |
| }); | |
| }; | |
| })(); | |