简体   繁体   中英

Write a script to open new link and click a button

I write a small greasemonkey script to open links at this type http://example.com/details.php?id=*&very=1 and click a hello button at those link, but somehow it can't work

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful

// @include http://example.com/details.php?id=*&very=1

// @copyright  2012+, You
// ==/UserScript==

  function sayhello(){
     for(var i=1;i<100;i++){
      window.location.href='http://example.com/details.php?id='+i+'&very=1'
      $("#hello").click()}
      }
    };

   sayhello();

But it does not work as my expection. And since the new href fit the include rules, it will always load and load...

I rewrite it to:

   function sayhello(){
     var i=window.location.href.match(/\d+/)
     j=Number(i)+1
     window.location.href=window.location.href.replace(/\d+/, j);
     $("#hello").click()

    };

   sayhello();

It works slowly since every time it will load the whole new page and sometimes if something in a page do not load correctly it will just stay on that page waiting and loading and then crash.
How to just skip the can not load correctly pages and guarantee the script running?

And how to speed it up, i just need to click a specific button ,not need to load the pictures or the other stuff.

Anyone can help?

Thanks.

Like other JavaScript that runs on web pages, Greasemonkey scripts do not preserve state across page loads. As a result,

for(var i = 1; i < 100; i++) {
    window.location.href = 'http://example.com/details.php?id=' + i + '&very=1';
    $("#hello").click();
}

will try to load http://example.com/details.php?id=1&very=1 . In doing this, it unloads itself and discards its state. It does not get to $("#hello").click(); .

Rather than looking at it so procedurally, think about it this way: you are on a web page. You want to click the button, then go to the next web page. With that in mind, you could write it like this:

$("#hello").click();
var currentID = /* retrieve current ID from URL */;
var nextID = currentID + 1;
if(nextID < 100) {
    window.location.href = 'http://example.com/details.php?id='
                         + nextID + '&very=1';
}

Depending on what $("#hello").click() does, you might or might not be giving it enough time to complete what it's supposed to do. You might be able to get around that by adding a setTimeout before continuing on to the next page, or trying to find some sort of callback to hook.

Lastly, if you want it sped up, not loading images or anything, then the solution is to not use a browser . The way to make it run the fastest would be to figure out what exactly clicking #hello does, then writing a script outside of the browser to mimic that action. With any luck, you won't need to load images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM