Created
November 28, 2023 19:38
-
-
Save ericviana/9ed5d7ef9d626e2a0517f97163d89059 to your computer and use it in GitHub Desktop.
Extract all urls from a website
This file contains 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
const results = [ | |
['Url', 'Anchor Text', 'External'] | |
]; | |
var urls = document.getElementsByTagName('a'); | |
for (urlIndex in urls) { | |
const url = urls[urlIndex] | |
const externalLink = url.host !== window.location.host | |
if(url.href && url.href.indexOf('://')!==-1) results.push([url.href, url.text, externalLink]) // url.rel | |
} | |
const csvContent = results.map((line)=>{ | |
return line.map((cell)=>{ | |
if(typeof(cell)==='boolean') return cell ? 'TRUE': 'FALSE' | |
if(!cell) return '' | |
let value = cell.replace(/[\f\n\v]*\n\s*/g, "\n").replace(/[\t\f ]+/g, ' '); | |
value = value.replace(/\t/g, ' ').trim(); | |
return `"${value}"` | |
}).join('\t') | |
}).join("\n"); | |
console.log(csvContent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment