Auto-search webpage for text, w/ refresh
|
|
Hey guys, I was wondering if anyone could make a script that would auto-refresh the page every 15 seconds or so. Then, use a loop to continuously search for a word (or string of words I guess). When it finds the word, it would also be helpful to play some sort of noise to alert the user that the word has appeared! Could this be done? Thanks for reading (my first post btw) Cheers, |
|
|
something like:
var words="Something to look for"; // words to look for
var goaway=setTimeout(function(){ // begin countdown
location.reload() //reload the page in 15 seconds
},15000); // units are in milliseconds!!
finder=function(){ // set-up a looker
if (document.body.innerHTML.indexOf(words)>-1) {
clearTimeout(goaway); // stop countdown
clearInterval(checker); // stop searching
alert("Match found!!"); // alert a match
}
}
var checker=setInterval(finder,500) // initialize the looker to look every half a second
|
|
|
I was actually in the process of making a script with this exact idea a while ago when my hard drive went screwy and I had to format my windows (and forgot to back up my scripts. I now keep a backup on my site everytime I change a script) |
|
|
This would be a very very awesome and much appreciated program if it were made... :-) I'll give some kudos to whomever does it.
|
|
|
It works... just tell me the website to run it in or whatnot. Here's the code (right click on the monkey, choose "new script", type in the details, then paste this in:)
var words="Something to look for"; // words to look for
var goaway=setTimeout(function(){ // begin countdown
location.reload() //reload the page in 15 seconds
},15000); // units are in milliseconds!!
finder=function(){ // set-up a looker
if (document.body.innerHTML.indexOf(words)>-1) {
clearTimeout(goaway); // stop countdown
clearInterval(checker); // stop searching
alert("Match found!!"); // alert a match
}
}
var checker=setInterval(finder,500) // initialize the looker to look every half a second
|
|
|
Works great thus far! Sorry for not having realized your original post was indeed the code. Now I had one more tweak for that I would suggest for the program. The pages I am using this code on most have a window pop-up every time I try to refresh the page. The window can be seen Basically, I have to hit resend every 15 seconds in order for the page to refresh. |
|
|
hey, can you give us the source code? the screenshot isn't very helpful, we know what a prompt it lol ;) |
|
|
you could do this with XHR so you don't have to be on the page all the time. seriously avg?
var words="Something to look for"; // words to look for
var goaway=setTimeout(function(){ // begin countdown
location.reload() //reload the page in 15 seconds
},15000); // units are in milliseconds!!
finder=function(){ // set-up a looker
if (document.evaluate('//child::text()[contains(.,"'+words+'")]', document, null, 9, null).singleNodeValue) {
clearTimeout(goaway); // stop countdown
clearInterval(checker); // stop searching
alert("Match found!!"); // alert a match
}
}
var checker=setInterval(finder,500) // initialize the looker to look every half a second
|
|
|
Because indexOf tramples evaluate in term of execution time. While this is a small script, xpath definitely isn't the best option. |
|
|
The source-code can be found at here. There are auto-refresher addons offered by Mozilla just none that search for a word in the page and then alert the user if it is found. A good example of this is Reload Every v3. In this add-on, when it tries refreshing the page for the first cycle it pops up a window that says:
Either way, thanks for all the feedback thus far! Truly appreciate it all. |
|
|
@Tim Smart
@siralex
|
|
|
Okay yeah you are right Tim. Sorry. Also note that indexOf and regexp are very close, but yeah they both blow evaluate out of the water. |
|
|
K, I have no idea what XHR is but that sounds alright to me. I don't know how long those take to write either but whenever you get some free-time would be awesome. Thanks again for the quick feedback and response thus far. |
|
|
K, I have no idea what XHR is but that sounds alright to me |
|
|
Something like this I would guess.
var url = "whatever page you are searching";
var words = "Something to look for";
function finder(xhr){ // set-up a looker
if (xhr.indexOf(words)>-1) {
alert("Match found!!"); // alert a match
document.location.href = url;
} else {
setTimeout(function() {get(url);},15000);
}
}
function get(url) {
GM_xmlhttpRequest({
method: "GET",
url: url,
onload: function(xhr) { finder(xhr.responseText); }
});
}
setTimeout(function() {get(url);},15000);
|
|
|
http://lmgtfy.com/?q=XHR Oh my god avg! That is awesome! I'm going to to use that so much lol |
|
|
Because indexOf tramples evaluate in term of execution time. While this is a small script, xpath definitely isn't the best option. I got evaluate to perform better than indexOf.
javascript:words="Because it's your web.";
start=new Date().getTime();
regex = new RegExp(words);
if(regex.exec(document.body.innerHTML)) alert("regexp: "+(new Date().getTime()-start));
start=new Date().getTime();
if (document.evaluate('//child::text()[contains(.,"'+words+'")]', document, null, 9, null).singleNodeValue) alert("evaluate: "+(new Date().getTime()-start));
start=new Date().getTime();
if (document.body.innerHTML.indexOf(words)>-1) alert("indexOf: "+(new Date().getTime()-start));
|
|
|
@Chris D
|
|
|
Really? I always though indexOf was really slow. You wouldn't have any data to back that up by any change? I'm also curious, how would Regexp compare. Actually... indexOf is much faster than RegExp. Here are my tests... Function 1 is this:
The results are:
Function 1 is 1,797 ms (5.61x) faster than Function 2 As you can see, indexOf was 5.61 times faster than RegExp. You can test this or anything for yourself in my Measure Functions' Execution Times script. I've found that comparing searching innerHTML and textContent, that searching the textContent is 27x faster. You can test for yourself in my script using document.body. |
|
|
Yes it's true indexOf() is faster than a RegExp.
|
|
|
Yeah you would use xpath instead of indexOf for innerHTML on the document.body.
I've taken sizzlemctwizzle's method and put it into a function if anyone needs a simple, quick way to search the html of the body...
// Search body's inner html
function searchDoc(t) {
return document.evaluate("//child::text()[contains(.,'"+t+"')]",document,null,9,null).singleNodeValue!=null;
}
Example:
hello = searchDoc('Hello world') ? true : false;
Probably not best to search the innerHTML of a node when textContent is faster and will give you less hassle.
"None are more hopelessly enslaved than those who falsely believe they are free." - Johann Wolfgang von Goethe My website |
|
|
Why returning a boolean value? If I'm searching for something I would also know where it has been found. I believe that you have also to escape the text with something like |
|
|
No, apostrophes don't have to be escaped if they're inside of quotes. My Java teacher thought so too... it works the same, but you don't need it. "'" // allowed "\'" // allowed, but not needed |
|
|
What happens if you search something like "can't" with the JoeSimmons's function?
I'm quite sure that you will receive an error! |
|
|
Probably but I can't think of a way where it will be completely bugless. |

here