Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon.nova-pro-v1:0· AmazonHugging Face LMArena Human Preference 140K

write javascript to get a list of all urls on a website in a array with the following…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash’s answer.
  1. User

    write javascript to get a list of all urls on a website in a array with the following attributes:

    url text inside the a tag text of the nearest h1 or h2 tag in the node chain

  2. Amazon Nova Assistant

    Certainly! Below is a JavaScript code snippet that will gather all URLs on a webpage into an array. Each element in the array will have the following attributes:

    • url: The URL from the href attribute of the a tag.
    • text: The text inside the a tag.
    • headingText: The text of the nearest h1 or h2 tag in the node chain.
    document.addEventListener("DOMContentLoaded", function() {
        const links = document.querySelectorAll('a');
        const result = [];
    
        links.forEach(link => {
            if (link.href) {
                const urlObject = {
                    url: link.href,
                    text: link.textContent.trim(),
                    headingText: getNearestHeadingText(link)
                };
                result.push(urlObject);
            }
        });
    
        console.log(result);
    });
    
    function getNearestHeadingText(element) {
        let currentNode = element;
        
        while (currentNode) {
            if (currentNode.tagName === 'H1' || currentNode.tagName === 'H2') {
                return currentNode.textContent.trim();
            }
            currentNode = currentNode.parentNode;
        }
        
        return null;
    }
    

    Explanation:

    1. Event Listener: The script waits for the DOM to be fully loaded using DOMContentLoaded.
    2. Query Selector: It selects all a tags on the page using document.querySelectorAll('a').
    3. Iteration: It iterates over each a tag.
    4. URL Object: For each a tag, it creates an object with the required attributes:
      • url: The href attribute of the a tag.
      • text: The text content inside the a tag, trimmed of any extra whitespace.
      • headingText: The text of the nearest h1 or h2 tag found by traversing up the node chain.
    5. Helper Function: getNearestHeadingText traverses up the DOM tree from the given element to find the nearest h1 or h2 tag and returns its text content. If no such tag is found, it returns null.

    Usage:

    • You can run this script in the browser’s console or include it in your webpage to log the array of URL objects to the console.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.