Cookie Consent by Free Privacy Policy Generator

Extract and Download Link Data using Chrome DevTools

Quickly extract all the href links on the current webpage that you're currently viewing. Paste the JS snippet below into Chrome DevTool Console. It will collect all the links (internal and external), sort them and save the data to a CSV that you can download.

Code:
(function() {
    const results = [['Url', 'Anchor Text', 'External']];
    const urls = document.getElementsByTagName('a');

    Array.from(urls).forEach((url) => {
        // Determine if the link is external
        const externalLink = new URL(url.href, document.baseURI).host !== window.location.host;
        // Check if URL is valid and contains '://'
        if (url.href && url.href.includes('://')) {
            results.push([
                url.href,
                url.textContent || url.innerText, 
                externalLink 
            ]);
        }
    });

    // Generate CSV content
    const csvContent = results.map(line => {
        return line.map(cell => {
            if (typeof cell === 'boolean') return cell ? 'TRUE' : 'FALSE';
            if (!cell) return '""'; // Ensure empty fields are represented correctly
            let value = cell.replace(/[\r\n]+/g, " ").trim(); // Simplify whitespace and trim
            return `"${value}"`; 
        }).join(',');
    }).join('\n');


    const blob = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'});
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.setAttribute('href', url);
    link.setAttribute('download', 'links.csv');
    document.body.appendChild(link); // Required for Firefox
    link.click(); // Trigger download
    document.body.removeChild(link); 
})();

Here's a quick explanation of what the script does:

  • Extracts Link Data: Gathers all <a> elements on the webpage and retrieves their href attribute (URL), the text inside the anchor tag (anchor text), and determines whether the link is external (not part of the current domain).
  • Formats as CSV: Arranges the extracted data into a CSV format with headers 'Url', 'Anchor Text', and 'External'. Each link's data is properly formatted, with text values enclosed in quotes to ensure CSV integrity, especially if the text contains commas or quotes.
  • Downloads Automatically: Converts the formatted CSV data into a Blob, which is then used to create a temporary downloadable link in the browser. Clicking this link triggers the download of the data as a 'links.csv' file, facilitating easy access and use outside the browser.
 
Back
Top