[Greasemonkey] document.getElementsByTagName("a") problem

Jeremy Dunck jdunck at gmail.com
Wed Sep 7 11:59:53 EDT 2005


On 9/7/05, Ying Zhang <meow at regretless.com> wrote:
...
>     uim = cand.getAttribute('href');
>     // alert(uim);
>     if(uim.search("#comment") >= 0)
>                 alert(uim);
...
> The funny thing is if I comment out the
> .search if state, and uncomment the alert(uim), it produces more links
> on the page than the if statement. 

Why is that funny?  If's are conditional, so any code inside the if
branch will be hit the same or fewer times as code outside the if.

I assume you're saying you're changing the code from this:
     uim = cand.getAttribute('href');
     // alert(uim);
     if(uim.search("#comment") >= 0)
                 alert(uim);

to this:
     uim = cand.getAttribute('href');
      alert(uim);
     //if(uim.search("#comment") >= 0)
     //            alert(uim);

If that's correct, I would expect the latter code to alert at least as
much as the former.

At any rate, this does almost exactly what you want:

http://diveintogreasemonkey.org/patterns/match-attribute.html

I'm attaching a script which is adapted a bit to your need.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: find-comments.js
Type: application/x-javascript
Size: 298 bytes
Desc: not available
Url : http://mozdev.org/pipermail/greasemonkey/attachments/20050907/df798056/find-comments.js


More information about the Greasemonkey mailing list