Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
function fetchAndExtractHTML(link, overwrite) {
    // Fetch the HTML content from the link
    fetch(link)
        .then(response => response.text())
        .then(htmlContent => {
            // Create a temporary div element
            var tempDiv = document.createElement('div');
            // Set the innerHTML of the temporary div with the fetched HTML content
            tempDiv.innerHTML = htmlContent;
            // Find all elements with the class "quoteheader"
            var quoteHeaderElements = tempDiv.querySelectorAll('.quoteheader');
            // Loop through each quoteheader element
            quoteHeaderElements.forEach(function(element) {
                // Insert extra content before removing the element
                element.insertAdjacentHTML('beforebegin', '<div style="height: 15px;"></div>');
                // Remove the element
                element.remove();
            });
            // Find the specific div element with class "inner"
            var targetDiv = tempDiv.querySelector('.inner');
            if (targetDiv) {
                // Get the HTML content of the target div
                var extractedHTML = targetDiv.innerHTML;
                // Get the element where you want to apply the extracted HTML content
                var element = document.getElementById("maincontent");
                // Set the innerHTML property to the extracted HTML content
                if (overwrite === true) {
                    element.innerHTML = extractedHTML;
                } else {
                    element.innerHTML += extractedHTML;
                }
            } else {
                console.log('No div element with class "inner" found.');
            }
        })
        .catch(error => console.log('Error fetching HTML:', error));
   }


What I have tried:

Using XHTTPRequest but similar results.
Posted
Comments
Pete O'Hanlon 26-Mar-24 3:58am    
Do you get any response back when you issue the fetch, does it return bad data, or does it just hang? Right now, there's a lot going on in that code and you're asking us to guess which part doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900