selberg.org Home Home

msn_tryothers

My first Firefox extension is a GreaseMonkey script. GreaseMonkey is an extension that lets you create user-level scripts and plug them in… it really trims down the level and skill for writing (and learning!).

I wrote up one called TryOthers for MSN that just puts in a “Try your search on these engines:” on the page. It’s based on the Butler script that does the same to Google. It’s still a big longer and more intimidating than it needs to be, but here’s the useful bit:


        // add other search sites to web search
        addOtherWebSearches: function() {

            var header = document.evaluate("//div[@id='header']/h5″, documen\
t, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
            if (!header) return;

           this.addGlobalStyle(’#tryOthers {margin-bottom:1em;}’);

            // it’d be nice to use document.qf, but MSN uses id= vs name=
            var q = escape(document.forms[0].q.value);
            var other = document.createElement(’div’);
                    other.setAttribute(”id”, “tryOthers”);
            var s = ”;
            s += ‘Try your search on:’;
            s += TryOthersServices._otherWebSearches(q);
            s += ‘
‘; other.innerHTML = s; header.parentNode.insertBefore(other, header.nextSibling); }, }

A couple things to note… first, we add some global CSS style info there. Second, we’re looking for a particular node designated by a particular class — a H5 in a DIV with id=”header. Once we have that, we then stick the TryOthers string in front of it. Simple as that.

The hardest thing about this script is identifying the node you want to alter… it’s really easy if the person writing the page gave you a DIV with an appropriate ID, but usually you are going after something else, so it can take a bit of troubleshooting. And, there’s no real debugger (that I’ve found), so it’s a lot of trial-and-error and logging via the alert() call. Bleah.

Leave a Reply