Search code examples
javascriptxmlhttprequesttampermonkey

How to retreive data from another webpage that's dynamically loaded without using iframes? In a tampermonkey script


Apologies if there's an easy answer to this, I'm still new and trying to learn. I'm making a Tampermonkey script for a site (site A) that needs to get a string from a separate webpage (site B) to function correctly. I tried using GM_xmlhttprequest, but site B has almost all of its content loaded dynamically, so the string I needed wasn't being retrieved. When I check the network tab, there's around 130 separate requests that are made when loading.

I've gotten the script to technically work now via an iframe that loads site B as hidden, but when I do the console is full of "Source map error: Error: request failed with status 403" statements. The string that the script needs to function does load, so it technically is working, but I don't want all the console spam.

Both of these sites are company domains that I don't have access to change any settings for. Is there any way to load that dynamically generated content without using an iframe? Or is there any way to put in a header or something that requests the CORS access?

This is what I've got working now, but I feel like leaving all the 403 errors is not good practice.

            const iframe = document.createElement('iframe');
            iframe.setAttribute('src', 'linkhere');
            document.body.insertBefore(iframe, document.body.firstChild);
            iframe.style.display = 'none';

Solution

  • Update for anyone who may come across this, I was able to figure it out.

    I ended up going back to GM_xmlhttprequest and just creating a separate function that waited until the JSON content loaded to run the next function. Trimmed out most of what I used but here's sort of how I did it

    GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function (response) {
            // parse the JSON data
            var dataContent = JSON.parse(response.responseText);
        }
    });
    
    function waiter() {
        setTimeout(function () {
            // if dataContent is not null, continue
            if (dataContent) {
                queryCreate();
            } else {
                // if dataContent is null, wait 1/2 second and try again
                waiter();
            }
        }, 500);
    }
    waiter();