0

This is a GreaseMonkey specific request. I'm trying to use the GM_xmlhttpRequest to make a GET request to a page (cross domain). The problem is, the target page fetches a lot of its content after the page has been loaded once (and until then it shows a progress bar for a few seconds) when opened in the browser (again through AJAX). I want to fetch the contents of this page only when everything has been loaded, so am simply looking for a way to add a few seconds delay in the request. Is it even possible? If yes, how can I do that?

Edit: I think I'm not clear enough. I am calling a page like this:

    GM_xmlhttpRequest({
method: "GET",
url: "http://example.com",
onload: function(response) {
            if(response.responseText.length > 0)
            {
                callBack(response.responseText);
            }
        },
onerror: function(response) {
            log("Error in fetching contents: " + response.responseText);
        }

    });

The page I'm actually using instead of example.com loads once but then delay loads its useful contents. but the response.responseText only contains the initial load HTML.

Thanks!

Piyush Soni
  • 1,356
  • 3
  • 17
  • 40
  • If the content is loaded with ajax, you would probably have to look for some handler to hook into in the source of that page ? – adeneo Apr 06 '15 at 19:27
  • @adeneo: I'm not sure I understand what you mean by that. I am trying to fetch the contents of a page which has its contents brought dynamically through AJAX. I am making my request on some other domain for that page using GM_xmlhttpRequest. – Piyush Soni Apr 06 '15 at 20:29
  • And you would have to wait until that page loads it's content, hooking into some sort of event etc. Of course, just getting the HTML gets you nowhere, you'd need to simulate a browser, and I'm not sure where you'd even start doing something like that from a tampermonkey script, but try looking for headless browsers. – adeneo Apr 06 '15 at 20:50
  • @adeneo: There are already methods using which I can simulate the browser DOM methods on the obtained HTML. Something like this: function parseHTML(text) { var doc = document.implementation.createHTMLDocument("TestTitle"); doc.documentElement.innerHTML = text; return doc; } – Piyush Soni Apr 06 '15 at 21:19
  • That's not really the same, you fetch the HTML and you parse it. You have to simulate *a browser* to get the scripts running and load the content with javascript. – adeneo Apr 06 '15 at 21:31
  • Aah. Get it what you mean. That probably just means it's not possible using the standard browser and standard GreaseMonkey/TamperMonkey methods. I was just thinking may be there is an event like 'onprogress' similar to onload, onerror in the request itself which I could use. Thanks for your helpful comments though. – Piyush Soni Apr 06 '15 at 22:38
  • You can use Brock's waitForKeyElements https://gist.githubusercontent.com/BrockA/2625891/raw/fd02ec05e3079cdd52cf5892a7ba27b67b6b6131/waitForKeyElements.js – Edge Apr 09 '15 at 07:52

1 Answers1

0

You can use a simple setTimeout that would delay by a fixed amount of time (e.g. 2 seconds if the page loads fast, or more if you want to make sure).

Another solution would be to use mutation observers on the body for node added or removed and then fire your script and kill the observer.

For references, the example of the MDN site really helped my understand and use mutation observers for heavy face lifting userscripts

Louis
  • 593
  • 4
  • 13
  • 1
    I'm not sure if I explained my question clearly. It's not the page I'm calling the script from which is having the delay in loading. It's the page I want to fetch the contents of using GM_xmlhttpRequest GET. The result of my GET request returns an almost empty HTML document, since that page loads its contents through its own AJAX I have no control over. – Piyush Soni Apr 06 '15 at 20:25
  • thats exactly what my suggestion is for, your script can wait for the document to be ready, then set either a timer because you know roughly how long it takes for the page to load, or a mutation observer because you can look for specific elements that will get populated once the page loads – Louis Apr 07 '15 at 01:02
  • Sorry, that won't work. – Piyush Soni Apr 08 '15 at 13:39