Files
tool__scripts/browser-snippets/a.src-grabber.js
2026-07-09 01:05:56 +02:00

80 lines
2.2 KiB
JavaScript

const id = 'collectedAHref';
let collectedImgSrc = JSON.parse(localStorage.getItem(id)) || [];
function collectInitialSrc(targetNode) {
const elems = targetNode.querySelectorAll('a[href]');
elems.forEach(elem => {
if (!collectedImgSrc.includes(elem.href)) {
collectedImgSrc.push(elem.href);
localStorage.setItem(id, JSON.stringify(collectedImgSrc));
console.log(`🙄 Snagged an initial a.href: ${elem.href}`);
}
});
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
const elems = addedNode.querySelectorAll('a[href]');
elems.forEach((elem) => {
if (!collectedImgSrc.includes(elem.href)) {
collectedImgSrc.push(elem.href);
localStorage.setItem(id, JSON.stringify(collectedImgSrc));
console.log(`🙄 Added a.href: ${elem.href}`);
}
});
}
});
});
});
function waitForElement(selector, callback) {
if (document.querySelector(selector) != null) {
console.log("💠‍🌐 Target acquired. Commencing operation collect-a.srcs.");
callback(document.querySelector(selector));
} else {
setTimeout(() => waitForElement(selector, callback), 500);
}
}
collectInitialSrc(document.body);
const config = { childList: true, subtree: true };
observer.observe(document.body, 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 stopObservingAHref() {
observer.disconnect();
console.log("💠‍🌐 I've ceased surveillance. Here's what I've collected in a.href:");
const data = JSON.parse(localStorage.getItem(id)).join('\n');
console.log(data);
downloadData(data, 'a-hrefs.txt');
}
function clearCollectedAHrefs() {
localStorage.removeItem(id);
collectedImgSrc = [];
console.log("💠‍🌐 Local storage cleared. The slate is wiped clean.");
}