Skip to content

Instantly share code, notes, and snippets.

@yradunchev
Created June 15, 2023 09:33
Show Gist options
  • Save yradunchev/d09241bae1e71a706093758f4e263437 to your computer and use it in GitHub Desktop.
Save yradunchev/d09241bae1e71a706093758f4e263437 to your computer and use it in GitHub Desktop.
Extract Links from Web Page

Open Google Chrome Developer Tools with Cmd + Opt + i (Mac) or F12 (Windows).
Click on the Console tab.
Copy-Paste the following JavaScript code and press Enter.

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