Skip to content

Instantly share code, notes, and snippets.

@fgm
Created December 20, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgm/27c8b45a7e9ee15e438cdaf19d9ba366 to your computer and use it in GitHub Desktop.
Save fgm/27c8b45a7e9ee15e438cdaf19d9ba366 to your computer and use it in GitHub Desktop.
List the hosts referenced on a HTML page (href, src), by decreasing occurrence count
/**
* List the hosts referenced on a HTML page.
*
* Licence: MIT.
*/
a = {};
document.querySelectorAll("a[href],[src]").forEach((v, k) => {
const raw = v.getAttribute("href") || v.getAttribute("src");
if (raw[0] !== "h") {
return;
}
const u = new URL(raw);
if (typeof a[u.host] == "undefined") {
a[u.host] = 0;
}
a[u.host]++;
});
b = Array.from(Object.entries(a)).sort((x, y) => {
if (x[1] < y[1]) {
return 1;
}
if (x[1] > y[1]) {
return -1;
}
if (x[0] < y[0]) {
return -1;
}
if (x[0] > y[0]) {
return 1;
}
return 0;
});
c = b.reduce((accu, curr) => {
accu.push({ host: curr[0], count: curr[1] });
return accu;
}, []);
console.table(c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment