Files
tool__scripts/browser-snippets/href-collector__reddit.js
2026-07-09 01:05:56 +02:00

77 lines
2.2 KiB
JavaScript

let collectedHrefs = JSON.parse(localStorage.getItem('collectedHrefs')) || [];
function collectInitialHrefs(targetNode) {
const links = targetNode.querySelectorAll('a[href]');
links.forEach(link => {
if (!collectedHrefs.includes(link.href)) {
collectedHrefs.push(link.href);
localStorage.setItem('collectedHrefs', JSON.stringify(collectedHrefs));
console.log(`🙄 Snagged an initial href: ${link.href}`);
}
});
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
const links = addedNode.querySelectorAll('a[href]');
links.forEach((link) => {
if (!collectedHrefs.includes(link.href)) {
collectedHrefs.push(link.href);
localStorage.setItem('collectedHrefs', JSON.stringify(collectedHrefs));
console.log(`🙄 Added href: ${link.href}`);
}
});
}
});
});
});
function waitForElement(selector, callback) {
if (document.querySelector(selector) != null) {
console.log("💠‍🌐 Target acquired. Commencing operation collect-hrefs.");
callback(document.querySelector(selector));
} else {
setTimeout(() => waitForElement(selector, callback), 500);
}
}
waitForElement(".main-container", (targetNode) => {
collectInitialHrefs(targetNode);
const config = { childList: true, subtree: true };
observer.observe(targetNode, config);
});
function downloadData(data, fileName, type = 'text/plain') {
const blob = new Blob([data], { type });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = fileName;
link.click();
URL.revokeObjectURL(url);
}
function stopObserving() {
observer.disconnect();
console.log("💠‍🌐 I've ceased surveillance. Here's what I've collected:");
const data = JSON.parse(localStorage.getItem('collectedHrefs')).join('\n');
console.log(data);
downloadData(data, 'hrefs__reddit.txt');
}
function clearCollectedHrefs() {
localStorage.removeItem('collectedHrefs');
collectedHrefs = [];
console.log("💠‍🌐 Local storage cleared. The slate is wiped clean.");
}