-1

Trying to automate a task in the browser with Greasemonkey.

Requirement: load a web page which contains a table displaying 15 out of 340+ results, with button controls to see the next page worth of results, go back, go to the end and so forth.

  1. Collect the first 15 results --> got this working
  2. Click on the 'Next' button --> got this working
  3. Wait until the page loads the next set of results --> dont know how
  4. Repeat until the next button is disabled --> got this working

Cant show pictures cuz its a corporate app.

So far if I loop with setTimeouts, the timeouts get queued up and executed at the same time I think. Instead, the requirement is for the code to run the info collection, click next, wait, then collect info again.

1
  • -why wait for the page to load next 15 results? ! Commented Jun 7, 2017 at 22:51

4 Answers 4

0

You could try a utility function waitForKeyElement, a greasemonkey utility script. It is essentially doing what you were trying to do before. It sets a timeout to continuously checks to see if an element is loaded. Executing it would look something like this:

waitForKeyElements (
     "div.comments", 
     commentCallbackFunction
);

Where comment callback function would be the function that collects results.

It would help also to understand why setTimeout in a loop doesn't work. setTimeout does not block execution of a script to run your callback function. It queues up that function to be executed no earlier than the time that you pass in as a second parameter. Because of this, the loop runs very quickly and instead of getting function calls at 3000ms, 6000ms, and 9000ms... as you might expect you get something closer to function calls at 3000ms, 3001ms, 3002ms.

Sign up to request clarification or add additional context in comments.

Comments

0

You can execute arbitrary JS logic mixed with API calls, recursion and timeouts sequentially via synchronous executor nsynjs:

function synchronousCode() {
    var i=0;
    while(true) {
      console.log("waiting 1 sec, iteration", i++);
      $('#myDiv').toggle();
      nsynWait(nsynjsCtx,1000);
    };
}

var ctx;
function btnStopClicked() {
    ctx.stop();
    ctx=null;
}

function btnStartClicked() {
    ctx=nsynjs.run(synchronousCode,{},function(){
			console.log("Synchronous Code done");
		});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://rawgit.com/amaksr/nsynjs/master/nsynjs.js"></script>
<script src="https://rawgit.com/amaksr/nsynjs/master/wrappers/nsynWait.js"></script>
<body>
  <button id="buttonStart" onclick="btnStartClicked()">Start</button>
  <button id="buttonStop" onclick="btnStopClicked()">Stop</button>
  <div id="myDiv">Flashing div</div>
</body>

See more examples here: https://github.com/amaksr/nsynjs/tree/master/examples

Comments

0

Thanks people, found this post to be the best answer Synchronous setTimeout + Loop

This way the page loads, the first values are read, the next button is clicked, next results appear and are read, until the last page.

Thanks again for the input and ideas

Comments

0

you can use it

for(const elment of arrayElements) {
            await yourFunc(elment)
            await yourOtherFunc('somePatameter')
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.