80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
const MAGNET_STORAGE_KEY = 'nova_magnets';
|
|
|
|
function getStoredMagnets() {
|
|
const raw = localStorage.getItem(MAGNET_STORAGE_KEY);
|
|
|
|
if (!raw) return [];
|
|
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function setStoredMagnets(magnets) {
|
|
const unique = Array.from(new Set(magnets));
|
|
localStorage.setItem(MAGNET_STORAGE_KEY, JSON.stringify(unique));
|
|
}
|
|
|
|
(function collectMagnets() {
|
|
const pageMagnets = [...document.querySelectorAll('a')]
|
|
.map(a => a.href)
|
|
.filter(href => href && href.startsWith('magnet:'));
|
|
|
|
if (pageMagnets.length === 0) {
|
|
console.log('No magnet links found on this page.');
|
|
return;
|
|
}
|
|
|
|
const stored = getStoredMagnets();
|
|
const combined = stored.concat(pageMagnets);
|
|
|
|
setStoredMagnets(combined);
|
|
|
|
console.log(`Stored ${pageMagnets.length} new magnet(s). Total in storage: ${getStoredMagnets().length}`);
|
|
})();
|
|
|
|
async function copyStoredMagnetsToClipboard() {
|
|
const magnets = getStoredMagnets();
|
|
if (magnets.length === 0) {
|
|
console.log('No stored magnets to copy.');
|
|
return;
|
|
}
|
|
|
|
const text = magnets.join('\n');
|
|
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
console.log('Successfully copied magnets to clipboard (navigator.clipboard).');
|
|
return;
|
|
} catch (err) {
|
|
console.warn('navigator.clipboard failed, falling back to execCommand:', err);
|
|
}
|
|
}
|
|
|
|
const tempTextArea = document.createElement('textarea');
|
|
tempTextArea.value = text;
|
|
document.body.appendChild(tempTextArea);
|
|
tempTextArea.select();
|
|
|
|
try {
|
|
const successful = document.execCommand('copy');
|
|
const msg = successful
|
|
? 'Successfully copied magnets to clipboard (execCommand fallback).'
|
|
: 'Failed to copy magnets to clipboard (execCommand fallback).';
|
|
console.log(msg);
|
|
} catch (err) {
|
|
console.error('Fallback: unable to copy magnets', err);
|
|
} finally {
|
|
document.body.removeChild(tempTextArea);
|
|
}
|
|
}
|
|
|
|
function clearStoredMagnets() {
|
|
localStorage.removeItem(MAGNET_STORAGE_KEY);
|
|
console.log('Cleared stored magnets.');
|
|
}
|