feat: add protected public web lyrics workflow

This commit is contained in:
Flechazo committed 2026-08-25 23:08:28 +08:00
1 parent e71e0c74dd
commit 54167c1b59
13 files changed
+425 -57

No files matched your search

+24 -3
View File
@@ -2,6 +2,9 @@ const form = document.querySelector('#search-form');
const keyword = document.querySelector('#keyword');
const notice = document.querySelector('#notice');
const results = document.querySelector('#results');
const modeNote = document.querySelector('#mode-note');
const localDownloads = document.querySelector('#local-downloads');
const lyricCache = new Map();
function text(value) { return document.createTextNode(value ?? ''); }
function safeName(value) { return (value || 'unknown').replace(/[\\/:*?"<>|\u0000-\u001f]/g, '_').trim().slice(0, 180) || 'unknown'; }
@@ -15,6 +18,21 @@ async function api(path, payload) {
return data;
}
async function loadInfo() {
try {
const response = await fetch('/api/info');
const info = await response.json();
if (info.mode === 'local') {
modeNote.textContent = '本机模式:查询从这台电脑直接发出,不经过 lrc.flechazo.xin。';
} else {
modeNote.textContent = '网页模式:查询由本站后端转发,但不会写入数据库或日志正文。下载文件在浏览器中生成。';
localDownloads.hidden = false;
}
} catch {
modeNote.textContent = '无法确认运行模式,请刷新页面重试。';
}
}
function download(name, body, type = 'text/plain;charset=utf-8') {
const url = URL.createObjectURL(new Blob([body], {type}));
const anchor = document.createElement('a');
@@ -27,7 +45,8 @@ async function fetchAndSave(song, kind, button) {
button.disabled = true;
notice.className = 'notice'; notice.textContent = `正在读取《${song.name}》…`;
try {
const data = await api('/api/lyrics', {mid: song.mid});
if (!lyricCache.has(song.mid)) lyricCache.set(song.mid, api('/api/lyrics', {mid: song.mid}));
const data = await lyricCache.get(song.mid).catch(error => { lyricCache.delete(song.mid); throw error; });
const base = safeName(`${song.name} - ${song.singer}`);
if (kind === 'lrc') download(`${base}.lrc`, data.lyric);
if (kind === 'txt') download(`${base}.txt`, plainText(data.lyric));
@@ -35,7 +54,7 @@ async function fetchAndSave(song, kind, button) {
if (!data.trans) throw new Error('这首歌没有翻译歌词');
download(`${base}.trans.lrc`, data.trans);
}
notice.textContent = '文件已在浏览器中生成,内容未上传。';
notice.textContent = '文件已在浏览器中生成。';
} catch (error) {
notice.className = 'notice error'; notice.textContent = error.message;
} finally { button.disabled = false; }
@@ -61,10 +80,12 @@ function renderSongs(songs) {
form.addEventListener('submit', async event => {
event.preventDefault(); const submit = form.querySelector('button'); submit.disabled = true;
notice.className = 'notice'; notice.textContent = '正在从这台电脑搜索…'; results.replaceChildren();
notice.className = 'notice'; notice.textContent = '正在搜索…'; results.replaceChildren(); lyricCache.clear();
try {
const data = await api('/api/search', {keyword: keyword.value.trim(), limit: 15});
renderSongs(data.songs || []); notice.textContent = data.songs?.length ? `找到 ${data.songs.length} 条结果。` : '没有找到相关歌曲。';
} catch (error) { notice.className = 'notice error'; notice.textContent = error.message; }
finally { submit.disabled = false; }
});
loadInfo();