What's the problem in this script?

24 views
Skip to first unread message

Dhruva Sagar

unread,
Apr 9, 2007, 5:11:29 AM4/9/07
to greasemon...@googlegroups.com
I am making a simple user script to replace any ':)' in the page with a smiley image.
Can anybody please look into this code and tell me where i might be going wrong...The code is not working properly as I want it to work...

Please help!!!

// ==UserScript==
// @name                Smilize
// @author               Dhruva Sagar
// @description        Turn ':)' to smiley image.
// @include *
// ==/UserScript==

(
function() {
    try {
        var smileyRegex = /\:\)/ig;
        var smileElements = document.evaluate(    "//text()[not(ancestor::script) " +
                            "and not(ancestor::style) and " +
                            "contains(.,':)')]",
                            document,
                            null,
                            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                            null);

        for(var i = smileElements.snapshotLength - 1;i >= 0; i--) {   
            var elm = smileElements.snapshotItem(i);
            if(smileyRegex.test(elm.nodeValue )) {
                var elmSpan = document.createElement("span");
                var text = elm.nodeValue;
                elm.parentNode.replaceChild(elmSpan, text);
                urlRegex.lastIndex = 0;
                for(var match = null, lastLastIndex = 0; (match = smileyRegex.exec(text)); ) {
                    elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex, match.index)));
                    var elmSmile = document.createElement("img");
                    elmSmile.setAttribute("src", "data:image/gif,GIF89a%12%00%12%00%E6w%00%FF%E6%23%FF%E6%22%FF%E3%1F%FF%E4%20%F0%B6%00%FF%E4%1F%F7%CB%00%CA%CA%CA%80~y%FC%D9%11%F7%CC%00%FB%D7%0D%FF%E2%1Dh_L%FB%D8%0F%FF%E4!%EC%AB%00%7F%7Dx%F0%B7%00%FE%E0%1B%F6%C9%00%EF%B1%00%A1~%26%E7%9C%00%FA%D3%08%FE%E1%1D%E9%A2%00%89l0%FB%D7%0E%F2%BD%00%F8%CC%00%F7%CA%00%EF%B2%00%FD%DD%17%FD%DF%19%7F~x%89i%2F%D8%94%06%FA%D4%0A%ED%C7%0B%85s0%B7%7F%17%F6%C8%00%E7%B5%05%FE%E2%1E%F6%CA%00%F4%C3%00h%60L%DA%91%05%C4%9E%17%80d0%DD%93%05%DE%96%06%F2%BF%00%83k0%8Ew%2F%F1%CA%0B%DF%99%06%E4%A6%05%88i0%EC%BE%06%EA%B8%05%FB%D9%10iaL%F8%CE%02%FE%E1%1C%FD%DE%18%8F%7B%2F%B4%7D%18jbL%BE%9E%18%FC%DB%13%FA%D2%07%B8%84%17%FC%DB%14%ED%C1%06%9Cq%26%F3%C2%00%FE%E3%1F%FF%E2%1E%EC%AA%00%ED%AE%00%8F%7B0%F3%C1%00%E1%A3%05%B6%83%18%EA%BE%06haL%FE%E2%1D%D8%92%06%C4%A3%17%EB%C0%06%F3%BF%00%8Ao0%81i0%8Ds0%89h0%8Du0%BC%98%18%8Ez0%FF%E7%23%F0%B8%00%F4%C2%00%A7%8E%26%FB%DA%11%FE%E0%1A%FD%DC%16%FE%DF%19%FE%DF%1A%FF%E5!%A4%A4%A4%84%84%84%CB%CB%CBnnn%98%98%98%D6%D6%D6%FF%E5%22%FF%FF%9933%99%FF%FF%FF%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%FF%0BNETSCAPE2.0%03%01%00%00%00!%F9%04%05d%00w%00%2C%00%00%00%00%12%00%12%00%00%07%FF%80w%82%82pnoqqonp%83%8DwsrWcFK%3Cba%2Frs%8E%07%08('%0B%09%09%1CH%1F%2B6%08%07%83s%11g%0Bjl%0C%0C%13!%0E%1E%1D%16%23%9BwrR%0Bkvv%02%03%03%C1%0E%06%12%5DrwpE8!vtt%03%0Fm%0Ftv%18f%3A%0D%85C%0B%13%02uvmm%D8u%19%3E%14%15%1B%86Z%09%0C%E2%D1%01%E7%05G%06%04I%87%5B%F0%03%E3%01%00%EA)Q%40%A0%04%22%2B%1C%82%0C%90%06%90L%40'hZ%80%C8%92(%06%06!v%AA%01%04%00%A0%8D%1D%13.%20%A4Pt%E3%83%03a%E6%02%04h%83%C5%8E%822%1AH%2C%FA%D1%C3%C3%C9%0C%05%0A%B0%10a%07%08%17%083%BA%F1%FA%D2%C1%C0%C9%60%C1%14%D4%88r%01%CC%B2G%11%2C%10%98%A2%C2%80%02%0AM%24%40%B9%C0%24%C2%AE%3B%9D%BCP%A9%40%80%00%08%08%1A%60%C8H%E5%08R%83%0DU%10r%D0%20%B2%A3%81%26G%83%0A%1DJ%B4%C8Q%20%00!%F9%04%05%0F%00w%00%2C%04%00%03%00%09%00%03%00%00%07%14%80wvvw%85%83%82O%02%85w%02ivO%8B%91%22%81%00!%F9%04%05%0F%00w%00%2C%04%00%03%00%09%00%03%00%00%07%15%80wjlw%85!%0Ewkvv%85w%8B%0E%8C%8D%8Dv%81%00!%F9%04%05%0F%00w%00%2C%04%00%03%00%09%00%03%00%00%07%14%80wvvw%85%83%82O%02%85w%02ivO%8B%91%22%81%00%3B");
                    elmSpan.appendChild(elmSmile);
                    lastLastIndex = smileyRegex.lastIndex;
                }
                elmSpan.appendChild(document.createTextNode(text.substring(lastLastIndex)));
                elmSpan.normalize();
            }
        }
    } catch (e) {
        GM_Log(e);
    }
})();

