[Greasemonkey] Help with Replace

John Plsek gm at plsek.id.au
Thu Aug 4 12:41:10 EDT 2005


Randy Reed wrote:

> Hi All,
> I'm trying to write a script that changes one href to another and I am 
> encountering a frustrating error. I use a xpath node to get the href, 
> copy it to string and then try to modify it with replace. However, 
> when I run it I get the message "thisLink has no properties". When I 
> type the same commands in the javascript shell, it works fine. I'm 
> running 0.5 of greasemonkey and 1.0.6 of firefox. All help is 
> appreciated, code is below.
>
> Thanks
> Randy
>
> // ==UserScript==
> // @name ChangeHref
> // @description script to reroute downloads through the proper port
> // @include https://schoolconnect.*
> // ==/UserScript==
>
>
> function xpath(query) {
>     return document.evaluate(query, document, null,
>         XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
> }
>
> var allLinks;
> var newLink;
> var thisLink;
>
> allLinks=xpath('//a[@href]')
>
> thisLink=allLinks.iterateNext();
> while (thisLink) {
>     
>     thisLink=allLinks.href;
>     GM_log('current link =' + thisLink);
>     newLink=thisLink.replace(/^https:/,'http');
>     GM_log('new link = '+ newLink);
>     thisLink=allLinks.iterateNext();
> }
>
>
the problem line is:

thisLink=allLinks.href;

allLinks is the result of the document.evaluate ... I think you may have 
wanted to do something like

var allLinks;
var newLink;
var thisNode; // **********
var thisLink;

allLinks=xpath('//a[@href]')

thisNode=allLinks.iterateNext(); // **********
while (thisNode) { // ***********
    
    thisLink=thisNode.href; // *************
    GM_log('current link =' + thisLink);
    newLink=thisLink.replace(/^https:/,'http');
    GM_log('new link = '+ newLink);
    thisNode=allLinks.iterateNext(); // ************
}


More information about the Greasemonkey mailing list