← Pablo Murad

Snippets

Link Extractor, every link on a page in a text file

Link Extractor is a small script I wrote: run it on any page and it downloads every link there as a text file, one full address per line, without duplicates.

A small script I wrote that collects every link on the current page and downloads them as a text file, one address per line.

It reads every <a> that has an address, and also <area>, the clickable regions of image maps.1 Relative links become full addresses, javascript: links are skipped, and duplicates are dropped because the list lives in a Set.2 The file is built in the browser and named after the site, so on this blog it's links-pablomurad.com.txt.3 At the end, an alert says how many unique links it found.

Run it in the developer console, or keep it as a bookmarklet like Font Peek and Page to Markdown: a new bookmark with javascript: followed by the whole file in the address field.

link-extractor.js raw
/*
  @name Link Extractor
  @description Extract all hyperlinks from the current page and download them as a TXT file
  @author Pablo Murad · https://pablomurad.com
*/

(() => {
    const links = new Set();

    document.querySelectorAll('a[href], area[href]').forEach(element => {
        const href = element.getAttribute('href').trim();

        if (!href || /^javascript:/i.test(href)) return;

        try {
            links.add(new URL(href, document.baseURI).href);
        } catch {
            /* Ignore invalid URLs */
        }
    });

    if (links.size === 0) {
        alert('No links found!');
        return;
    }

    const blob = new Blob(
        [Array.from(links).join('\n')],
        { type: 'text/plain;charset=utf-8' }
    );

    const url = URL.createObjectURL(blob);
    const download = document.createElement('a');

    download.href = url;
    download.download = `links-${location.hostname}.txt`;

    document.body.appendChild(download);
    download.click();
    download.remove();

    setTimeout(() => URL.revokeObjectURL(url), 1000);

    alert(`${links.size} unique links extracted!`);
})();

curl -O https://pablomurad.com/snippets/link-extractor.js

The top of links-pablomurad.com.txt opened in a browser: one full address per line, starting with https://pablomurad.com/#content, https://pablomurad.com/, mailto:pablo@pablomurad.com, then GitHub, git.pablomurad.com, Codeberg, sr.ht, X, LinkedIn, murad.social, Telegram and Discord links.
The start of the file it saved from the front page of this blog.

On the front page of this blog it found 49 links.

Notes

  1. An image map splits one picture into clickable areas, each with its own link. The <area> element came from a 1996 proposal for client-side image maps, RFC 1980, and made it into HTML 3.2. ↩

  2. A Set keeps one copy of each value, so a link that shows up in both the menu and the footer is counted once. new URL(href, document.baseURI) resolves relative addresses against the address of the page. ↩

  3. URL.createObjectURL gives the file a temporary address inside the browser, so the download link has something to point at. The script revokes it a second later to free the memory. ↩

Reply by email

Comments

Leave a comment

Comments are read before they show up here. No email address needed.