JavaScript (via Greasemonkey) failing to set “title” attributes on tags


I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey. It goes through a page, looks for <a> tags whose href points to, and adds a "title" attribute that identifies the true destination of the link. Much of the important code comes from an older (unsupported) Greasemonkey script that quits working when the inner component that held the XPath implementation changed. My script:

(function() {
    var providers = new Array();
    providers[''] = function(link, fragment) {
        // This is mostly taken from the (broken due to XPath component
        // issues) tinyurl_popup_preview script.
        link.title = "Loading...";
                method: 'GET',
                url: '' + fragment,
                onload: function(res) {
                    var re = res.responseText.match("<blockquote><b>(.+)</b></blockquote>");
                    if (re)
                        link.title = re[1].replace(/\<br \/\>/g, "").replace(/&amp;/g, "&");
                        link.title = "Parsing failed...";
                onerror: function() {
                    link.title = "Connection failed...";
    var uriPattern = /(tinyurl\.com)\/([a-zA-Z0-9]+)/;
    var aTags = document.getElementsByTagName("a");

    for (i = 0; i < aTags.length; i++)
        var data = aTags[i].href.match(uriPattern);
        if (data != null && data.length > 1 && data[2] != "preview")
            var source = data[1];
            var fragment = data[2];
            var link = aTags[i];
            aTags[i].addEventListener("mouseover", function() {
                if (link.title == "")
                    (providers[source])(link, fragment);
            }, false);

(The reason the "providers" associative array is set up the way it is, is so that I can expand this to cover other URL-shortening services as well.)

I have verified that all the various branches of code are being reached correctly, in cases where the link being examined does and does not match the pattern. What isn't happening, is any change to the "title" attribute of the anchor tags. I've watched this via Firebug, thrown alert() calls in left and right, and it just never changes. In a previous iteration all expressions of the form:

link.title = "...";

had originally been:

link.setAttribute("title", "...");

That didn't work, either. I'm no newbie to JavaScript OR Greasemonkey, but this one has me stumped!

Best Solution

Try replacing the body of the if with this code instead.

        aTags[i].addEventListener("mouseover", (function(source, fragment)
            return function()
                if (this.title == "")
                    (providers[source])(this, fragment);
        })(data[1], data[2]), false) ; 

Note after the loop is complete do aTags = null;.

Your problem is that an if statement block is not a true scope, anything var'd will belong to the outer functions scope. Hence your inner function that you are providing as an event handler would use the source, link and fragment of the last pass. Additionaly by maintaining references to DOM object you would have a memory leak due to circular references.

The above approach create a new scope on each pass via a function call so each source and fragment is in its own scope. It also uses the fact that a function called as an event listener has the this property pointing at the element to which it is attached, hence avoiding a circular reference containing a DOM element.