Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  156] [ 7]  / answers: 1 / hits: 6078  / 10 Years ago, mon, april 14, 2014, 12:00:00

I write a small greasemonkey script to open links at this typehttp://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.


More From » php

 Answers
6

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.


[#46047] Saturday, April 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
masonm

Total Points: 167
Total Questions: 87
Total Answers: 103

Location: Rwanda
Member since Wed, Jun 8, 2022
2 Years ago
masonm questions
;