v1.0.5: 网址列表支持上传txt批量导入(UTF-8/GBK自动识别/去重/行内注释清理)
This commit is contained in:
@@ -161,6 +161,51 @@ async function delTask(tid) {
|
||||
} catch (e) { toast(e.message, true); }
|
||||
}
|
||||
|
||||
/* ---------------- 网址文件导入 ---------------- */
|
||||
const MAX_IMPORT_SIZE = 2 * 1024 * 1024; // 2MB 上限
|
||||
|
||||
async function readFileSmart(file) {
|
||||
/* 自动识别 UTF-8 / GBK 编码 */
|
||||
const buf = await file.arrayBuffer();
|
||||
let text = new TextDecoder("utf-8").decode(buf);
|
||||
if (text.includes("\uFFFD")) {
|
||||
try { text = new TextDecoder("gbk").decode(buf); } catch (e) { /* 保留 utf-8 结果 */ }
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
function importUrlsText(text) {
|
||||
const ta = $("taskForm").elements["urls"];
|
||||
const clean = (s) => s.split(" #")[0].trim(); // 去掉行内注释 (URL 不含空格, 安全)
|
||||
const existing = new Set(ta.value.split(/\r?\n/).map(clean).filter(Boolean));
|
||||
const fresh = text.split(/\r?\n/).map(clean).filter(Boolean);
|
||||
let added = 0;
|
||||
for (const line of fresh) {
|
||||
if (!existing.has(line)) { existing.add(line); added++; }
|
||||
}
|
||||
ta.value = Array.from(existing).join("\n");
|
||||
return { total: fresh.length, added };
|
||||
}
|
||||
|
||||
$("btnImportUrls").onclick = () => $("urlFileInput").click();
|
||||
$("urlFileInput").onchange = async (e) => {
|
||||
const file = e.target.files && e.target.files[0];
|
||||
e.target.value = ""; // 允许重复选择同一文件
|
||||
if (!file) return;
|
||||
if (file.size > MAX_IMPORT_SIZE) {
|
||||
toast("文件过大(上限 2MB),请拆分后导入", true);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const text = await readFileSmart(file);
|
||||
const r = importUrlsText(text);
|
||||
formDirty = true;
|
||||
toast(`已导入 ${file.name}:共 ${r.total} 行,新增 ${r.added} 条网址`);
|
||||
} catch (err) {
|
||||
toast("文件读取失败: " + err.message, true);
|
||||
}
|
||||
};
|
||||
|
||||
/* ---------------- 搜索 ---------------- */
|
||||
async function doSearch() {
|
||||
const q = $("searchInput").value.trim();
|
||||
|
||||
Reference in New Issue
Block a user