/* 简航 SimpleNav —— 后台交互 */ (function () { 'use strict'; var $ = function (s) { return document.querySelector(s); }; /* ---------- 自动获取网站信息 ---------- */ var fetchBtn = $('#btn-fetch'); if (fetchBtn) { fetchBtn.addEventListener('click', function () { var url = ($('#f-url') || {}).value || ''; var hint = $('#fetch-hint'); if (!url) { if (hint) hint.textContent = '请先填写网址'; return; } if (hint) hint.textContent = '正在获取…'; fetchBtn.disabled = true; fetch(fetchBtn.getAttribute('data-url') + '?url=' + encodeURIComponent(url)) .then(function (r) { return r.json(); }) .then(function (d) { fetchBtn.disabled = false; if (!d.ok) { if (hint) hint.textContent = d.msg || '获取失败'; return; } if ($('#f-name')) $('#f-name').value = d.name || ''; if ($('#f-short') && !$('#f-short').value) $('#f-short').value = d.short || d.name || ''; if ($('#f-icon')) $('#f-icon').value = d.icon || ''; if (hint) hint.textContent = (d.icon ? '已获取名称与图标' : '已获取名称(未找到图标)') + (d.url ? ':' + d.url : ''); }) .catch(function () { fetchBtn.disabled = false; if (hint) hint.textContent = '网络错误'; }); }); } var resetBtn = $('#btn-reset'); if (resetBtn) { resetBtn.addEventListener('click', function () { ['f-id', 'f-url', 'f-name', 'f-short', 'f-icon'].forEach(function (id) { var el = $('#' + id); if (el) el.value = id === 'f-id' ? '0' : ''; }); var v = $('#f-visible'); if (v) v.checked = true; var h = $('#fetch-hint'); if (h) h.textContent = ''; }); } /* ---------- 编辑网站:回填表单 ---------- */ document.querySelectorAll('.edit-site').forEach(function (btn) { btn.addEventListener('click', function () { var d = btn.dataset; $('#f-id').value = d.id; $('#f-group').value = d.group; $('#f-url').value = d.url; $('#f-name').value = d.name; $('#f-short').value = d.short; $('#f-icon').value = d.icon; $('#f-visible').checked = d.visible === '1'; window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); /* ---------- 编辑板块 / 引擎 ---------- */ document.querySelectorAll('.edit-group').forEach(function (btn) { btn.addEventListener('click', function () { var d = btn.dataset; $('#g-id').value = d.id; $('#g-name').value = d.name; $('#g-display').value = d.display; $('#g-visible').checked = d.visible === '1'; window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); var gReset = $('#g-reset'); if (gReset) gReset.addEventListener('click', function () { $('#g-id').value = '0'; $('#g-name').value = ''; $('#g-display').value = 'grid'; $('#g-visible').checked = true; }); document.querySelectorAll('.edit-engine').forEach(function (btn) { btn.addEventListener('click', function () { var d = btn.dataset; $('#e-id').value = d.id; $('#e-name').value = d.name; $('#e-slug').value = d.slug; $('#e-search').value = d.search; $('#e-param').value = d.param; $('#e-sug').value = d.sug; $('#e-sugtype').value = d.sugtype; $('#e-color').value = d.color; $('#e-active').checked = d.active === '1'; window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); var eReset = $('#e-reset'); if (eReset) eReset.addEventListener('click', function () { ['e-id', 'e-name', 'e-slug', 'e-search', 'e-sug', 'e-color'].forEach(function (id) { var el = $('#' + id); if (el) el.value = id === 'e-id' ? '0' : ''; }); $('#e-param').value = 'q'; $('#e-active').checked = false; }); /* ---------- 一键“全部更新”(分批循环) ---------- */ var upBtn = $('#btn-update-all'); if (upBtn) { upBtn.addEventListener('click', function () { if (!confirm('将批量刷新所有网站的图标与名称(用户自定义简称不受影响),确定继续?')) return; var base = upBtn.getAttribute('data-url'); var total = 0, updated = 0, failed = 0, offset = 0; upBtn.disabled = true; function step() { fetch(base + '?offset=' + offset + '&limit=6') .then(function (r) { return r.json(); }) .then(function (d) { total = d.total; updated += d.updated; failed += d.failed; offset = d.offset; upBtn.textContent = '⟳ 更新中 ' + Math.min(offset, total) + '/' + total; if (!d.done) { step(); } else { upBtn.disabled = false; upBtn.textContent = '⟳ 全部更新(图标+名称)'; alert('更新完成:成功 ' + updated + ',失败/无变化 ' + failed + ',共 ' + total + ' 个网站。'); location.reload(); } }) .catch(function () { upBtn.disabled = false; upBtn.textContent = '⟳ 全部更新(图标+名称)'; alert('更新中断,请重试。'); }); } step(); }); } })();