CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2007
    Posts
    96

    JS greasemonkey problems plz help!!

    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)

    Code:
    (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:
    Code:
    (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!

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: JS greasemonkey problems plz help!!

    Have you even tried alerting the document.location.href to see what it's actual contents are?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2007
    Posts
    96

    Re: JS greasemonkey problems plz help!!

    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.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: JS greasemonkey problems plz help!!

    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.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured