Scaffold WebExtension: manifest, content collector, background aria2 RPC, popup UI, filters, and scripts
This commit is contained in:
parent
b81ca429d4
commit
8eeb1d3b20
10 changed files with 427 additions and 0 deletions
117
src/popup/index.js
Normal file
117
src/popup/index.js
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import { applyFilters } from "../lib/filters.js";
|
||||
|
||||
const els = {
|
||||
fType: document.getElementById('f-type'),
|
||||
fTypeRe: document.getElementById('f-type-re'),
|
||||
fName: document.getElementById('f-name'),
|
||||
fNameRe: document.getElementById('f-name-re'),
|
||||
fSizeMin: document.getElementById('f-size-min'),
|
||||
fSizeMax: document.getElementById('f-size-max'),
|
||||
fDateFrom: document.getElementById('f-date-from'),
|
||||
fDateTo: document.getElementById('f-date-to'),
|
||||
fCase: document.getElementById('f-case'),
|
||||
btnRefresh: document.getElementById('btn-refresh'),
|
||||
btnCopy: document.getElementById('btn-copy'),
|
||||
btnSend: document.getElementById('btn-send'),
|
||||
rpcEndpoint: document.getElementById('rpc-endpoint'),
|
||||
rpcSecret: document.getElementById('rpc-secret'),
|
||||
count: document.getElementById('count'),
|
||||
status: document.getElementById('status'),
|
||||
};
|
||||
|
||||
let allItems = [];
|
||||
|
||||
function getFilters() {
|
||||
return {
|
||||
type: els.fType.value,
|
||||
name: els.fName.value,
|
||||
typeRegex: els.fTypeRe.checked,
|
||||
nameRegex: els.fNameRe.checked,
|
||||
sizeMin: els.fSizeMin.value,
|
||||
sizeMax: els.fSizeMax.value,
|
||||
dateFrom: els.fDateFrom.value,
|
||||
dateTo: els.fDateTo.value,
|
||||
caseSensitive: els.fCase.checked,
|
||||
};
|
||||
}
|
||||
|
||||
async function getActiveTabId() {
|
||||
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||
return tabs[0]?.id;
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
els.status.textContent = '';
|
||||
try {
|
||||
const tabId = await getActiveTabId();
|
||||
if (!tabId) throw new Error('No active tab');
|
||||
const items = await browser.tabs.sendMessage(tabId, { type: 'collectLinks' });
|
||||
allItems = items || [];
|
||||
updateCount();
|
||||
} catch (e) {
|
||||
els.status.textContent = String(e?.message || e);
|
||||
}
|
||||
}
|
||||
|
||||
function filtered() {
|
||||
return applyFilters(allItems, getFilters());
|
||||
}
|
||||
|
||||
function updateCount() {
|
||||
els.count.textContent = String(filtered().length);
|
||||
}
|
||||
|
||||
async function copyLinks() {
|
||||
const urls = filtered().map(i => i.url).join('\n');
|
||||
await navigator.clipboard.writeText(urls);
|
||||
els.status.textContent = `Copied ${filtered().length} link(s) to clipboard.`;
|
||||
}
|
||||
|
||||
async function sendToAria2() {
|
||||
const endpoint = els.rpcEndpoint.value.trim();
|
||||
const secret = els.rpcSecret.value.trim();
|
||||
const uris = filtered().map(i => i.url);
|
||||
if (!endpoint) { els.status.textContent = 'Please set aria2 endpoint.'; return; }
|
||||
if (uris.length === 0) { els.status.textContent = 'No links to send.'; return; }
|
||||
|
||||
// Persist settings
|
||||
await browser.storage.local.set({ aria2: { endpoint, secret } });
|
||||
els.status.textContent = 'Sending to aria2…';
|
||||
|
||||
const res = await browser.runtime.sendMessage({
|
||||
type: 'aria2.send',
|
||||
payload: { endpoint, secret, uris }
|
||||
});
|
||||
if (res?.ok) {
|
||||
els.status.textContent = `Sent ${uris.length} link(s) to aria2.`;
|
||||
} else {
|
||||
els.status.textContent = `Error: ${res?.error || 'unknown'}`;
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreSettings() {
|
||||
const { aria2 } = await browser.storage.local.get('aria2');
|
||||
if (aria2) {
|
||||
if (aria2.endpoint) els.rpcEndpoint.value = aria2.endpoint;
|
||||
if (aria2.secret) els.rpcSecret.value = aria2.secret;
|
||||
} else {
|
||||
els.rpcEndpoint.value = 'http://localhost:6800/jsonrpc';
|
||||
}
|
||||
}
|
||||
|
||||
function wire() {
|
||||
els.btnRefresh.addEventListener('click', refresh);
|
||||
els.btnCopy.addEventListener('click', copyLinks);
|
||||
els.btnSend.addEventListener('click', sendToAria2);
|
||||
[
|
||||
els.fType, els.fTypeRe, els.fName, els.fNameRe, els.fSizeMin, els.fSizeMax,
|
||||
els.fDateFrom, els.fDateTo, els.fCase
|
||||
].forEach(el => el.addEventListener('input', updateCount));
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
wire();
|
||||
await restoreSettings();
|
||||
await refresh();
|
||||
});
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue