HARPA.AI
LIBRARYUSE CASESGUIDESAI COMMANDSBLOG

🧩  Extract URLs From Page

Extracts all urls from the current webpage.

Created by HARPA AI
Updated on Nov 9, 04:32
Installed 110 times
RUNS JS CODE

How to Use

IMPORT COMMAND

Content

- code: |-
    // Function to extract all URLs from the page
    function extractUrls() {
        const urls = [];

        // Select all anchor (a) elements on the page
        const anchorElements = document.querySelectorAll('a');

        // Iterate through each anchor element and extract the full URL
        anchorElements.forEach(function (anchor) {
            const url = anchor.href; // Use anchor.href to get the full URL

            // Check if the URL is not null and not empty
            if (url && url.trim() !== '') {
                urls.push(url);
            }
        });

        // Return the list of URLs as a string with each item separated by new lines
        return urls.join('\n');
    }

    // Function to check if the DOM content has already loaded
    function isDOMLoaded() {
        return document.readyState === 'complete' || document.readyState === 'interactive';
    }

    // Check if the DOM content is already loaded
    if (isDOMLoaded()) {
        // If already loaded, immediately call extractUrls
        const urlList = extractUrls();
        console.log(urlList);
    } else {
        // If not loaded, wait for the DOMContentLoaded event
        document.addEventListener('DOMContentLoaded', function () {
            const urlList = extractUrls();
            console.log(urlList);

            // You can use the urlList variable as needed in your application
        });
    }

    urlList = extractUrls();

    return urlList;
  type: js
  args: ''
  param: links
- message: '{{links}}'
  type: say
Contact us
HomeUse CasesGuidesPrivacy PolicyTerms of Service
CAN WE STORE COOKIES?
Our website uses cookies for the purposes of accessibility and security. They also allow us to gather statistics in order to improve the website for you. More info: Privacy Policy