// ==UserScript== // @name 网盘极速下载助手(脚本猫版) // @namespace netdisk-speedup-scriptcat // @version 2.0.1 // @description 在任意网页粘贴百度网盘分享链接/文案即可计算流量、兑换卡密、选择文件提交高速下载。下载在服务端执行。 // @author you // @match *://*/* // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_openInTab // @run-at document-idle // ==/UserScript== // changelog // 2.0.1 新增「🤖 问答」自助客服(注册/充值/速度/保存等问题即问即答);失败提示附带智能解决建议。 // 1.6.0 改为全网页可用:任意网页都能显示面板,直接粘贴整段分享文案(链接+提取码)即可下载,不必打开百度分享页。 // 1.5.1 面板新增「使用说明」入口。 // 1.5.0 增加脚本版本号与服务端强制升级校验(旧版不再可用);下载面板补充速度说明与实时总速度。 // 1.4.0 登录改为用户名+密码(/api/login-password),卡密只用于登录后充值。 // 1.3.3 下载面板补充速度说明(经服务器中转 1–3MB/s);卡密文案改为按面值发放。 // 1.3.2 卡密输入容错:自动修正全角字符/空格/零宽字符,缺字符时提示实际位数。 // 1.3.1 面板补充卡密规则提示:可共享、每卡每日 20GB、00:00 重置。 // 1.3.0 提取码输入框支持直接粘贴整段百度分享文案,由服务端自动识别链接与提取码。 // 1.2.0 登录改为卡密激活(/api/login-with-card),界面只保留卡密入口;一张卡密仅激活一次。 // 1.1.0 面板新增"剩余额度"与"退出";额度/共享池/心跳 5 秒同步,网页端兑换后自动生效; // 共享池文案去掉账号数量等运营信息,只显示剩余/总额。 // 1.0.1 修复跨域与登录态读写问题。 // 1.0.0 初始公开版。 (function () { 'use strict'; // ===== 配置:服务端地址由安装地址推导;展示链接由服务端下发 ===== // 源码不写死任何业务域名/访问码/本地地址,避免公开脚本泄漏地址与凭证。 const INJECTED_API_BASE = "https://app.cxbank.icu"; /* injected */ function detectApiBase() { const saved = String(GM_getValue('speedup_api_base', '') || '').trim(); if (saved) return saved.replace(/\/$/, ''); if (INJECTED_API_BASE) return INJECTED_API_BASE; try { const info = (typeof GM_info !== 'undefined' && GM_info && GM_info.script) || {}; const src = info.downloadURL || info.updateURL || ''; if (src) return new URL(src).origin; } catch (e) {} return ''; // 未设置且无法推导时,提示用户在脚本菜单里填写 } const API_BASE = detectApiBase(); let CLIENT_CFG = { guide_url: '', shop_url: '', contact: '有问题加 QQ 群,找群主解决哈' }; const SCRIPT_VERSION = '2.0.1'; let upgradeNotified = false; // ===== 工具 ===== const TOKEN_KEY = 'speedup_token'; const getToken = () => GM_getValue(TOKEN_KEY, ''); const setToken = (t) => GM_setValue(TOKEN_KEY, t); const esc = (s) => String(s == null ? '' : s).replace(/[<>]/g, ''); const fmt = (n) => { n = Number(n) || 0; if (n > 1073741824) return (n / 1073741824).toFixed(2) + ' GB'; if (n > 1048576) return (n / 1048576).toFixed(1) + ' MB'; if (n > 1024) return (n / 1024).toFixed(0) + ' KB'; return n + ' B'; }; function api(path, method, body, cb) { if (!API_BASE) { cb({ ok: false, err: '未配置服务端地址,请在脚本菜单「设置服务端地址」里填写' }); return; } const headers = { 'Content-Type': 'application/json', 'X-Client': 'scriptcat', 'X-Script-Version': SCRIPT_VERSION }; const t = getToken(); if (t) headers['Authorization'] = 'Bearer ' + t; GM_xmlhttpRequest({ method: method || 'GET', url: API_BASE + path, headers: headers, data: body ? JSON.stringify(body) : undefined, timeout: 180000, onload: (r) => { let j; try { j = JSON.parse(r.responseText); } catch (e) { j = { ok: false, err: '服务端返回非 JSON(HTTP ' + r.status + ')' }; } if (r.status === 426 || j.upgrade) { const msg = '脚本版本过旧,请安装 ' + SCRIPT_VERSION + ' 新版:' + API_BASE + '/install.user.js'; if (!upgradeNotified) { upgradeNotified = true; GM_openInTab(API_BASE + '/install.user.js', true); alert(msg); } cb({ ok: false, err: msg, upgrade: true }); return; } cb(j); }, onerror: () => cb({ ok: false, err: '无法连接服务端,请检查 API_BASE 配置' }), ontimeout: () => cb({ ok: false, err: '请求超时' }), }); } // 全网页模式:链接来自用户粘贴的整段文案,不再依赖当前页面 URL。 // 在百度分享页时,renderAuth 会把当前链接和识别到的提取码预填进输入框。 function shareText() { const el = document.getElementById('sp_share'); return (el ? el.value : '').trim(); } function normCode(v) { return String(v || '').normalize('NFKC') .replace(/[\s\u200b\u200c\u200d\ufeff]/g, '').toUpperCase(); } function cleanPwd(v) { v = String(v || '').trim().split('#')[0].split('&')[0]; const m = v.match(/([A-Za-z0-9]{4})/); return m ? m[1] : ''; } function sharePwd() { let m = location.href.match(/[?&]pwd=([^&]+)/); if (m) { const p = cleanPwd(decodeURIComponent(m[1])); if (p) return p; } m = location.hash.match(/pwd=([^&]+)/); if (m) { const p = cleanPwd(decodeURIComponent(m[1])); if (p) return p; } const inputs = Array.from(document.querySelectorAll('input')); for (const el of inputs) { const ph = (el.placeholder || '') + ' ' + (el.name || '') + ' ' + (el.id || ''); if (/提取码|access|pwd|password/i.test(ph) && el.value) { const p = cleanPwd(el.value); if (p) return p; } } for (const el of inputs) { const v = (el.value || '').trim(); if (/^[A-Za-z0-9]{4}$/.test(v)) return v; } m = document.body.innerText.match(/提取码[::\s]*([A-Za-z0-9]{4})/); return m ? m[1] : ''; } // ===== UI ===== function createPanel() { const old = document.getElementById('speedup_panel'); if (old) old.remove(); const box = document.createElement('div'); box.id = 'speedup_panel'; box.innerHTML = `
⚡ 网盘极速下载 未登录
没有账号?点右上角「📖 说明」按指引注册。登录后在这里粘贴卡密充值,一个账号可多人共用。
`; document.body.appendChild(box); box.querySelector('#sp_login_btn').addEventListener('click', doLogin); box.querySelector('#sp_est').addEventListener('click', doEstimate); box.querySelector('#sp_dl').addEventListener('click', doDownload); box.querySelector('#sp_redeem').addEventListener('click', doRedeem); box.querySelector('#sp_logout').addEventListener('click', doLogout); box.querySelector('#sp_guide').addEventListener('click', () => { GM_openInTab(CLIENT_CFG.guide_url || (API_BASE + '/'), { active: true }); }); box.querySelector('#sp_ai').addEventListener('click', () => { const b = document.getElementById('sp_ai_box'); if (b) { b.style.display = b.style.display === 'none' ? 'block' : 'none'; } const q = document.getElementById('sp_ai_q'); if (q && b && b.style.display === 'block') q.focus(); }); box.querySelector('#sp_ai_send').addEventListener('click', askAI); const aiq = box.querySelector('#sp_ai_q'); if (aiq) aiq.addEventListener('keydown', (e) => { if (e.key === 'Enter') askAI(); }); box.querySelector('#sp_redeem_jump').addEventListener('click', () => { const c=document.getElementById('sp_card'); if(c){c.scrollIntoView({behavior:'smooth',block:'center'});c.focus();} }); box.querySelector('#sp_buy').addEventListener('click', () => { if (CLIENT_CFG.shop_url) GM_openInTab(CLIENT_CFG.shop_url, { active: true }); else msg('购买链接未配置,请在说明页查看', true); }); box.querySelector('#sp_refresh').addEventListener('click', doRefresh); box.querySelector('#sp_files_btn').addEventListener('click', loadFiles); box.querySelector('#sp_mode').addEventListener('click', toggleMode); applyMode(); } // 简单/高级模式 let advanced = GM_getValue('speedup_advanced', false); function applyMode() { const a = document.getElementById('sp_advanced'); const b = document.getElementById('sp_mode'); if (a) a.style.display = advanced ? 'block' : 'none'; if (b) b.textContent = advanced ? '简单 ▴' : '高级 ▾'; } function toggleMode() { advanced = !advanced; GM_setValue('speedup_advanced', advanced); applyMode(); if (advanced) { doRefresh(); loadFiles(); } } // 失败提示自动附一条可执行的解决建议 function smartHint(text) { const s = String(text || ''); if (/额度|不足|quota/i.test(s)) return '👉 先充值或换账号:点「购买额度」。'; if (/版本|426|过旧/.test(s)) return '👉 安装新版脚本:' + API_BASE + '/install.user.js'; if (/硬盘|断开|未挂载|507/.test(s)) return '👉 硬盘插回后任务会自动恢复,稍后再试。'; if (/过期|提取码|链接为空|解析失败/.test(s)) return '👉 重新粘贴整段分享文案(链接+提取码)再解析。'; if (/网络|超时|连接|失败|403|429/.test(s)) return '👉 网络类失败系统会自动退避重试 2 次,稍等即可。'; if (/群|客服/.test(s)) return '👉 ' + (CLIENT_CFG.contact || '请联系客服'); return ''; } function msg(text, err) { const m = document.getElementById('sp_msg'); if (!m) return; m.style.display = 'block'; m.style.color = err ? '#ff9b9b' : '#7ff0a8'; m.textContent = String(text || '') + (err ? ' ' + smartHint(text) : ''); clearTimeout(msg._t); msg._t = setTimeout(() => { m.style.display = 'none'; }, err ? 8000 : 5000); } // 面板内自助问答:调公开 /api/faq,不用进群等回复 function askAI() { const q = document.getElementById('sp_ai_q'); const a = document.getElementById('sp_ai_answer'); const question = (q ? q.value : '').trim(); if (!question || !a) return; a.textContent = '查询中…'; api('/api/faq', 'POST', { question: question }, (r) => { a.textContent = r.ok ? (r.answer || '暂无答案') : ('问答服务暂时不可用:' + (r.err || '未知错误')); }); } function renderAuth() { const logged = !!getToken(); document.getElementById('sp_login').style.display = logged ? 'none' : 'block'; document.getElementById('sp_main').style.display = logged ? 'block' : 'none'; document.getElementById('sp_logout').style.display = logged ? 'inline-block' : 'none'; document.getElementById('sp_status').textContent = logged ? '已登录' : '未登录'; if (logged) { // 在百度分享页时预填当前链接+提取码;其他网页留空由用户粘贴文案 const shareEl = document.getElementById('sp_share'); if (shareEl && !shareEl.value && /pan\.baidu\.com\/s\//.test(location.href)) { const p = sharePwd(); shareEl.value = location.href.split('#')[0] + (p ? ' 提取码: ' + p : ''); } loadMe(); loadFiles(); loadPool(); } } function loadMe() { if (!getToken()) return; api('/api/me', 'GET', null, (r) => { if (!r.ok) { // token 失效:清掉,避免和网页端串号 if (/未登录|令牌|无效|401/.test(r.err || '')) { setToken(''); renderAuth(); } return; } const u = r.user || {}; const v = document.getElementById('sp_quota_val'); if (v) v.textContent = fmt(u.remaining); const q = document.getElementById('sp_quota'); if (q) q.title = '总额 ' + fmt(u.quota) + ' · 已用 ' + fmt(u.used) + (u.is_trial ? ' · 试用额度' : ''); const st = document.getElementById('sp_status'); if (st) st.textContent = (u.plan_name || '已登录') + ' · 剩 ' + fmt(u.remaining); }); } function doLogout() { setToken(''); PICKED = []; renderAuth(); msg('已退出,可换账号登录'); } function heartbeat() { if (!getToken()) return; api('/api/heartbeat', 'POST', {}, () => {}); } function loadPool() { api('/api/service-status', 'GET', null, (r) => { const e = document.getElementById('sp_pool'); if (!e || !r.ok) return; const p = r.pool || {}; e.innerHTML = `📊 今日共享池剩余 ${p.remaining_gb}GB / ${p.total_gb}GB${p.busy ? ' · 额度紧张,请错峰' : ''}`; }); } function doLogin() { const username = (document.getElementById('sp_user').value || '').trim(); const password = document.getElementById('sp_pass').value || ''; if (!username || !password) return msg('请输入用户名和密码', true); api('/api/login-password', 'POST', { username, password }, (r) => { if (!r.ok) return msg(r.err || r.error || '登录失败', true); setToken(r.token); document.getElementById('sp_pass').value = ''; msg('登录成功'); renderAuth(); }); } function doEstimate() { const text = shareText(); if (!text) return msg('请先粘贴百度网盘分享链接或整段文案', true); api('/api/estimate', 'POST', { url: text, pwd: '' }, (r) => { if (!r.ok) return msg(r.err || '估算失败', true); document.getElementById('sp_est_result').innerHTML = `
${esc(r.hint)}
请先购买对应激活卡并兑换,再提交下载。
`; msg('估算完成'); }); } function doRedeem() { const code = normCode(document.getElementById('sp_card').value); api('/api/redeem', 'POST', { code }, (r) => { if (!r.ok) return msg(r.error || r.msg || '兑换失败', true); document.getElementById('sp_card').value = ''; msg('兑换成功:' + (r.msg || '额度已到账')); loadMe(); loadPool(); }); } let PICKED = []; function doDownload() { const text = shareText(); if (!text) return msg('请先粘贴百度网盘分享链接或整段文案', true); api('/api/parse', 'POST', { url: text, pwd: '' }, (r) => { if (!r.ok) return msg(r.err || '解析失败', true); const files = (r.files || []).filter(f => !f.is_dir); if (!files.length) return msg('没有可下载文件', true); PICKED = files; const box = document.getElementById('sp_pick'); const list = document.getElementById('sp_pick_list'); box.style.display = 'block'; const minIdx = files.reduce((a, f, i) => f.size < files[a].size ? i : a, 0); list.innerHTML = files.map((f, i) => `
${esc(f.name)} ${fmt(f.size)}
`).join('') + ``; list.querySelector('#sp_confirm').addEventListener('click', submitPicked); msg('已列出文件,默认勾选最小的。勾选后点「下载选中文件」;文件仅保留 12 小时,请及时保存。'); }); } function submitPicked() { const checks = Array.from(document.querySelectorAll('.sp_fchk')); const sel = checks.map((c, i) => c.checked ? PICKED[i] : null).filter(Boolean); if (!sel.length) return msg('请至少勾选一个文件', true); api('/api/download', 'POST', { files: sel, threads: 8 }, (d) => { if (!d.ok) return msg(d.err || '提交失败', true); const n = d.started.filter(x => x.ok).length; msg(advanced ? ('已提交 ' + n + ' 个任务') : ('已提交 ' + n + ' 个任务,点右上「高级」查看进度和取文件')); document.getElementById('sp_tasks').style.display = 'block'; doRefresh(); }); } function doRefresh() { api('/api/tasks', 'GET', null, (r) => { if (!r.ok) return; const box = document.getElementById('sp_tasks'); if (!box) return; const tasks = r.tasks || []; const running = tasks.filter(t => t.status === 'running'); const spd = running.reduce((a, t) => a + (Number(t.speed) || 0), 0); const sd = document.getElementById('sp_speed'); if (sd) sd.textContent = running.length ? `实时总速度 ${fmt(spd)}/s(${running.length} 个任务下载中)` : '当前无下载任务'; box.innerHTML = '任务进度' + tasks.map(t => { let btns = ''; if (['created', 'queued', 'running', 'paused'].includes(t.status)) btns += ``; if (t.status === 'done' || t.percent >= 100) btns += ``; return `
${esc(t.name)}${t.status}
${t.percent}% · ${fmt(t.done)}/${fmt(t.total)}${t.status === 'running' && t.speed > 0 ? ' · ' + fmt(t.speed) + '/s' : ''} ${btns}
`; }).join(''); box.querySelectorAll('.sp_cancel').forEach(b => b.addEventListener('click', () => cancelTask(b.dataset.id))); box.querySelectorAll('.sp_link').forEach(b => b.addEventListener('click', () => quickDownload(b.dataset.name))); }); } function cancelTask(id) { api('/api/task/' + id + '/cancel', 'POST', null, (r) => { msg(r.ok ? '任务已取消' : (r.err || '取消失败'), !r.ok); doRefresh(); }); } function loadFiles() { api('/api/files', 'GET', null, (r) => { const box = document.getElementById('sp_files'); if (!box || !r.ok) return; const list = r.files || []; box.innerHTML = list.length ? list.map((f, i) => `
📄 ${esc(f.name)} ${fmt(f.size)}
`).join('') : '
还没有已完成的文件
'; box.querySelectorAll('.sp_saveas').forEach(b => b.addEventListener('click', () => saveAsFile(list[+b.dataset.i].name))); box.querySelectorAll('.sp_quick').forEach(b => b.addEventListener('click', () => quickDownload(list[+b.dataset.i].name))); }); } function quickDownload(name) { api('/api/file-link', 'POST', { name }, (r) => { if (!r.ok) return msg(r.err || '获取链接失败', true); window.open(API_BASE + r.url, '_blank'); msg('已打开下载,默认保存到浏览器下载目录'); loadFiles(); }); } function saveAsFile(name) { if (!window.showSaveFilePicker) { msg('当前浏览器不支持选择保存位置,请用「快速下载」', true); return; } api('/api/file-link', 'POST', { name }, async (r) => { if (!r.ok) return msg(r.err || '获取链接失败', true); try { const handle = await window.showSaveFilePicker({ suggestedName: name }); const writable = await handle.createWritable(); const resp = await fetch(API_BASE + r.url); if (!resp.ok) throw new Error('HTTP ' + resp.status); const reader = resp.body.getReader(); let received = 0; const total = Number(resp.headers.get('Content-Length') || 0); while (true) { const { done, value } = await reader.read(); if (done) break; await writable.write(value); received += value.length; if (total) msg('已保存 ' + fmt(received) + ' / ' + fmt(total)); } await writable.close(); msg('保存完成:' + name); loadFiles(); } catch (e) { if (e && e.name !== 'AbortError') msg('保存失败:' + (e.message || e), true); } }); } GM_registerMenuCommand('设置服务端地址', () => { const v = prompt('服务端 API 地址(向管理员索取,不要填来路不明的地址)', API_BASE); if (v) { GM_setValue('speedup_api_base', v.replace(/\/$/, '')); location.reload(); } }); // 展示类配置(说明页/购买页/客服)从服务端下发,不写死在公开脚本里 function loadClientConfig() { if (!API_BASE) return; api('/api/client-config', 'GET', null, (r) => { if (r && r.ok && r.guide_url !== undefined) { CLIENT_CFG = { guide_url: r.guide_url || '', shop_url: r.shop_url || '', contact: r.contact || CLIENT_CFG.contact }; } }); } // 启动方式与第一版一致:createPanel + renderAuth + 轮询 loadClientConfig(); createPanel(); renderAuth(); setInterval(() => { if (getToken()) { doRefresh(); loadPool(); loadMe(); heartbeat(); } }, 5000); })();