下载模块 Init

This commit is contained in:
zhongluofeng
2026-07-20 18:28:34 +08:00
parent e84958e0fc
commit 548b022426
37 changed files with 4795 additions and 65 deletions
@@ -0,0 +1,96 @@
/**
* Thing Extension - Popup 脚本
* 配置 RPC 地址、密钥等参数,存储到 chrome.storage.local
*/
const $ = (id) => document.getElementById(id)
const DEFAULT_CONFIG = {
rpcUrl: 'http://127.0.0.1:6800/jsonrpc',
rpcSecret: '',
interceptDownload: true,
minSize: 0,
excludeDomains: [],
showNotifications: true
}
function setStatus(state, text) {
const dot = $('statusDot')
const txt = $('statusText')
dot.className = 'dot ' + (state === 'ok' ? 'ok' : state === 'fail' ? 'fail' : '')
txt.textContent = text
}
async function loadConfig() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ type: 'getConfig' }, (config) => {
resolve(config || { ...DEFAULT_CONFIG })
})
})
}
function fillForm(config) {
$('rpcUrl').value = config.rpcUrl || DEFAULT_CONFIG.rpcUrl
$('rpcSecret').value = config.rpcSecret || ''
$('interceptDownload').checked = config.interceptDownload !== false
$('showNotifications').checked = config.showNotifications !== false
$('minSize').value = config.minSize || 0
$('excludeDomains').value = (config.excludeDomains || []).join(',')
}
function readForm() {
return {
rpcUrl: $('rpcUrl').value.trim() || DEFAULT_CONFIG.rpcUrl,
rpcSecret: $('rpcSecret').value.trim(),
interceptDownload: $('interceptDownload').checked,
showNotifications: $('showNotifications').checked,
minSize: parseInt($('minSize').value, 10) || 0,
excludeDomains: $('excludeDomains').value
.split(',')
.map(s => s.trim())
.filter(Boolean)
}
}
document.getElementById('configForm').addEventListener('submit', async (e) => {
e.preventDefault()
const config = readForm()
chrome.runtime.sendMessage({ type: 'saveConfig', config }, (res) => {
if (res && res.ok) {
setStatus('', '已保存')
setTimeout(() => window.close(), 500)
} else {
setStatus('fail', '保存失败:' + (res?.error || '未知错误'))
}
})
})
$('testBtn').addEventListener('click', () => {
setStatus('', '测试中...')
// 先保存当前表单值,再测试
const config = readForm()
chrome.runtime.sendMessage({ type: 'saveConfig', config }, () => {
chrome.runtime.sendMessage({ type: 'testConnection' }, (res) => {
if (res && res.ok) {
setStatus('ok', `已连接 · aria2 ${res.version}`)
} else {
setStatus('fail', '连接失败:' + (res?.error || '未知错误'))
}
})
})
})
// 初始化
;(async () => {
const config = await loadConfig()
fillForm(config)
// 自动测试一次连接
setStatus('', '检测中...')
chrome.runtime.sendMessage({ type: 'testConnection' }, (res) => {
if (res && res.ok) {
setStatus('ok', `已连接 · aria2 ${res.version}`)
} else {
setStatus('fail', '未连接')
}
})
})()