Created
December 20, 2019 12:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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