--
Thanks & Regards,
Dhruva Sagar

RodMcguire

unread,
Apr 9, 2007, 10:40:08 AM4/9/07
to greasemonkey-users
n Apr 9, 5:11 am, "Dhruva Sagar" <dhruva.sa...@gmail.com> wrote:
> I am making a simple user script to replace any ':)' in the page with a
> smiley image.
> Can anybody please look into this code and tell me where i might be going
> wrong...The code is not working properly as I want it to work...
>

Have you tested you XPath in XPather? Have you tested the individual
steps in the JavascriptShell or FireBug? See:

http://wiki.greasespot.net/Useful_Tools_for_Script_Writers

RodMcguire

unread,
Apr 9, 2007, 10:45:02 AM4/9/07
to greasemonkey-users
And I forgot to mention, it is no longer necessary to wrap your stuff
in the function stuff. If you do want to contain your code the script
can be something like:

function Main (){ ....}
Main();

Dhruva Sagar

unread,
Apr 9, 2007, 10:48:42 AM4/9/07
to greasemon...@googlegroups.com
Yes I have tested it in Javascript Shell and am trying to make it run in the javascript debugger as well. The Xpath Is working fine, the regular expression test is also working fine though i have updated the XPath expression by :

var smileElements = document.evaluate("//text()[not(ancestor::script) and not(ancestor::style) and contains(.,':)')]",
                            document,
                            null,
                            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
                            null);

and the regular expression by this:
var smileyRegex = /\b(\:\)[^\s+]+)/ig;

But still in the function where i am supposed to go through the entire text words that match the regular expression something is going wrong...

And about the rapping thing, i just like to do that so that i can also make my scripts as bookmarklets nothing else :).

Thanks for trying, i appreciate it.

RodMcguire

unread,
Apr 9, 2007, 11:05:31 AM4/9/07
to greasemonkey-users
On Apr 9, 10:48 am, "Dhruva Sagar" <dhruva.sa...@gmail.com> wrote:
...

> And about the rapping thing, i just like to do that so that i can also make
> my scripts as bookmarklets nothing else :).

this code looks wrong:

elm.parentNode.replaceChild(elmSpan, text);
It should be
elm.parentNode.replaceChild(elmSpan, elm);

In general it would help if you mentioned what errors you get.

Also with regard to wrapping. It can be easier to develop unwrapped,
and then wrap after everything works.

Anthony Lieuallen

unread,
Apr 9, 2007, 11:06:03 AM4/9/07
to greasemon...@googlegroups.com
On 4/9/2007 10:45 AM, RodMcguire wrote:
> If you do want to contain your code the script can be something like:
>
> function Main (){ ....}
> Main();

The OP did this properly, with an anonymous function, called directly.
Your way works, but it's not necessary to actually name the function.

You are correct that it's not necessary, though.

Dhruva Sagar

unread,
Apr 9, 2007, 11:13:44 AM4/9/07
to greasemon...@googlegroups.com
Awesome!! That was abosultely correct. Also my regular expression was not proper.
I have updated it with this simple one now:
var smileyRegex = /(\:\))/ig;

Thanks a million for your help. I am in the learning stage with greasemonkey so your help was really cool.

Anthony Lieuallen

unread,
Apr 9, 2007, 11:35:28 AM4/9/07
to greasemon...@googlegroups.com
On 4/9/2007 5:11 AM, Dhruva Sagar wrote:
> I am making a simple user script to replace any ':)' in the page with a
> smiley image.

There are already a lot of example "linkify" scripts to look at for
examples across the web. Even more, there's a customizable one that
will easily let you specify custom replacements:

http://www.squarefree.com/2005/05/22/autolink/

Now, you're not looking to make <a> tags, so you can't use any of them
directly, but they'll all be very very close. One of the simplest
examples is probably:

http://ftp.iasi.roedu.net/mirrors/mozdev.org/greasemonkey/linkify.user.js#

As to your actual script:

> elm.parentNode.replaceChild(elmSpan, text);

This line fails horribly.
(And the error console reports it improperly. A case that my fix to
this doesn't catch, grr!)

The node "text" is a string. Even if it was the text node, it's not a
child of "elm.parentNode" so you can't operate on it directly.

The XPath here has already returned the text node that contained the
":)" so you don't want to try to drill down further from there.

> urlRegex.lastIndex = 0;

This (urlRegex) doesn't exist anywhere. smileyRegex perhaps?

> } catch (e) {
> GM_Log(e);
> }

There is no GM_Log (there is GM_log, though).

Finally, I'd move the element replace line to the bottom of the loop,
/after/ elmSpan is filled up completely. It's terribly minor, but may
help reduce just a little jitter.

Reply all
Reply to author
Forward
0 new messages