Click to See Complete Forum and Search --> : JS greasemonkey problems plz help!!


WhorlyWhelk
November 14th, 2008, 09:00 PM
I am trying to read data from one frame's HTML before replacing it with a different frame using greasemonkey.

Here I demonstrate the problem: (see first 2 lines)

(function(){
if (document.location.href.indexOf("header")!=-1)
alert("poo");

var b=window.location.href.indexOf("cache");
var c=document.getElementsByTagName("script");
for (var psf=0;psf<c.length;psf++){
var p=c[psf].innerHTML.indexOf('updateStats(');
if(p!=-1){
GM_setValue("nst",c[psf].innerHTML.substring(p+12,c[psf].innerHTML.indexOf(')',p)),365);
break;
}
}
var a,f,n,l='';
a=document.evaluate("//frame",document,null,XPathResult.ANY_TYPE,null);
while(f=a.iterateNext()){
n=f.name;
if(n.length==8)
document.location.replace(f.src);
}
do_stuff();
})();

This alerts NOTHING.

But if I add this single else statement:
(function(){
if (document.location.href.indexOf("header")!=-1)
alert("poo");
else alert("crap"); //<------------- ADDED THIS LINE

var b=window.location.href.indexOf("cache");
var c=document.getElementsByTagName("script");
for (var psf=0;psf<c.length;psf++){
var p=c[psf].innerHTML.indexOf('updateStats(');
if(p!=-1){
GM_setValue("nst",c[psf].innerHTML.substring(p+12,c[psf].innerHTML.indexOf(')',p)),365);
break;
}
}
var a,f,n,l='';
a=document.evaluate("//frame",document,null,XPathResult.ANY_TYPE,null);
while(f=a.iterateNext()){
n=f.name;
if(n.length==8)
document.location.replace(f.src);
}
do_stuff();
})();

It now alerts "poo" during the load, as well as a few times "crap".

I really don't know why it does this... but I want to replace those alerts with some real calculations based on substrings within the page. However none of them work in the same way that "poo" isn't alerted. I really don't want to alert things. If I replace that else with something trivial it will also fail.

Any clues on what is happening and how to avoid it?

Thanks!

PeejAvery
November 15th, 2008, 08:32 AM
Have you even tried alerting the document.location.href to see what it's actual contents are?

WhorlyWhelk
November 15th, 2008, 02:17 PM
What? Yea.... I know what it's contents are. One of the frames is always header.php. Greasemonkey calls that script for every document. It goes something like this every time:

header.php
banner.php
body.php

But when I remove that else alert() statement, it doesn't alert when on header.php. :confused:

PeejAvery
November 17th, 2008, 10:38 AM
I think you misunderstood my post...If it is alerting only when you have the else statement in there, then that means that it isn't finding the phrase header in the href. So, you should try alerting the actual href so that you see exactly what JavaScript is trying to evaluate.