|
|
@@ -0,0 +1,895 @@
|
|
|
+// ============================================================================
|
|
|
+// tools/network.js: 网络传输工具页——LocalSend 文件互传、Croc 加密传输、yt-dlp 视频下载
|
|
|
+// Depends on bindings(LocalSendService/CrocService/YTDLPService/SettingsService),
|
|
|
+// Events/Clipboard, core.js(getSettings/escapeHtml/callAction)
|
|
|
+// ============================================================================
|
|
|
+import { Events, Clipboard } from "@wailsio/runtime";
|
|
|
+import { LocalSendService, CrocService, YTDLPService, SettingsService } from "../../bindings/TengTuToolbox/index.js";
|
|
|
+import { getSettings, escapeHtml, callAction } from "../core.js";
|
|
|
+
|
|
|
+// ------------------------------------------------------------------------
|
|
|
+// 工具页:LocalSend 文件互传——组播发现设备,HTTPS 接收文件
|
|
|
+// ------------------------------------------------------------------------
|
|
|
+const lsToggleBtn = document.getElementById('localsend-toggle'); // 启动/停止乒乓按钮
|
|
|
+let lsRunning = false; // 当前 LocalSend 服务是否运行(乒乓按钮状态)
|
|
|
+const lsHintEl = document.getElementById('localsend-hint'); // 操作提示
|
|
|
+const lsStatusEl = document.getElementById('localsend-status'); // 状态徽标
|
|
|
+const lsAliasInput = document.getElementById('localsend-alias'); // 本机名称(设置页)
|
|
|
+const lsAutoRadio = document.getElementById('localsend-auto'); // 自动接收(设置页)
|
|
|
+const lsManualRadio = document.getElementById('localsend-manual'); // 手动确认(设置页)
|
|
|
+const lsRecvDirInput = document.getElementById('localsend-recvdir');// 接收位置(设置页)
|
|
|
+const lsPickDirBtn = document.getElementById('localsend-pickdir'); // 选择目录(设置页)
|
|
|
+const lsSendHintEl = document.getElementById('localsend-send-hint');// 发送状态提示
|
|
|
+const lsDevicesBox = document.getElementById('localsend-devices'); // 设备列表容器
|
|
|
+const lsDeviceList = document.getElementById('localsend-device-list'); // 设备列表 ul
|
|
|
+
|
|
|
+// 确认弹窗元素
|
|
|
+const lsOfferMask = document.getElementById('ls-offer-mask');
|
|
|
+const lsOfferSender = document.getElementById('ls-offer-sender');
|
|
|
+const lsOfferFiles = document.getElementById('ls-offer-files');
|
|
|
+const lsOfferAccept = document.getElementById('ls-offer-accept');
|
|
|
+const lsOfferReject = document.getElementById('ls-offer-reject');
|
|
|
+
|
|
|
+let lsCurrentOffer = null; // 当前待确认的 fileOffer(手动模式)
|
|
|
+
|
|
|
+// 读取当前 LocalSend 配置(浏览器预览时回退默认值)
|
|
|
+async function lsLoadConfig() {
|
|
|
+ if (!LocalSendService?.GetConfig) return null;
|
|
|
+ try {
|
|
|
+ return await LocalSendService.GetConfig();
|
|
|
+ } catch (err) {
|
|
|
+ console.warn('获取 LocalSend 配置失败:', err);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 同步运行状态到 UI(配置字段已移入设置页,此处只更新状态徽标与按钮)
|
|
|
+function lsSyncStatus(cfg) {
|
|
|
+ if (!cfg) return;
|
|
|
+ lsRunning = !!cfg.enabled;
|
|
|
+ lsStatusEl.textContent = lsRunning ? '运行中' : '';
|
|
|
+ lsStatusEl.className = 'tool-status' + (lsRunning ? ' is-on' : '');
|
|
|
+ // 乒乓按钮:运行中显示「停止服务」(红),未运行显示「启动服务」(主色)
|
|
|
+ lsToggleBtn.textContent = lsRunning ? '停止服务' : '启动服务';
|
|
|
+ lsToggleBtn.classList.toggle('btn-danger', lsRunning);
|
|
|
+ lsToggleBtn.classList.toggle('btn-primary', !lsRunning);
|
|
|
+ lsToggleBtn.disabled = false;
|
|
|
+ lsHintEl.textContent = lsRunning ? '服务已启动,等待其他设备发送文件…' : '';
|
|
|
+}
|
|
|
+
|
|
|
+// 渲染设备列表
|
|
|
+function lsRenderDevices(devices) {
|
|
|
+ if (!lsDevicesBox || !lsDeviceList) return;
|
|
|
+ const list = Array.isArray(devices) ? devices.filter(Boolean) : [];
|
|
|
+ lsDevicesBox.hidden = false;
|
|
|
+ if (!list.length) {
|
|
|
+ lsDeviceList.innerHTML = '<li class="localsend-empty">未发现其他设备(确保对方已启动 LocalSend)</li>';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ lsDeviceList.innerHTML = list.map(d => {
|
|
|
+ const online = d.available ? '在线' : '离线';
|
|
|
+ const type = d.deviceType || 'desktop';
|
|
|
+ const proto = d.protocol || 'https';
|
|
|
+ const ip = escapeHtml(d.ip || '');
|
|
|
+ return `<li data-ls-ip="${ip}">
|
|
|
+ <span class="ls-dev-name">${escapeHtml(d.alias || d.ip)}</span>
|
|
|
+ <span class="ls-dev-meta">${ip} · ${type} · ${proto} · ${escapeHtml(d.version || '')}</span>
|
|
|
+ <span class="ls-dev-online">● ${online}</span>
|
|
|
+ <button class="btn-ghost ls-dev-send" data-ls-ip="${ip}" type="button" title="发送文件到该设备">发送</button>
|
|
|
+ </li>`;
|
|
|
+ }).join('');
|
|
|
+}
|
|
|
+
|
|
|
+// 格式化文件大小
|
|
|
+function lsFormatSize(bytes) {
|
|
|
+ bytes = Number(bytes) || 0;
|
|
|
+ if (bytes >= 1024 * 1024 * 1024) return (bytes / 1024 / 1024 / 1024).toFixed(1) + ' GB';
|
|
|
+ if (bytes >= 1024 * 1024) return (bytes / 1024 / 1024).toFixed(1) + ' MB';
|
|
|
+ if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
|
|
+ return bytes + ' B';
|
|
|
+}
|
|
|
+
|
|
|
+// 显示文件接收确认弹窗(手动模式)
|
|
|
+function lsShowOffer(offer) {
|
|
|
+ if (!offer) return;
|
|
|
+ lsCurrentOffer = offer;
|
|
|
+ lsOfferSender.textContent = `来自:${offer.alias || '未知设备'}`;
|
|
|
+ lsOfferFiles.innerHTML = (offer.files || []).map(f =>
|
|
|
+ `<li><span class="ls-file-name" title="${escapeHtml(f.fileName || '')}">${escapeHtml(f.fileName || '')}</span>` +
|
|
|
+ `<span class="ls-file-size">${lsFormatSize(f.size)}</span></li>`
|
|
|
+ ).join('') || '<li class="localsend-empty">(无文件)</li>';
|
|
|
+ lsOfferMask.hidden = false;
|
|
|
+}
|
|
|
+
|
|
|
+function lsHideOffer() {
|
|
|
+ lsOfferMask.hidden = true;
|
|
|
+ lsCurrentOffer = null;
|
|
|
+}
|
|
|
+
|
|
|
+// 乒乓按钮:点击切换启动/停止(配置来自设置页,已随保存写入 settings.json)
|
|
|
+if (lsToggleBtn) lsToggleBtn.addEventListener('click', async () => {
|
|
|
+ if (lsRunning) {
|
|
|
+ // 当前运行中 → 停止
|
|
|
+ lsHintEl.textContent = '停止中…';
|
|
|
+ lsToggleBtn.disabled = true;
|
|
|
+ try {
|
|
|
+ await LocalSendService.Stop();
|
|
|
+ const cfg = await lsLoadConfig();
|
|
|
+ lsSyncStatus(cfg);
|
|
|
+ lsHintEl.textContent = '服务已停止';
|
|
|
+ lsDevicesBox.hidden = true;
|
|
|
+ } catch (err) {
|
|
|
+ lsHintEl.textContent = '停止失败:' + (err?.message || err);
|
|
|
+ lsToggleBtn.disabled = false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 当前未运行 → 启动
|
|
|
+ lsHintEl.textContent = '启动中…';
|
|
|
+ lsToggleBtn.disabled = true;
|
|
|
+ try {
|
|
|
+ await LocalSendService.Start();
|
|
|
+ const cfg = await lsLoadConfig();
|
|
|
+ lsSyncStatus(cfg);
|
|
|
+ lsHintEl.textContent = '服务已启动,等待其他设备发送文件…';
|
|
|
+ lsDevicesBox.hidden = false;
|
|
|
+ await lsRefreshDevices(); // 立即拉取一次设备
|
|
|
+ } catch (err) {
|
|
|
+ lsHintEl.textContent = '启动失败:' + (err?.message || err);
|
|
|
+ lsToggleBtn.disabled = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 选择接收目录(设置页按钮,选后标记未保存并提示)
|
|
|
+if (lsPickDirBtn) lsPickDirBtn.addEventListener('click', async () => {
|
|
|
+ try {
|
|
|
+ const dir = await SettingsService.PickWebDir(); // 复用目录选择对话框
|
|
|
+ if (!dir) return;
|
|
|
+ lsRecvDirInput.value = dir;
|
|
|
+ callAction('markDirty');
|
|
|
+ lsRecvDirInput.dispatchEvent(new Event('input'));
|
|
|
+ } catch (err) {
|
|
|
+ console.warn('选择 LocalSend 接收目录失败:', err);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 点击设备项「发送」按钮(事件委托):先弹文件选择框,选中后直接发送到该设备
|
|
|
+if (lsDeviceList) lsDeviceList.addEventListener('click', async (e) => {
|
|
|
+ const btn = e.target.closest('.ls-dev-send');
|
|
|
+ if (!btn) return;
|
|
|
+ const ip = btn.getAttribute('data-ls-ip');
|
|
|
+ // 每次点击「发送」都重新选择文件(不预选,避免误发)
|
|
|
+ let files;
|
|
|
+ try {
|
|
|
+ files = await SettingsService.PickSendFiles();
|
|
|
+ } catch (err) {
|
|
|
+ lsSendHintEl.textContent = '选择文件失败:' + (err?.message || err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!files || !files.length) {
|
|
|
+ lsSendHintEl.textContent = '未选择文件';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ btn.disabled = true;
|
|
|
+ lsSendHintEl.textContent = `正在发送 ${files.length} 个文件到 ${ip}…`;
|
|
|
+ try {
|
|
|
+ await LocalSendService.SendFiles(files, ip);
|
|
|
+ lsSendHintEl.textContent = `已发送 ${files.length} 个文件到 ${ip} ✓`;
|
|
|
+ setTimeout(() => { lsSendHintEl.textContent = ''; }, 4000);
|
|
|
+ } catch (err) {
|
|
|
+ lsSendHintEl.textContent = '发送失败:' + (err?.message || err);
|
|
|
+ } finally {
|
|
|
+ btn.disabled = false;
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 拉取并渲染设备列表
|
|
|
+async function lsRefreshDevices() {
|
|
|
+ if (!LocalSendService?.GetDevices) return;
|
|
|
+ try {
|
|
|
+ const devices = await LocalSendService.GetDevices();
|
|
|
+ lsRenderDevices(devices);
|
|
|
+ } catch (err) {
|
|
|
+ console.warn('获取 LocalSend 设备失败:', err);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 订阅设备发现事件
|
|
|
+Events.On('localsend:devices', (event) => {
|
|
|
+ lsRenderDevices(event.data);
|
|
|
+});
|
|
|
+
|
|
|
+// 订阅文件接收请求事件(手动模式弹出确认)
|
|
|
+Events.On('localsend:fileOffer', (event) => {
|
|
|
+ lsShowOffer(event.data);
|
|
|
+});
|
|
|
+
|
|
|
+// 订阅错误事件
|
|
|
+Events.On('localsend:error', (event) => {
|
|
|
+ const msg = event.data?.message || String(event.data || '未知错误');
|
|
|
+ lsHintEl.textContent = 'LocalSend 错误:' + msg;
|
|
|
+ lsStatusEl.textContent = '错误';
|
|
|
+ lsStatusEl.className = 'tool-status';
|
|
|
+ lsRunning = false;
|
|
|
+ lsToggleBtn.textContent = '启动服务';
|
|
|
+ lsToggleBtn.classList.remove('btn-danger');
|
|
|
+ lsToggleBtn.classList.add('btn-primary');
|
|
|
+ lsToggleBtn.disabled = false;
|
|
|
+});
|
|
|
+
|
|
|
+// 接受文件
|
|
|
+lsOfferAccept?.addEventListener('click', async () => {
|
|
|
+ if (!lsCurrentOffer) return;
|
|
|
+ try {
|
|
|
+ await LocalSendService.ConfirmTransfer(lsCurrentOffer.sessionId, true);
|
|
|
+ lsHintEl.textContent = '已接受,文件接收中…';
|
|
|
+ } catch (err) {
|
|
|
+ lsHintEl.textContent = '接受失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+ lsHideOffer();
|
|
|
+});
|
|
|
+
|
|
|
+// 拒绝文件
|
|
|
+lsOfferReject?.addEventListener('click', async () => {
|
|
|
+ if (!lsCurrentOffer) return;
|
|
|
+ try {
|
|
|
+ await LocalSendService.ConfirmTransfer(lsCurrentOffer.sessionId, false);
|
|
|
+ lsHintEl.textContent = '已拒绝该文件';
|
|
|
+ } catch (err) {
|
|
|
+ lsHintEl.textContent = '拒绝失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+ lsHideOffer();
|
|
|
+});
|
|
|
+
|
|
|
+// 页面加载时初始化 LocalSend 状态(配置表单由 loadSettings→syncForm 回填)
|
|
|
+(async function initLocalSend() {
|
|
|
+ const cfg = await lsLoadConfig();
|
|
|
+ lsSyncStatus(cfg);
|
|
|
+ if (cfg?.enabled) {
|
|
|
+ lsDevicesBox.hidden = false;
|
|
|
+ await lsRefreshDevices();
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+// ===== 工具页:Croc 传送——自动检测 PATH 中的 croc CLI,点对点加密传输文件 =====
|
|
|
+const crocCard = document.getElementById('croc-card'); // 工具卡片(croc 不存在时保持隐藏)
|
|
|
+const crocStatusEl = document.getElementById('croc-status'); // 状态徽标
|
|
|
+const crocToggleBtn = document.getElementById('croc-toggle'); // 接收开关(启动/停止乒乓按钮)
|
|
|
+const crocSendBtn = document.getElementById('croc-send'); // 发送(发送中变为停止发送)
|
|
|
+const crocHintEl = document.getElementById('croc-hint'); // 统一提示
|
|
|
+const crocLogEl = document.getElementById('croc-log'); // 日志输出
|
|
|
+
|
|
|
+let crocSending = false; // 是否正在发送(发送按钮在「发送/停止发送」间切换)
|
|
|
+let crocReceiving = false; // 是否正在接收(启动/停止乒乓按钮)
|
|
|
+
|
|
|
+// 更新 Croc 状态徽标(busy=运行中,高亮显示)
|
|
|
+function crocSetStatus(text, busy) {
|
|
|
+ if (!crocStatusEl) return;
|
|
|
+ crocStatusEl.textContent = text || '';
|
|
|
+ crocStatusEl.classList.toggle('is-on', !!busy);
|
|
|
+}
|
|
|
+
|
|
|
+// 追加一行日志到输出区域
|
|
|
+function crocAppendLog(kind, line) {
|
|
|
+ if (!crocLogEl) return;
|
|
|
+ crocLogEl.hidden = false;
|
|
|
+ const prefix = kind === 'send' ? '[发送] ' : '[接收] ';
|
|
|
+ crocLogEl.textContent += prefix + line + '\n';
|
|
|
+ crocLogEl.scrollTop = crocLogEl.scrollHeight; // 滚动到最新一行
|
|
|
+}
|
|
|
+
|
|
|
+// 按当前发送/接收状态刷新徽标(都空闲时显示已就绪)
|
|
|
+function crocRefreshStatus() {
|
|
|
+ if (crocSending) crocSetStatus('发送中', true);
|
|
|
+ else if (crocReceiving) crocSetStatus('接收中', true);
|
|
|
+ else crocSetStatus('已就绪', false);
|
|
|
+}
|
|
|
+
|
|
|
+// 发送:空闲时选择文件发送;发送中再点一次则停止发送
|
|
|
+if (crocSendBtn) crocSendBtn.addEventListener('click', async () => {
|
|
|
+ if (crocSending) {
|
|
|
+ try {
|
|
|
+ await CrocService.StopSend();
|
|
|
+ crocHintEl.textContent = '已停止发送';
|
|
|
+ } catch (err) {
|
|
|
+ crocHintEl.textContent = '停止失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ let files = [];
|
|
|
+ try {
|
|
|
+ files = await SettingsService.PickSendFiles();
|
|
|
+ } catch (err) {
|
|
|
+ crocHintEl.textContent = '选择文件失败:' + (err?.message || err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!files || !files.length) return; // 用户取消
|
|
|
+
|
|
|
+ if (crocLogEl) { crocLogEl.textContent = ''; crocLogEl.hidden = false; }
|
|
|
+ crocHintEl.textContent = `正在发送 ${files.length} 个文件…`;
|
|
|
+ crocSending = true;
|
|
|
+ crocSendBtn.textContent = '停止发送';
|
|
|
+ try {
|
|
|
+ await CrocService.StartSend(files);
|
|
|
+ crocRefreshStatus();
|
|
|
+ } catch (err) {
|
|
|
+ crocSending = false;
|
|
|
+ crocSendBtn.textContent = '发送';
|
|
|
+ crocHintEl.textContent = '发送失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 启动/停止乒乓按钮:接收端用设置中的密码作为传输码
|
|
|
+if (crocToggleBtn) crocToggleBtn.addEventListener('click', async () => {
|
|
|
+ if (crocReceiving) {
|
|
|
+ try {
|
|
|
+ await CrocService.StopReceive();
|
|
|
+ crocHintEl.textContent = '已停止接收';
|
|
|
+ } catch (err) {
|
|
|
+ crocHintEl.textContent = '停止失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const pwd = (getSettings()?.croc?.password || '').trim();
|
|
|
+ if (!pwd) {
|
|
|
+ crocHintEl.textContent = '请先在「设置 → 工具」配置 Croc 传送密码,接收时以该密码作为传输码';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (crocLogEl) { crocLogEl.textContent = ''; crocLogEl.hidden = false; }
|
|
|
+ crocHintEl.textContent = '正在接收…(用设置中的密码作为传输码)';
|
|
|
+ try {
|
|
|
+ await CrocService.StartReceive('', (getSettings()?.croc?.dir || '').trim());
|
|
|
+ crocReceiving = true;
|
|
|
+ crocToggleBtn.textContent = '停止';
|
|
|
+ crocRefreshStatus();
|
|
|
+ } catch (err) {
|
|
|
+ crocHintEl.textContent = '接收失败:' + (err?.message || err);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 后端日志事件
|
|
|
+Events.On('croc:log', (e) => {
|
|
|
+ const d = e?.data || {};
|
|
|
+ crocAppendLog(d.kind || 'send', d.line || '');
|
|
|
+});
|
|
|
+Events.On('croc:exit', (e) => {
|
|
|
+ const d = e?.data || {};
|
|
|
+ const ok = d.code === 0;
|
|
|
+ if (d.kind === 'send') {
|
|
|
+ crocSending = false;
|
|
|
+ if (crocSendBtn) crocSendBtn.textContent = '发送';
|
|
|
+ crocHintEl.textContent = ok ? '发送已结束' : `发送已停止(退出码 ${d.code})`;
|
|
|
+ } else {
|
|
|
+ crocReceiving = false;
|
|
|
+ if (crocToggleBtn) crocToggleBtn.textContent = '启动';
|
|
|
+ crocHintEl.textContent = ok ? '接收已完成' : `接收已停止(退出码 ${d.code})`;
|
|
|
+ }
|
|
|
+ crocRefreshStatus();
|
|
|
+});
|
|
|
+
|
|
|
+// 隐藏指定的设置行,并同步刷新各设置分组标题上的「N 项」计数
|
|
|
+function hideSettingRows(selector) {
|
|
|
+ document.querySelectorAll(selector).forEach(row => { row.style.display = 'none'; });
|
|
|
+ document.querySelectorAll('.setting-group').forEach(group => {
|
|
|
+ const visible = Array.from(group.querySelectorAll('.setting-row'))
|
|
|
+ .filter(row => row.style.display !== 'none')
|
|
|
+ .filter(row => {
|
|
|
+ // 行位于已整体隐藏的子组框内(如 croc 未安装)时不计入,避免「N 项」虚高
|
|
|
+ const sub = row.closest('.setting-subgroup');
|
|
|
+ return !sub || sub.style.display !== 'none';
|
|
|
+ }).length;
|
|
|
+ const count = group.querySelector('.setting-group-count');
|
|
|
+ if (count) count.textContent = `${visible} 项`;
|
|
|
+ });
|
|
|
+ // 子组框:若其内部所有行都被隐藏(如 croc 未安装导致整框无内容),则隐藏整个子组框,避免留空壳
|
|
|
+ document.querySelectorAll('.setting-subgroup').forEach(sub => {
|
|
|
+ const anyVisible = Array.from(sub.querySelectorAll('.setting-row')).some(row => row.style.display !== 'none');
|
|
|
+ sub.style.display = anyVisible ? '' : 'none';
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+// 未检测到 croc 时隐藏设置页中的 Croc 配置项(密码 / 接收保存目录)
|
|
|
+function hideCrocSettings() {
|
|
|
+ hideSettingRows('.croc-setting-row');
|
|
|
+}
|
|
|
+
|
|
|
+// 页面加载时检测 croc 是否可用:不可用则隐藏整张卡片与相关设置项
|
|
|
+(async function initCroc() {
|
|
|
+ if (!crocCard) return;
|
|
|
+ try {
|
|
|
+ const info = await CrocService.Available();
|
|
|
+ if (!info?.available) {
|
|
|
+ crocCard.style.display = 'none'; // PATH 中没有 croc:不展示工具
|
|
|
+ hideCrocSettings(); // 设置页中的 Croc 配置一并隐藏
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ crocCard.style.display = ''; // 可用:正常展示(默认折叠)
|
|
|
+ crocCard.querySelector('.tool-card-title')?.setAttribute(
|
|
|
+ 'title',
|
|
|
+ `通过 croc 命令行工具在任意两台电脑间点对点加密传输文件。已检测到:${info.path || 'croc'}${info.version ? '(' + info.version + ')' : ''}。点击「启动」用设置中的密码作为传输码开始接收,再点一次停止;点击「发送」选择文件后发送。密码与接收目录在「设置 → 工具」中配置。`
|
|
|
+ );
|
|
|
+ crocSetStatus('已就绪', false);
|
|
|
+ } catch (err) {
|
|
|
+ crocCard.style.display = 'none';
|
|
|
+ hideCrocSettings();
|
|
|
+ console.warn('检测 croc 失败:', err);
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+
|
|
|
+// ===== 工具页:视频下载——自动检测 PATH 中的 yt-dlp,监视剪贴板中的 B 站链接 =====
|
|
|
+const ytdlpCard = document.getElementById('ytdlp-card'); // 工具卡片(yt-dlp 不存在时保持隐藏)
|
|
|
+const ytdlpStatusEl = document.getElementById('ytdlp-status'); // 状态徽标
|
|
|
+const ytdlpListEl = document.getElementById('ytdlp-list'); // B 站链接列表
|
|
|
+const ytdlpLogEl = document.getElementById('ytdlp-log'); // 下载日志
|
|
|
+const ytdlpSelectAll = document.getElementById('ytdlp-select-all'); // 全选
|
|
|
+const ytdlpSelectAllLabel = document.getElementById('ytdlp-select-all-label'); // 全选文案(随循环态变化)
|
|
|
+const ytdlpBatchBtn = document.getElementById('ytdlp-batch-download'); // 批量下载
|
|
|
+const ytdlpBatchMp3Btn = document.getElementById('ytdlp-batch-mp3'); // 批量转 mp3(ffmpeg 可用时显示)
|
|
|
+const ytdlpClearBtn = document.getElementById('ytdlp-clear'); // 清空列表
|
|
|
+
|
|
|
+let ytdlpAvailable = false; // PATH 中是否存在 yt-dlp
|
|
|
+let ytdlpFFmpeg = false; // PATH 中是否存在 ffmpeg(决定是否显示「下载转 mp3」)
|
|
|
+let ytdlpDownloading = false; // 是否正在下载(同时只允许一个任务)
|
|
|
+let ytdlpActiveBtn = null; // 当前下载行的按钮(用于恢复文字/禁用状态)
|
|
|
+let ytdlpActiveHint = null; // 当前下载行的状态提示
|
|
|
+let ytdlpActiveLabel = ''; // 当前下载按钮的原始文字(下载 / 下载转 mp3)
|
|
|
+let ytdlpBatching = false; // 是否处于批量下载流程
|
|
|
+let ytdlpBatchAudio = false; // 批量流程是否为转 mp3 模式
|
|
|
+let ytdlpBatchQueue = []; // 批量下载待处理链接队列
|
|
|
+let ytdlpBatchTotal = 0; // 批量下载总数
|
|
|
+let ytdlpBatchDone = 0; // 批量下载已完成数
|
|
|
+const ytdlpSeen = new Set(); // 已加入列表的链接,避免剪贴板反复读取导致重复
|
|
|
+const YTDLP_MAX_ITEMS = 100; // 列表最多保留的链接条数
|
|
|
+
|
|
|
+// 「全选」循环态:0=全选所有 1=全选未下载 2=全选已下载 3=取消全选(下次点击回到全选所有)
|
|
|
+const YTDLP_SELECT_MODE_ALL = 0;
|
|
|
+const YTDLP_SELECT_MODE_PENDING = 1;
|
|
|
+const YTDLP_SELECT_MODE_DONE = 2;
|
|
|
+const YTDLP_SELECT_MODE_OFF = 3;
|
|
|
+let ytdlpSelectMode = YTDLP_SELECT_MODE_ALL;
|
|
|
+
|
|
|
+// 从任意文本中提取第一个 B 站链接(支持 bilibili.com 与 b23.tv 短链)
|
|
|
+function extractBilibiliUrl(text) {
|
|
|
+ const m = String(text || '').match(/https?:\/\/(?:[A-Za-z0-9-]+\.)*(?:bilibili\.com|b23\.tv)\/[^\s"'<>]*/i);
|
|
|
+ if (!m) return '';
|
|
|
+ return m[0].replace(/[),,。;;!!??]+$/, ''); // 去掉尾随的中文标点/括号
|
|
|
+}
|
|
|
+
|
|
|
+// 更新视频下载状态徽标
|
|
|
+function ytdlpSetStatus(text, busy) {
|
|
|
+ if (!ytdlpStatusEl) return;
|
|
|
+ ytdlpStatusEl.textContent = text;
|
|
|
+ ytdlpStatusEl.classList.toggle('is-busy', !!busy);
|
|
|
+}
|
|
|
+
|
|
|
+// 取已勾选的链接(按列表顺序)
|
|
|
+function ytdlpSelectedUrls() {
|
|
|
+ if (!ytdlpListEl) return [];
|
|
|
+ return Array.from(ytdlpListEl.querySelectorAll('.ytdlp-item'))
|
|
|
+ .filter(item => item.querySelector('.ytdlp-check')?.checked)
|
|
|
+ .map(item => item.dataset.url)
|
|
|
+ .filter(Boolean);
|
|
|
+}
|
|
|
+
|
|
|
+// 刷新「全选」与「批量下载」按钮状态
|
|
|
+function ytdlpUpdateBatchCount() {
|
|
|
+ if (!ytdlpListEl) return;
|
|
|
+ const total = ytdlpListEl.querySelectorAll('.ytdlp-item').length;
|
|
|
+ const selected = ytdlpSelectedUrls().length;
|
|
|
+ if (ytdlpSelectAll) {
|
|
|
+ // 三态循环下复选框外观由 syncSelectAllMode 按当前态设置,
|
|
|
+ // 这里只处理「列表为空」的禁用态,避免覆盖循环态的显示。
|
|
|
+ ytdlpSelectAll.disabled = total === 0;
|
|
|
+ }
|
|
|
+ if (ytdlpBatchBtn) {
|
|
|
+ ytdlpBatchBtn.disabled = selected === 0 || ytdlpDownloading;
|
|
|
+ ytdlpBatchBtn.textContent = selected > 0 ? `批量下载(${selected})` : '批量下载';
|
|
|
+ }
|
|
|
+ if (ytdlpBatchMp3Btn) {
|
|
|
+ ytdlpBatchMp3Btn.disabled = selected === 0 || ytdlpDownloading || !ytdlpFFmpeg;
|
|
|
+ ytdlpBatchMp3Btn.textContent = selected > 0 ? `批量转 mp3(${selected})` : '批量转 mp3';
|
|
|
+ }
|
|
|
+ if (ytdlpClearBtn) ytdlpClearBtn.disabled = total === 0;
|
|
|
+}
|
|
|
+
|
|
|
+// 按「全选」循环态设置列表行的勾选状态:
|
|
|
+// ALL=勾选所有行;PENDING=只勾未下载行;DONE=只勾已下载行;OFF=全部取消勾选。
|
|
|
+// 返回实际勾选的行数。
|
|
|
+function ytdlpApplySelectMode() {
|
|
|
+ if (!ytdlpListEl) return 0;
|
|
|
+ const items = Array.from(ytdlpListEl.querySelectorAll('.ytdlp-item'));
|
|
|
+ let selected = 0;
|
|
|
+ items.forEach(item => {
|
|
|
+ const done = item.classList.contains('is-done');
|
|
|
+ let on = false;
|
|
|
+ if (ytdlpSelectMode === YTDLP_SELECT_MODE_ALL) on = true;
|
|
|
+ else if (ytdlpSelectMode === YTDLP_SELECT_MODE_PENDING) on = !done;
|
|
|
+ else if (ytdlpSelectMode === YTDLP_SELECT_MODE_DONE) on = done;
|
|
|
+ else on = false;
|
|
|
+ const cb = item.querySelector('.ytdlp-check');
|
|
|
+ if (cb) cb.checked = on;
|
|
|
+ if (on) selected++;
|
|
|
+ });
|
|
|
+ return selected;
|
|
|
+}
|
|
|
+
|
|
|
+// 把当前循环态同步到复选框外观与文案,让用户能看出现在处于哪种「全选」。
|
|
|
+function ytdlpSyncSelectAllUI() {
|
|
|
+ if (!ytdlpListEl) return;
|
|
|
+ const total = ytdlpListEl.querySelectorAll('.ytdlp-item').length;
|
|
|
+ const selected = ytdlpSelectedUrls().length;
|
|
|
+ if (ytdlpSelectAllLabel) {
|
|
|
+ const text = {
|
|
|
+ [YTDLP_SELECT_MODE_ALL]: '全选所有',
|
|
|
+ [YTDLP_SELECT_MODE_PENDING]: '全选未下载',
|
|
|
+ [YTDLP_SELECT_MODE_DONE]: '全选已下载',
|
|
|
+ [YTDLP_SELECT_MODE_OFF]: '全选',
|
|
|
+ }[ytdlpSelectMode] || '全选';
|
|
|
+ ytdlpSelectAllLabel.textContent = total === 0 ? '全选' : text;
|
|
|
+ ytdlpSelectAllLabel.dataset.mode = String(ytdlpSelectMode);
|
|
|
+ }
|
|
|
+ if (ytdlpSelectAll) {
|
|
|
+ // 取消全选态显示未勾选;其余态只要有勾选就显示勾选,
|
|
|
+ // 半选(部分行匹配)用 indeterminate 区分。
|
|
|
+ ytdlpSelectAll.checked = selected > 0;
|
|
|
+ ytdlpSelectAll.indeterminate =
|
|
|
+ ytdlpSelectMode !== YTDLP_SELECT_MODE_ALL && selected > 0 && selected < total;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 是否存在已下载(is-done)的列表行
|
|
|
+function ytdlpHasDone() {
|
|
|
+ if (!ytdlpListEl) return false;
|
|
|
+ return Array.from(ytdlpListEl.querySelectorAll('.ytdlp-item'))
|
|
|
+ .some(item => item.classList.contains('is-done'));
|
|
|
+}
|
|
|
+
|
|
|
+// 「全选」按钮的循环切换:全选所有 → 全选未下载 → 全选已下载 → 取消全选 → 循环。
|
|
|
+// 没有已下载内容时跳过「全选未下载」——此时它与「全选所有」效果相同,留着只是多余一步。
|
|
|
+function ytdlpCycleSelectAll() {
|
|
|
+ ytdlpSelectMode = (ytdlpSelectMode + 1) % 4;
|
|
|
+ if (ytdlpSelectMode === YTDLP_SELECT_MODE_PENDING && !ytdlpHasDone()) {
|
|
|
+ ytdlpSelectMode = (ytdlpSelectMode + 1) % 4;
|
|
|
+ }
|
|
|
+ ytdlpApplySelectMode();
|
|
|
+ ytdlpSyncSelectAllUI();
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+}
|
|
|
+
|
|
|
+// 按链接查找列表行
|
|
|
+function ytdlpItemByUrl(url) {
|
|
|
+ return Array.from(ytdlpListEl?.children || []).find(el => el.dataset.url === url) || null;
|
|
|
+}
|
|
|
+
|
|
|
+// 开始批量下载:把勾选的链接放入队列,逐个串行下载(后端同一时间只允许一个任务)
|
|
|
+// audioOnly 为 true 时批量下载并转 mp3。
|
|
|
+function ytdlpStartBatch(audioOnly) {
|
|
|
+ if (ytdlpDownloading) return; // 当前有任务在跑:等它结束再批量
|
|
|
+ const urls = ytdlpSelectedUrls();
|
|
|
+ if (!urls.length) return;
|
|
|
+ ytdlpBatching = true;
|
|
|
+ ytdlpBatchAudio = !!audioOnly;
|
|
|
+ ytdlpBatchQueue = urls.slice();
|
|
|
+ // 点击后自动取消选中,并把循环态复位到「全选所有」
|
|
|
+ ytdlpListEl?.querySelectorAll('.ytdlp-check').forEach(c => { c.checked = false; });
|
|
|
+ ytdlpSelectMode = YTDLP_SELECT_MODE_ALL;
|
|
|
+ ytdlpSyncSelectAllUI();
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+ ytdlpBatchTotal = urls.length;
|
|
|
+ ytdlpBatchDone = 0;
|
|
|
+ if (ytdlpLogEl) { ytdlpLogEl.textContent = ''; ytdlpLogEl.hidden = false; }
|
|
|
+ ytdlpProcessBatch();
|
|
|
+}
|
|
|
+
|
|
|
+// 处理批量队列中的下一个链接
|
|
|
+function ytdlpProcessBatch() {
|
|
|
+ if (!ytdlpBatching) return;
|
|
|
+ if (!ytdlpBatchQueue.length) {
|
|
|
+ ytdlpBatching = false;
|
|
|
+ ytdlpBatchTotal = 0;
|
|
|
+ ytdlpSetStatus('已就绪', false);
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const url = ytdlpBatchQueue.shift();
|
|
|
+ const item = ytdlpItemByUrl(url);
|
|
|
+ const buttons = Array.from(item?.querySelectorAll('button') || []);
|
|
|
+ // 转 mp3 模式选「下载转 mp3」按钮,否则选第一个「下载」按钮
|
|
|
+ const btn = ytdlpBatchAudio ? (buttons.find(b => b.textContent.includes('mp3')) || buttons[0]) : buttons[0];
|
|
|
+ const hint = item?.querySelector('.tool-hint');
|
|
|
+ if (!item || !btn || !hint) { ytdlpProcessBatch(); return; } // 行已被移除则跳过
|
|
|
+
|
|
|
+ ytdlpDownloading = true;
|
|
|
+ ytdlpBatchDone += 1;
|
|
|
+ ytdlpActiveBtn = btn;
|
|
|
+ ytdlpActiveHint = hint;
|
|
|
+ ytdlpActiveLabel = btn.textContent;
|
|
|
+ btn.textContent = '停止';
|
|
|
+ item.classList.remove('is-done'); // 重新下载时恢复普通样式
|
|
|
+ hint.textContent = ytdlpBatchAudio ? '批量转 mp3 中…' : '批量下载中…';
|
|
|
+ document.querySelectorAll('#ytdlp-list button').forEach(b => { b.disabled = (b !== btn); });
|
|
|
+ const batchLabel = ytdlpBatchAudio ? '批量转 mp3' : '批量下载';
|
|
|
+ ytdlpSetStatus(`${batchLabel} ${ytdlpBatchDone}/${ytdlpBatchTotal}`, true);
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+
|
|
|
+ YTDLPService.StartDownload(url, ytdlpBatchAudio).catch(() => {
|
|
|
+ hint.textContent = '下载失败';
|
|
|
+ ytdlpFinishDownload(false);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+// 向列表追加一条 B 站链接(已存在则忽略),返回是否新增
|
|
|
+function addYTDLPItem(url) {
|
|
|
+ if (!ytdlpListEl || ytdlpSeen.has(url)) return false;
|
|
|
+ ytdlpSeen.add(url);
|
|
|
+
|
|
|
+ const item = document.createElement('div');
|
|
|
+ item.className = 'ytdlp-item';
|
|
|
+ item.dataset.url = url;
|
|
|
+
|
|
|
+ // 多选框:支持勾选后批量下载
|
|
|
+ const check = document.createElement('input');
|
|
|
+ check.type = 'checkbox';
|
|
|
+ check.className = 'ytdlp-check';
|
|
|
+ check.title = '勾选后可批量下载';
|
|
|
+ // 单行勾选变化后同步「全选」复选框的外观与半选态
|
|
|
+ check.addEventListener('change', () => { ytdlpUpdateBatchCount(); ytdlpSyncSelectAllUI(); });
|
|
|
+
|
|
|
+ // 标题 + 链接:标题异步用 yt-dlp 解析,解析失败时回退显示链接
|
|
|
+ const meta = document.createElement('div');
|
|
|
+ meta.className = 'ytdlp-meta';
|
|
|
+
|
|
|
+ const title = document.createElement('a');
|
|
|
+ title.className = 'ytdlp-title';
|
|
|
+ title.href = url;
|
|
|
+ title.textContent = '获取标题中…';
|
|
|
+ title.title = '点击在浏览器打开';
|
|
|
+ title.setAttribute('data-wml-openURL', url); // 点击用默认/设置中的浏览器打开
|
|
|
+
|
|
|
+ const link = document.createElement('span');
|
|
|
+ link.className = 'ytdlp-url';
|
|
|
+ link.textContent = url;
|
|
|
+ link.title = url;
|
|
|
+
|
|
|
+ meta.appendChild(title);
|
|
|
+ meta.appendChild(link);
|
|
|
+
|
|
|
+ const hint = document.createElement('span');
|
|
|
+ hint.className = 'tool-hint';
|
|
|
+
|
|
|
+ const btn = document.createElement('button');
|
|
|
+ btn.className = 'btn-primary';
|
|
|
+ btn.type = 'button';
|
|
|
+ btn.textContent = '下载';
|
|
|
+ btn.addEventListener('click', () => ytdlpToggleDownload(url, btn, false, hint));
|
|
|
+
|
|
|
+ item.appendChild(check);
|
|
|
+ item.appendChild(meta);
|
|
|
+ item.appendChild(hint);
|
|
|
+ item.appendChild(btn);
|
|
|
+
|
|
|
+ // PATH 中存在 ffmpeg 时,追加「下载转 mp3」按钮
|
|
|
+ if (ytdlpFFmpeg) {
|
|
|
+ const mp3Btn = document.createElement('button');
|
|
|
+ mp3Btn.className = 'btn-ghost';
|
|
|
+ mp3Btn.type = 'button';
|
|
|
+ mp3Btn.textContent = '下载转 mp3';
|
|
|
+ mp3Btn.title = '下载音频并转换为 mp3(使用 ffmpeg)';
|
|
|
+ mp3Btn.addEventListener('click', () => ytdlpToggleDownload(url, mp3Btn, true, hint));
|
|
|
+ item.appendChild(mp3Btn);
|
|
|
+ }
|
|
|
+
|
|
|
+ ytdlpListEl.insertBefore(item, ytdlpListEl.firstChild); // 最新链接放最上面
|
|
|
+
|
|
|
+ // 异步解析视频标题(不下载),避免用户分不清多个链接
|
|
|
+ YTDLPService.FetchTitle(url).then(t => {
|
|
|
+ title.textContent = t || url;
|
|
|
+ }).catch(() => {
|
|
|
+ title.textContent = url;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 超出上限时移除最旧的(跳过正在下载的行)
|
|
|
+ while (ytdlpListEl.children.length > YTDLP_MAX_ITEMS) {
|
|
|
+ const last = ytdlpListEl.lastElementChild;
|
|
|
+ if (last === ytdlpActiveBtn?.closest('.ytdlp-item')) break;
|
|
|
+ ytdlpSeen.delete(last.dataset.url || '');
|
|
|
+ last.remove();
|
|
|
+ }
|
|
|
+ // 新链接按当前「全选」循环态决定是否勾选:
|
|
|
+ // 处于「全选所有」或「全选未下载」时新链接(未下载)自动勾选,
|
|
|
+ // 「全选已下载」与「取消全选」态下不勾选。
|
|
|
+ const newCheck = item.querySelector('.ytdlp-check');
|
|
|
+ if (newCheck) {
|
|
|
+ newCheck.checked =
|
|
|
+ ytdlpSelectMode === YTDLP_SELECT_MODE_ALL ||
|
|
|
+ ytdlpSelectMode === YTDLP_SELECT_MODE_PENDING;
|
|
|
+ }
|
|
|
+ ytdlpSyncSelectAllUI(); // 刷新全选框外观与文案
|
|
|
+ ytdlpUpdateBatchCount(); // 刷新批量按钮计数
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// 清空列表:先终止批量队列与进行中的任务,再移除全部条目并重置去重记录
|
|
|
+async function ytdlpClearList() {
|
|
|
+ if (!ytdlpListEl) return;
|
|
|
+ ytdlpBatching = false; // 先停批量:避免清空后队列继续下一条
|
|
|
+ ytdlpBatchAudio = false;
|
|
|
+ ytdlpBatchQueue = [];
|
|
|
+ ytdlpBatchTotal = 0;
|
|
|
+ ytdlpBatchDone = 0;
|
|
|
+ if (ytdlpDownloading) {
|
|
|
+ try {
|
|
|
+ await YTDLPService.StopDownload();
|
|
|
+ } catch {
|
|
|
+ // 任务可能已自行结束,忽略
|
|
|
+ }
|
|
|
+ ytdlpFinishDownload(false); // 复位按钮与状态(批量标志已清,不会续跑队列)
|
|
|
+ }
|
|
|
+ ytdlpSeen.clear(); // 清空后允许重新收录相同链接
|
|
|
+ ytdlpListEl.innerHTML = '';
|
|
|
+ ytdlpSelectMode = YTDLP_SELECT_MODE_ALL; // 清空后「全选」复位到初始态
|
|
|
+ ytdlpSyncSelectAllUI();
|
|
|
+ ytdlpActiveBtn = null;
|
|
|
+ ytdlpActiveHint = null;
|
|
|
+ ytdlpActiveLabel = '';
|
|
|
+ if (ytdlpLogEl) {
|
|
|
+ ytdlpLogEl.textContent = '';
|
|
|
+ ytdlpLogEl.hidden = true;
|
|
|
+ }
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+ ytdlpSetStatus('已清空', false);
|
|
|
+}
|
|
|
+
|
|
|
+// 恢复下载按钮与状态到空闲态
|
|
|
+function ytdlpFinishDownload(ok) {
|
|
|
+ if (!ytdlpDownloading && !ytdlpActiveBtn) return; // 清空后迟到的退出事件:无需处理
|
|
|
+ ytdlpDownloading = false;
|
|
|
+ const item = ytdlpActiveBtn?.closest('.ytdlp-item');
|
|
|
+ if (ytdlpActiveBtn) {
|
|
|
+ // 已完成的按钮改为「重下载」(mp3 按钮对应「重下载 mp3」),否则恢复原文字
|
|
|
+ if (ok) ytdlpActiveBtn.textContent = (ytdlpActiveLabel || '').includes('mp3') ? '重下载 mp3' : '重下载';
|
|
|
+ else ytdlpActiveBtn.textContent = ytdlpActiveLabel || '下载';
|
|
|
+ }
|
|
|
+ if (ytdlpActiveHint) ytdlpActiveHint.textContent = '';
|
|
|
+ if (ok && item) {
|
|
|
+ // 下载完成:标记为已完成并移到列表末尾,与未下载项区分
|
|
|
+ item.classList.add('is-done');
|
|
|
+ if (ytdlpActiveHint) ytdlpActiveHint.textContent = '已下载';
|
|
|
+ if (ytdlpListEl) ytdlpListEl.appendChild(item);
|
|
|
+ }
|
|
|
+ ytdlpActiveBtn = null;
|
|
|
+ ytdlpActiveHint = null;
|
|
|
+ ytdlpActiveLabel = '';
|
|
|
+ document.querySelectorAll('#ytdlp-list button').forEach(b => { b.disabled = false; });
|
|
|
+ ytdlpSetStatus(ok ? '已就绪' : '已停止', false);
|
|
|
+ // 下载状态变化后按当前「全选」循环态重新套用勾选:
|
|
|
+ // 处于「全选未下载」时刚完成的行自动取消勾选,处于「全选已下载」时自动勾上。
|
|
|
+ ytdlpApplySelectMode();
|
|
|
+ ytdlpSyncSelectAllUI();
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+ if (ytdlpBatching) setTimeout(ytdlpProcessBatch, 300); // 批量流程:继续下一条
|
|
|
+}
|
|
|
+
|
|
|
+// 点击某行的「下载 / 下载转 mp3」:空闲时启动任务,进行中再点同一按钮则停止
|
|
|
+async function ytdlpToggleDownload(url, btn, audioOnly, hintEl) {
|
|
|
+ if (ytdlpDownloading) {
|
|
|
+ if (btn !== ytdlpActiveBtn) return; // 其它按钮已禁用
|
|
|
+ try {
|
|
|
+ await YTDLPService.StopDownload();
|
|
|
+ } catch (err) {
|
|
|
+ hintEl.textContent = '停止失败';
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ytdlpDownloading = true;
|
|
|
+ ytdlpActiveBtn = btn;
|
|
|
+ ytdlpActiveHint = hintEl;
|
|
|
+ ytdlpActiveLabel = btn.textContent;
|
|
|
+ btn.textContent = '停止';
|
|
|
+ btn.closest('.ytdlp-item')?.classList.remove('is-done'); // 重新下载时恢复为普通样式
|
|
|
+ hintEl.textContent = audioOnly ? '转 mp3 中…' : '下载中…';
|
|
|
+ document.querySelectorAll('#ytdlp-list button').forEach(b => { b.disabled = (b !== btn); });
|
|
|
+ if (ytdlpLogEl) { ytdlpLogEl.textContent = ''; ytdlpLogEl.hidden = false; }
|
|
|
+ ytdlpSetStatus(audioOnly ? '转 mp3 中' : '下载中', true);
|
|
|
+ ytdlpUpdateBatchCount(); // 单条下载期间禁用批量下载
|
|
|
+
|
|
|
+ try {
|
|
|
+ await YTDLPService.StartDownload(url, audioOnly);
|
|
|
+ } catch (err) {
|
|
|
+ hintEl.textContent = '下载失败';
|
|
|
+ ytdlpFinishDownload(false);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 读取剪贴板文本:优先用 Wails 原生剪贴板,浏览器预览时回退 Web Clipboard API
|
|
|
+async function readClipboardText() {
|
|
|
+ try {
|
|
|
+ if (typeof Clipboard?.Text === 'function') return await Clipboard.Text();
|
|
|
+ } catch (err) {
|
|
|
+ // 忽略,走下面的浏览器回退
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return await navigator.clipboard.readText();
|
|
|
+ } catch (err) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+let ytdlpClipBusy = false; // 防止上一次读取未完成时重入
|
|
|
+let ytdlpLastClip = ''; // 上次处理过的剪贴板文本:内容不变不重复处理,清除列表后不会被立刻回填
|
|
|
+// 轮询剪贴板:发现 B 站链接就加入列表
|
|
|
+async function pollYTDLPClipboard() {
|
|
|
+ if (!ytdlpAvailable || document.hidden || ytdlpClipBusy) return;
|
|
|
+ ytdlpClipBusy = true;
|
|
|
+ try {
|
|
|
+ const text = await readClipboardText();
|
|
|
+ if (text === ytdlpLastClip) return; // 剪贴板未变化
|
|
|
+ ytdlpLastClip = text;
|
|
|
+ const url = extractBilibiliUrl(text);
|
|
|
+ if (url) addYTDLPItem(url);
|
|
|
+ } finally {
|
|
|
+ ytdlpClipBusy = false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 未检测到 yt-dlp 时隐藏设置页中的下载目录配置
|
|
|
+function hideYTDLPSettings() {
|
|
|
+ hideSettingRows('.ytdlp-setting-row');
|
|
|
+}
|
|
|
+
|
|
|
+// 页面加载时检测 yt-dlp 是否可用:可用则展示卡片并开始监视剪贴板
|
|
|
+(async function initYTDLP() {
|
|
|
+ if (!ytdlpCard) return;
|
|
|
+ try {
|
|
|
+ const info = await YTDLPService.Available();
|
|
|
+ if (!info?.available) {
|
|
|
+ ytdlpCard.style.display = 'none'; // PATH 中没有 yt-dlp:不展示工具
|
|
|
+ hideYTDLPSettings(); // 设置页中的下载目录一并隐藏
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ytdlpAvailable = true;
|
|
|
+ ytdlpFFmpeg = !!info.ffmpeg; // 有 ffmpeg 才显示「下载转 mp3」与「批量转 mp3」
|
|
|
+ if (ytdlpFFmpeg && ytdlpBatchMp3Btn) ytdlpBatchMp3Btn.style.display = '';
|
|
|
+ ytdlpUpdateBatchCount();
|
|
|
+ ytdlpCard.style.display = ''; // 可用:正常展示(默认折叠)
|
|
|
+ ytdlpCard.querySelector('.tool-card-title')?.setAttribute(
|
|
|
+ 'title',
|
|
|
+ `自动检测系统 PATH 中的 yt-dlp(${info.path || 'yt-dlp'}${info.version ? ',版本 ' + info.version : ''})。运行时自动读取剪贴板,发现 B 站链接(bilibili.com / b23.tv)就列在下方,点「下载」即可下载到「设置 → 工具」中配置的目录${info.ffmpeg ? ';检测到 ffmpeg,可点「下载转 mp3」直接提取音频' : ''}。`
|
|
|
+ );
|
|
|
+ ytdlpSetStatus('已就绪', false);
|
|
|
+ pollYTDLPClipboard(); // 启动后先读一次剪贴板
|
|
|
+ setInterval(pollYTDLPClipboard, 1500); // 之后每 1.5 秒自动检查
|
|
|
+ } catch (err) {
|
|
|
+ ytdlpCard.style.display = 'none';
|
|
|
+ hideYTDLPSettings();
|
|
|
+ console.warn('检测 yt-dlp 失败:', err);
|
|
|
+ }
|
|
|
+})();
|
|
|
+
|
|
|
+// 「全选」:点击循环切换四种态(全选所有 → 全选未下载 → 全选已下载 → 取消全选)。
|
|
|
+// 用 click 而非 change:复选框的勾选状态由 ytdlpSyncSelectAllUI 统一控制,
|
|
|
+// 用 change 会与程序化设置 checked 产生冲突。
|
|
|
+if (ytdlpSelectAll) {
|
|
|
+ ytdlpSelectAll.addEventListener('click', () => {
|
|
|
+ ytdlpCycleSelectAll();
|
|
|
+ });
|
|
|
+}
|
|
|
+if (ytdlpBatchBtn) ytdlpBatchBtn.addEventListener('click', () => ytdlpStartBatch(false));
|
|
|
+if (ytdlpBatchMp3Btn) ytdlpBatchMp3Btn.addEventListener('click', () => ytdlpStartBatch(true));
|
|
|
+if (ytdlpClearBtn) ytdlpClearBtn.addEventListener('click', ytdlpClearList);
|
|
|
+
|
|
|
+// 下载日志与退出事件
|
|
|
+Events.On('ytdlp:log', (e) => {
|
|
|
+ const line = String(e?.data?.line || '').trim();
|
|
|
+ if (!line || !ytdlpLogEl) return;
|
|
|
+ ytdlpLogEl.textContent += (ytdlpLogEl.textContent ? '\n' : '') + line;
|
|
|
+ ytdlpLogEl.scrollTop = ytdlpLogEl.scrollHeight; // 自动滚到底部
|
|
|
+});
|
|
|
+Events.On('ytdlp:exit', (e) => {
|
|
|
+ ytdlpFinishDownload(e?.data?.code === 0);
|
|
|
+});
|