fix: 恢复 app.js 全部修复 + JS 内联到 HTML 绕过缓存
- 恢复 async function openPdf、getLocalUrl HF 映射、checkSource no-cors - index.html 改为 inline script,不再依赖外部 app.js - 彻底绕过 OpenResty gzip 截断/浏览器缓存问题
This commit is contained in:
155
static/app.js
155
static/app.js
@@ -1,32 +1,37 @@
|
||||
/**
|
||||
* LLM 论文图书馆 — 前端 JS
|
||||
* 页面加载检测 arXiv/HF 连通性 → 底部状态条
|
||||
* 点击论文:arXiv 连通? → iframe 直连 arXiv → 5s 超时 → HK 兜底
|
||||
* IDM 拦就拦,不额外对抗
|
||||
* 打开时 ping 所有论文源 → 底部状态条 → 点击论文直用缓存结果
|
||||
*/
|
||||
|
||||
const API = '/api';
|
||||
|
||||
let modules = {};
|
||||
let moduleData = {};
|
||||
let currentPdf = null;
|
||||
let pdfTimeout = null;
|
||||
let networkStatus = {};
|
||||
let networkStatus = {}; // { arxiv: bool, huggingface: bool, hk: bool }
|
||||
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
const $$ = (sel) => document.querySelectorAll(sel);
|
||||
const TAG_CLASS = { '起点':'tag-start','关键节点':'tag-milestone','前沿':'tag-frontier','前瞻':'tag-forward','支线':'tag-branch' };
|
||||
|
||||
// ══════════════════ INIT ═══════════════════════════════
|
||||
// ═══════════════ INIT ═══════════════════════════════════
|
||||
async function init() {
|
||||
// Build network status bar
|
||||
buildStatusBar();
|
||||
// Load modules
|
||||
try {
|
||||
const resp = await fetch(`${API}/modules`);
|
||||
const mods = await resp.json();
|
||||
modules = {};
|
||||
for (const m of mods) modules[m.id] = m;
|
||||
renderCards(mods);
|
||||
attachGlowTracking();
|
||||
} catch (e) { console.error(e); }
|
||||
|
||||
// Ping sources
|
||||
checkSources();
|
||||
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key === 'Escape') {
|
||||
if ($('#pdfOverlay')?.classList.contains('open')) closePdf();
|
||||
@@ -35,23 +40,28 @@ async function init() {
|
||||
});
|
||||
}
|
||||
|
||||
// ══════════════════ STATUS BAR ════════════════════════
|
||||
// ═══════════════ STATUS BAR ════════════════════════════
|
||||
function buildStatusBar() {
|
||||
const bar = document.createElement('div');
|
||||
bar.id = 'statusBar'; bar.className = 'status-bar';
|
||||
bar.innerHTML = `<span class="status-label">连通性检测</span>
|
||||
bar.id = 'statusBar';
|
||||
bar.className = 'status-bar';
|
||||
bar.innerHTML = `
|
||||
<span class="status-label">连通性检测</span>
|
||||
<span class="status-item" id="status-arxiv"><span class="status-dot"></span> arXiv <span class="status-ms" id="ms-arxiv">—</span></span>
|
||||
<span class="status-item" id="status-hf"><span class="status-dot"></span> HuggingFace <span class="status-ms" id="ms-hf">—</span></span>`;
|
||||
<span class="status-item" id="status-hf"><span class="status-dot"></span> HuggingFace <span class="status-ms" id="ms-hf">—</span></span>
|
||||
`;
|
||||
document.body.appendChild(bar);
|
||||
}
|
||||
|
||||
function setStatus(id, ok, ms, aborted) {
|
||||
networkStatus[id.replace('status-','')] = ok;
|
||||
const el = document.getElementById(id); if (!el) return;
|
||||
el.querySelector('.status-dot').className = 'status-dot ' + (ok ? 'status-ok' : 'status-fail');
|
||||
const el = document.getElementById(id);
|
||||
if (!el) return;
|
||||
const dot = el.querySelector('.status-dot');
|
||||
dot.className = 'status-dot ' + (ok ? 'status-ok' : 'status-fail');
|
||||
const msEl = document.getElementById('ms-' + id.replace('status-',''));
|
||||
if (msEl) {
|
||||
if (aborted) msEl.textContent = '超时';
|
||||
if (aborted) msEl.textContent = '超时 (4s)';
|
||||
else if (ok) msEl.textContent = ms + 'ms';
|
||||
else msEl.textContent = '—';
|
||||
}
|
||||
@@ -60,6 +70,7 @@ function setStatus(id, ok, ms, aborted) {
|
||||
async function checkSource(name, url, statusId) {
|
||||
const start = performance.now();
|
||||
let aborted = false;
|
||||
// Add cache-buster and force no-store to avoid browser caching
|
||||
const probeUrl = url + '?_=' + Date.now();
|
||||
try {
|
||||
const ctrl = new AbortController();
|
||||
@@ -73,26 +84,26 @@ async function checkSource(name, url, statusId) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkSources() {
|
||||
async function checkSources() {
|
||||
checkSource('arxiv', 'https://arxiv.org/favicon.ico', 'status-arxiv');
|
||||
checkSource('hf', 'https://huggingface.co/favicon.ico', 'status-hf');
|
||||
}
|
||||
|
||||
// ══════════════════ GLOW ══════════════════════════════
|
||||
// ═══════════════ GLOW ══════════════════════════════════
|
||||
function attachGlowTracking() {
|
||||
$$('.card').forEach(card => {
|
||||
card.addEventListener('mousemove', e => {
|
||||
const r = card.getBoundingClientRect();
|
||||
card.style.setProperty('--mx', (e.clientX-r.left)/r.width*100+'%');
|
||||
card.style.setProperty('--my', (e.clientY-r.top)/r.height*100+'%');
|
||||
const rect = card.getBoundingClientRect();
|
||||
card.style.setProperty('--mx', ((e.clientX - rect.left) / rect.width * 100) + '%');
|
||||
card.style.setProperty('--my', ((e.clientY - rect.top) / rect.height * 100) + '%');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ══════════════════ CARDS → MODAL → PAPERS ═══════════
|
||||
// ═══════════════ CARDS -> MODAL -> PAPERS ═══════════════
|
||||
function renderCards(mods) {
|
||||
$('#moduleGrid').innerHTML = mods.map(m =>
|
||||
`<div class="card card-${m.id}" onclick="openModule('${m.id}')">
|
||||
$('#moduleGrid').innerHTML = mods.map(m => `
|
||||
<div class="card card-${m.id}" onclick="openModule('${m.id}')">
|
||||
<div class="card-header"><span class="card-icon">${m.icon}</span> ${m.name}</div>
|
||||
<div class="card-desc">${m.desc}</div>
|
||||
<div class="card-badge">${m.area_count} 子领域 · ${m.paper_count} 篇</div>
|
||||
@@ -103,14 +114,15 @@ async function openModule(modId) {
|
||||
let data = moduleData[modId];
|
||||
if (!data) {
|
||||
try { data = await (await fetch(`${API}/modules/${modId}`)).json(); moduleData[modId] = data; }
|
||||
catch { return; }
|
||||
catch (e) { return; }
|
||||
}
|
||||
const mod = modules[modId];
|
||||
$('#modalTitle').innerHTML = `<span class="card-icon">${mod?.icon||''}</span> ${data.name}`;
|
||||
$('#modalSubtitle').textContent = data.desc || '';
|
||||
const areas = data.areas || [];
|
||||
$('#modalTabs').innerHTML = areas.map((a,i)=>`<button class="tab ${i?'':'active'}" onclick="switchArea(${i})">${a.name}</button>`).join('');
|
||||
if (areas.length) { $('#modalTabs').dataset.areaIdx='0'; renderPapers(areas[0]); }
|
||||
$('#modalTabs').innerHTML = areas.map((a,i) =>
|
||||
`<button class="tab ${i===0?'active':''}" onclick="switchArea(${i})">${a.name}</button>`).join('');
|
||||
if (areas.length > 0) { $('#modalTabs').dataset.areaIdx = '0'; renderPapers(areas[0]); }
|
||||
$('#overlay').classList.add('open');
|
||||
}
|
||||
|
||||
@@ -124,21 +136,22 @@ function switchArea(idx) {
|
||||
function closeModal() { $('#overlay').classList.remove('open'); }
|
||||
|
||||
function renderPapers(area) {
|
||||
const s = (l, ps, c) => ps.length ? `<div class="section-label ${c}">${l}</div>`+ps.map(renderPaper).join('') : '';
|
||||
$('#modalContent').innerHTML =
|
||||
s('📌 主线论文', area.mainline||[],'mainline') +
|
||||
s('🌿 支线论文', area.branches||[],'branch') +
|
||||
s('🔮 前瞻探索', area.forward||[],'forward') ||
|
||||
'<p style="color:var(--text-dim);padding:20px;">暂无论文数据</p>';
|
||||
let h = '';
|
||||
function s(l, ps, c) { return ps.length ? `<div class="section-label ${c}">${l}</div>`+ps.map(renderPaper).join('') : ''; }
|
||||
h += s('📌 主线论文', area.mainline||[], 'mainline');
|
||||
h += s('🌿 支线论文', area.branches||[], 'branch');
|
||||
h += s('🔮 前瞻探索', area.forward||[], 'forward');
|
||||
$('#modalContent').innerHTML = h || '<p style="color:var(--text-dim);padding:20px;">暂无论文数据</p>';
|
||||
}
|
||||
|
||||
function renderPaper(p) {
|
||||
const pdfUrl = getPdfLink(p);
|
||||
const id = 'p'+Math.random().toString(36).slice(2,8);
|
||||
const tags = (p.tags||[]).map(t => `<span class="paper-tag ${TAG_CLASS[t]||'tag-branch'}">${t}</span>`).join(' ');
|
||||
const links = [];
|
||||
if (pdfUrl) links.push(`<button class="paper-link" data-pdf="${encodeURIComponent(pdfUrl)}" data-title="${encodeURIComponent(p.title)}" onclick="openPdfBtn(this)">📄 阅读</button>`);
|
||||
else if (p.arxiv) links.push(`<a class="paper-link" href="https://arxiv.org/abs/${p.arxiv}" target="_blank">📋 arXiv</a>`);
|
||||
return `<div class="paper-item"><div class="paper-year">${p.year||'—'}</div><div class="paper-body">
|
||||
return `<div class="paper-item"><div class="paper-year">${p.year||'—'}</div><div class="paper-body" id="${id}">
|
||||
<div class="paper-title">${p.title}</div>
|
||||
<div class="paper-meta"><span>${p.authors||''}</span>${p.venue?`<span class="paper-venue">${p.venue}</span>`:''}${tags}</div>
|
||||
<div class="paper-links">${links.join('')}</div>
|
||||
@@ -146,38 +159,32 @@ function renderPaper(p) {
|
||||
}
|
||||
|
||||
function getPdfLink(p) {
|
||||
// 有 pdf 字段 → 返回外部源 URL(arxiv 直连 / HF 直连)
|
||||
if (p.pdf) return p.pdf;
|
||||
// 只有 arxiv id → arXiv 直连
|
||||
if (p.arxiv) return `https://arxiv.org/pdf/${p.arxiv}.pdf`;
|
||||
return null;
|
||||
}
|
||||
|
||||
function openPdfBtn(btn) { openPdf(btn.dataset.pdf, btn.dataset.title); }
|
||||
|
||||
// ══════════════════ PDF VIEWER ════════════════════════
|
||||
function getLocalUrl(extUrl) {
|
||||
// arXiv
|
||||
// ═══════════════ PDF VIEWER (状态条驱动) ═══════════════
|
||||
function getLocalPdfUrl(extUrl) {
|
||||
const am = extUrl.match(/arxiv\.org\/pdf\/(\d+\.\d+)/);
|
||||
if (am) return `/papers/arxiv/${am[1]}.pdf`;
|
||||
// HuggingFace
|
||||
if (extUrl.includes('huggingface.co')) {
|
||||
const name = decodeURIComponent(extUrl).split('/').pop().replace('.pdf','');
|
||||
return `/papers/hf/${name}.pdf`;
|
||||
}
|
||||
if (extUrl.includes('huggingface.co')) return `/papers/hf/${extUrl.split('/').pop().replace('.pdf','')}.pdf`;
|
||||
return null;
|
||||
}
|
||||
|
||||
function openPdf(url, title) {
|
||||
const decodedUrl = decodeURIComponent(url);
|
||||
const decodedTitle = decodeURIComponent(title);
|
||||
currentPdf = decodedUrl;
|
||||
const hkFallback = getLocalPdfUrl(decodedUrl);
|
||||
|
||||
// 构建 overlay
|
||||
if (!$('#pdfOverlay')) {
|
||||
const div = document.createElement('div'); div.id='pdfOverlay'; div.className='pdf-overlay';
|
||||
div.innerHTML = `<div class="pdf-container" id="pdfContainer">
|
||||
<div class="pdf-toolbar"><span id="pdfTitle">PDF</span><span id="pdfStatus" style="color:var(--orange);font-size:0.8em;margin-left:8px;"></span>
|
||||
<button onclick="window.open($('#pdfFrame').src || currentPdf,'_blank')">🔗 新窗口</button>
|
||||
<div class="pdf-toolbar"><span id="pdfTitle">PDF</span><span id="pdfStatus" style="color:var(--orange);font-size:0.8em;margin-left:8px;display:none"></span>
|
||||
<button onclick="window.open(currentPdf,'_blank')">🔗 新窗口</button>
|
||||
<button class="pdf-close" onclick="closePdf()">×</button></div>
|
||||
<iframe class="pdf-frame" id="pdfFrame" src=""></iframe></div>`;
|
||||
document.body.appendChild(div);
|
||||
@@ -186,49 +193,65 @@ function openPdf(url, title) {
|
||||
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfTitle').textContent = decodedTitle;
|
||||
$('#pdfStatus').textContent = '';
|
||||
$('#pdfStatus').style.display = 'none';
|
||||
const frame = $('#pdfFrame');
|
||||
let loaded = false;
|
||||
let abortCtrl = null;
|
||||
frame.src = 'about:blank';
|
||||
frame.onload = ()=>{ loaded=true; if(pdfTimeout){clearTimeout(pdfTimeout);pdfTimeout=null;} $('#pdfStatus').textContent=''; };
|
||||
|
||||
const hkLocalUrl = getLocalUrl(decodedUrl);
|
||||
frame.onload = () => {
|
||||
loaded = true;
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfStatus').style.display = 'none';
|
||||
};
|
||||
|
||||
const isArxiv = decodedUrl.includes('arxiv.org');
|
||||
const isHF = decodedUrl.includes('huggingface.co');
|
||||
const isRemote = isArxiv || isHF;
|
||||
const sourceOk = isArxiv ? networkStatus['arxiv'] !== false
|
||||
: isHF ? networkStatus['hf'] !== false
|
||||
: false;
|
||||
const remoteOk = (isArxiv && networkStatus['arxiv'] !== false);
|
||||
|
||||
// 弹框先出
|
||||
$('#pdfOverlay').classList.add('open');
|
||||
|
||||
if (isRemote && sourceOk) {
|
||||
// 远程源 iframe 直连
|
||||
// arXiv: iframe 直连(跨域,IDM 只能认了)
|
||||
if (remoteOk) {
|
||||
loaded = false;
|
||||
$('#pdfStatus').textContent = isArxiv ? '🌐 arXiv 加载中...' : '🤗 HuggingFace 加载中...';
|
||||
$('#pdfStatus').textContent = '🌐 arXiv 加载中...';
|
||||
$('#pdfStatus').style.display = '';
|
||||
frame.onload = () => { loaded=true; $('#pdfStatus').style.display='none'; };
|
||||
frame.src = decodedUrl;
|
||||
|
||||
pdfTimeout = setTimeout(() => {
|
||||
pdfTimeout = setTimeout(async () => {
|
||||
if (loaded) return;
|
||||
if (!hkLocalUrl) { $('#pdfStatus').textContent='⚠️ 超时'; return; }
|
||||
$('#pdfStatus').textContent = '⏳ 超时,走 HK 服务器...';
|
||||
frame.src = hkLocalUrl;
|
||||
if (!hkFallback) { $('#pdfStatus').textContent='⚠️ arXiv 超时'; return; }
|
||||
// fallback to HK, use fetch→blob to beat IDM
|
||||
$('#pdfStatus').textContent = '⏳ arXiv 超时,走 HK 服务器...';
|
||||
try {
|
||||
const resp = await fetch(hkFallback, { cache:'default' });
|
||||
const blobUrl = URL.createObjectURL(await resp.blob());
|
||||
$('#pdfStatus').textContent = '';
|
||||
frame.src = blobUrl;
|
||||
} catch(e) { $('#pdfStatus').textContent = '⚠️ HK 加载失败'; }
|
||||
}, 5000);
|
||||
} else {
|
||||
// 直接走 HK
|
||||
$('#pdfStatus').textContent = hkLocalUrl ? '📂 HK 服务器加载中...' : '⏳ 加载中...';
|
||||
frame.src = hkLocalUrl || decodedUrl;
|
||||
// arXiv down / local / HF → use HK with fetch→blob
|
||||
const src = hkFallback || decodedUrl;
|
||||
$('#pdfStatus').textContent = hkFallback ? '📂 HK 服务器加载中...' : '⏳ 加载中...';
|
||||
$('#pdfStatus').style.display = '';
|
||||
try {
|
||||
const resp = await fetch(src, { cache:'default' });
|
||||
const blobUrl = URL.createObjectURL(await resp.blob());
|
||||
$('#pdfStatus').textContent = '';
|
||||
frame.src = blobUrl;
|
||||
} catch(e) { $('#pdfStatus').textContent = '⚠️ 加载失败'; }
|
||||
}
|
||||
|
||||
$('#pdfOverlay').classList.add('open');
|
||||
}
|
||||
|
||||
function closePdf() {
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfOverlay').classList.remove('open');
|
||||
$('#pdfFrame').src = '';
|
||||
currentPdf = null;
|
||||
}
|
||||
|
||||
// ══════════════════ SEARCH ════════════════════════════
|
||||
// ═══════════════ SEARCH ═══════════════════════════════
|
||||
function searchPapers(q) {
|
||||
q = (q||'').toLowerCase().trim();
|
||||
if (q.length<2) { $$('.card').forEach(c=>c.style.outline=''); return; }
|
||||
@@ -241,7 +264,7 @@ function searchPapers(q) {
|
||||
});
|
||||
}
|
||||
|
||||
// ══════════════════ EVENTS ════════════════════════════
|
||||
// ═══════════════ EVENTS ═══════════════════════════════
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
init();
|
||||
|
||||
@@ -29,6 +29,283 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/app.js"></script>
|
||||
<script>
|
||||
/**
|
||||
* LLM 论文图书馆 — 前端 JS
|
||||
* 打开时 ping 所有论文源 → 底部状态条 → 点击论文直用缓存结果
|
||||
*/
|
||||
|
||||
const API = '/api';
|
||||
|
||||
let modules = {};
|
||||
let moduleData = {};
|
||||
let currentPdf = null;
|
||||
let pdfTimeout = null;
|
||||
let networkStatus = {}; // { arxiv: bool, huggingface: bool, hk: bool }
|
||||
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
const $$ = (sel) => document.querySelectorAll(sel);
|
||||
const TAG_CLASS = { '起点':'tag-start','关键节点':'tag-milestone','前沿':'tag-frontier','前瞻':'tag-forward','支线':'tag-branch' };
|
||||
|
||||
// ═══════════════ INIT ═══════════════════════════════════
|
||||
async function init() {
|
||||
// Build network status bar
|
||||
buildStatusBar();
|
||||
// Load modules
|
||||
try {
|
||||
const resp = await fetch(`${API}/modules`);
|
||||
const mods = await resp.json();
|
||||
modules = {};
|
||||
for (const m of mods) modules[m.id] = m;
|
||||
renderCards(mods);
|
||||
attachGlowTracking();
|
||||
} catch (e) { console.error(e); }
|
||||
|
||||
// Ping sources
|
||||
checkSources();
|
||||
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key === 'Escape') {
|
||||
if ($('#pdfOverlay')?.classList.contains('open')) closePdf();
|
||||
else if ($('#overlay').classList.contains('open')) closeModal();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ═══════════════ STATUS BAR ════════════════════════════
|
||||
function buildStatusBar() {
|
||||
const bar = document.createElement('div');
|
||||
bar.id = 'statusBar';
|
||||
bar.className = 'status-bar';
|
||||
bar.innerHTML = `
|
||||
<span class="status-label">连通性检测</span>
|
||||
<span class="status-item" id="status-arxiv"><span class="status-dot"></span> arXiv <span class="status-ms" id="ms-arxiv">—</span></span>
|
||||
<span class="status-item" id="status-hf"><span class="status-dot"></span> HuggingFace <span class="status-ms" id="ms-hf">—</span></span>
|
||||
`;
|
||||
document.body.appendChild(bar);
|
||||
}
|
||||
|
||||
function setStatus(id, ok, ms, aborted) {
|
||||
networkStatus[id.replace('status-','')] = ok;
|
||||
const el = document.getElementById(id);
|
||||
if (!el) return;
|
||||
const dot = el.querySelector('.status-dot');
|
||||
dot.className = 'status-dot ' + (ok ? 'status-ok' : 'status-fail');
|
||||
const msEl = document.getElementById('ms-' + id.replace('status-',''));
|
||||
if (msEl) {
|
||||
if (aborted) msEl.textContent = '超时 (4s)';
|
||||
else if (ok) msEl.textContent = ms + 'ms';
|
||||
else msEl.textContent = '—';
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSource(name, url, statusId) {
|
||||
const start = performance.now();
|
||||
let aborted = false;
|
||||
// Add cache-buster and force no-store to avoid browser caching
|
||||
const probeUrl = url + '?_=' + Date.now();
|
||||
try {
|
||||
const ctrl = new AbortController();
|
||||
setTimeout(() => { aborted = true; ctrl.abort(); }, 4000);
|
||||
await fetch(probeUrl, { mode: 'no-cors', signal: ctrl.signal, cache: 'no-store' });
|
||||
const ms = Math.round(performance.now() - start);
|
||||
setStatus(statusId, true, ms, false);
|
||||
} catch {
|
||||
const ms = Math.round(performance.now() - start);
|
||||
setStatus(statusId, false, ms, aborted);
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSources() {
|
||||
checkSource('arxiv', 'https://arxiv.org/favicon.ico', 'status-arxiv');
|
||||
checkSource('hf', 'https://huggingface.co/favicon.ico', 'status-hf');
|
||||
}
|
||||
|
||||
// ═══════════════ GLOW ══════════════════════════════════
|
||||
function attachGlowTracking() {
|
||||
$$('.card').forEach(card => {
|
||||
card.addEventListener('mousemove', e => {
|
||||
const rect = card.getBoundingClientRect();
|
||||
card.style.setProperty('--mx', ((e.clientX - rect.left) / rect.width * 100) + '%');
|
||||
card.style.setProperty('--my', ((e.clientY - rect.top) / rect.height * 100) + '%');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ═══════════════ CARDS -> MODAL -> PAPERS ═══════════════
|
||||
function renderCards(mods) {
|
||||
$('#moduleGrid').innerHTML = mods.map(m => `
|
||||
<div class="card card-${m.id}" onclick="openModule('${m.id}')">
|
||||
<div class="card-header"><span class="card-icon">${m.icon}</span> ${m.name}</div>
|
||||
<div class="card-desc">${m.desc}</div>
|
||||
<div class="card-badge">${m.area_count} 子领域 · ${m.paper_count} 篇</div>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
async function openModule(modId) {
|
||||
let data = moduleData[modId];
|
||||
if (!data) {
|
||||
try { data = await (await fetch(`${API}/modules/${modId}`)).json(); moduleData[modId] = data; }
|
||||
catch (e) { return; }
|
||||
}
|
||||
const mod = modules[modId];
|
||||
$('#modalTitle').innerHTML = `<span class="card-icon">${mod?.icon||''}</span> ${data.name}`;
|
||||
$('#modalSubtitle').textContent = data.desc || '';
|
||||
const areas = data.areas || [];
|
||||
$('#modalTabs').innerHTML = areas.map((a,i) =>
|
||||
`<button class="tab ${i===0?'active':''}" onclick="switchArea(${i})">${a.name}</button>`).join('');
|
||||
if (areas.length > 0) { $('#modalTabs').dataset.areaIdx = '0'; renderPapers(areas[0]); }
|
||||
$('#overlay').classList.add('open');
|
||||
}
|
||||
|
||||
function switchArea(idx) {
|
||||
const data = Object.values(moduleData).find(d => d.areas && d.areas[idx]);
|
||||
if (!data) return;
|
||||
$$('#modalTabs .tab').forEach((t,i) => t.classList.toggle('active', i===idx));
|
||||
renderPapers(data.areas[idx]);
|
||||
}
|
||||
|
||||
function closeModal() { $('#overlay').classList.remove('open'); }
|
||||
|
||||
function renderPapers(area) {
|
||||
let h = '';
|
||||
function s(l, ps, c) { return ps.length ? `<div class="section-label ${c}">${l}</div>`+ps.map(renderPaper).join('') : ''; }
|
||||
h += s('📌 主线论文', area.mainline||[], 'mainline');
|
||||
h += s('🌿 支线论文', area.branches||[], 'branch');
|
||||
h += s('🔮 前瞻探索', area.forward||[], 'forward');
|
||||
$('#modalContent').innerHTML = h || '<p style="color:var(--text-dim);padding:20px;">暂无论文数据</p>';
|
||||
}
|
||||
|
||||
function renderPaper(p) {
|
||||
const pdfUrl = getPdfLink(p);
|
||||
const id = 'p'+Math.random().toString(36).slice(2,8);
|
||||
const tags = (p.tags||[]).map(t => `<span class="paper-tag ${TAG_CLASS[t]||'tag-branch'}">${t}</span>`).join(' ');
|
||||
const links = [];
|
||||
if (pdfUrl) links.push(`<button class="paper-link" data-pdf="${encodeURIComponent(pdfUrl)}" data-title="${encodeURIComponent(p.title)}" onclick="openPdfBtn(this)">📄 阅读</button>`);
|
||||
else if (p.arxiv) links.push(`<a class="paper-link" href="https://arxiv.org/abs/${p.arxiv}" target="_blank">📋 arXiv</a>`);
|
||||
return `<div class="paper-item"><div class="paper-year">${p.year||'—'}</div><div class="paper-body" id="${id}">
|
||||
<div class="paper-title">${p.title}</div>
|
||||
<div class="paper-meta"><span>${p.authors||''}</span>${p.venue?`<span class="paper-venue">${p.venue}</span>`:''}${tags}</div>
|
||||
<div class="paper-links">${links.join('')}</div>
|
||||
</div></div>`;
|
||||
}
|
||||
|
||||
function getPdfLink(p) {
|
||||
if (p.pdf) return p.pdf;
|
||||
if (p.arxiv) return `https://arxiv.org/pdf/${p.arxiv}.pdf`;
|
||||
return null;
|
||||
}
|
||||
|
||||
function openPdfBtn(btn) { openPdf(btn.dataset.pdf, btn.dataset.title); }
|
||||
|
||||
// ═══════════════ PDF VIEWER (状态条驱动) ═══════════════
|
||||
function getLocalPdfUrl(extUrl) {
|
||||
const am = extUrl.match(/arxiv\.org\/pdf\/(\d+\.\d+)/);
|
||||
if (am) return `/papers/arxiv/${am[1]}.pdf`;
|
||||
if (extUrl.includes('huggingface.co')) return `/papers/hf/${extUrl.split('/').pop().replace('.pdf','')}.pdf`;
|
||||
return null;
|
||||
}
|
||||
|
||||
function openPdf(url, title) {
|
||||
const decodedUrl = decodeURIComponent(url);
|
||||
const decodedTitle = decodeURIComponent(title);
|
||||
currentPdf = decodedUrl;
|
||||
const hkFallback = getLocalPdfUrl(decodedUrl);
|
||||
|
||||
if (!$('#pdfOverlay')) {
|
||||
const div = document.createElement('div'); div.id='pdfOverlay'; div.className='pdf-overlay';
|
||||
div.innerHTML = `<div class="pdf-container" id="pdfContainer">
|
||||
<div class="pdf-toolbar"><span id="pdfTitle">PDF</span><span id="pdfStatus" style="color:var(--orange);font-size:0.8em;margin-left:8px;display:none"></span>
|
||||
<button onclick="window.open(currentPdf,'_blank')">🔗 新窗口</button>
|
||||
<button class="pdf-close" onclick="closePdf()">×</button></div>
|
||||
<iframe class="pdf-frame" id="pdfFrame" src=""></iframe></div>`;
|
||||
document.body.appendChild(div);
|
||||
div.addEventListener('click', e => { if (e.target===div) closePdf(); });
|
||||
}
|
||||
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfTitle').textContent = decodedTitle;
|
||||
$('#pdfStatus').style.display = 'none';
|
||||
const frame = $('#pdfFrame');
|
||||
let loaded = false;
|
||||
let abortCtrl = null;
|
||||
frame.src = 'about:blank';
|
||||
|
||||
frame.onload = () => {
|
||||
loaded = true;
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfStatus').style.display = 'none';
|
||||
};
|
||||
|
||||
const isArxiv = decodedUrl.includes('arxiv.org');
|
||||
const remoteOk = (isArxiv && networkStatus['arxiv'] !== false);
|
||||
|
||||
// arXiv: iframe 直连(跨域,IDM 只能认了)
|
||||
if (remoteOk) {
|
||||
loaded = false;
|
||||
$('#pdfStatus').textContent = '🌐 arXiv 加载中...';
|
||||
$('#pdfStatus').style.display = '';
|
||||
frame.onload = () => { loaded=true; $('#pdfStatus').style.display='none'; };
|
||||
frame.src = decodedUrl;
|
||||
|
||||
pdfTimeout = setTimeout(async () => {
|
||||
if (loaded) return;
|
||||
if (!hkFallback) { $('#pdfStatus').textContent='⚠️ arXiv 超时'; return; }
|
||||
// fallback to HK, use fetch→blob to beat IDM
|
||||
$('#pdfStatus').textContent = '⏳ arXiv 超时,走 HK 服务器...';
|
||||
try {
|
||||
const resp = await fetch(hkFallback, { cache:'default' });
|
||||
const blobUrl = URL.createObjectURL(await resp.blob());
|
||||
$('#pdfStatus').textContent = '';
|
||||
frame.src = blobUrl;
|
||||
} catch(e) { $('#pdfStatus').textContent = '⚠️ HK 加载失败'; }
|
||||
}, 5000);
|
||||
} else {
|
||||
// arXiv down / local / HF → use HK with fetch→blob
|
||||
const src = hkFallback || decodedUrl;
|
||||
$('#pdfStatus').textContent = hkFallback ? '📂 HK 服务器加载中...' : '⏳ 加载中...';
|
||||
$('#pdfStatus').style.display = '';
|
||||
try {
|
||||
const resp = await fetch(src, { cache:'default' });
|
||||
const blobUrl = URL.createObjectURL(await resp.blob());
|
||||
$('#pdfStatus').textContent = '';
|
||||
frame.src = blobUrl;
|
||||
} catch(e) { $('#pdfStatus').textContent = '⚠️ 加载失败'; }
|
||||
}
|
||||
|
||||
$('#pdfOverlay').classList.add('open');
|
||||
}
|
||||
|
||||
function closePdf() {
|
||||
if (pdfTimeout) { clearTimeout(pdfTimeout); pdfTimeout = null; }
|
||||
$('#pdfOverlay').classList.remove('open');
|
||||
$('#pdfFrame').src = '';
|
||||
currentPdf = null;
|
||||
}
|
||||
|
||||
// ═══════════════ SEARCH ═══════════════════════════════
|
||||
function searchPapers(q) {
|
||||
q = (q||'').toLowerCase().trim();
|
||||
if (q.length<2) { $$('.card').forEach(c=>c.style.outline=''); return; }
|
||||
fetch(`${API}/papers?q=${encodeURIComponent(q)}&limit=10`).then(r=>r.json()).then(ps=>{
|
||||
const matched = new Set(ps.map(p=>p.module_id));
|
||||
$$('.card').forEach(c=>{
|
||||
const m=[...c.classList].find(x=>x.startsWith('card-'))?.replace('card-','');
|
||||
c.style.outline=matched.has(m)?'2px solid var(--green)':'';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ═══════════════ EVENTS ═══════════════════════════════
|
||||
if (typeof document !== 'undefined') {
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
init();
|
||||
$('.search-input').addEventListener('input', e => searchPapers(e.target.value));
|
||||
$('#modalClose').addEventListener('click', closeModal);
|
||||
$('#overlay').addEventListener('click', e => { if (e.target===$('#overlay')) closeModal(); });
|
||||
});
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user