Archive for the ‘Userscripts’ Category

Javascript: Function.prototype.toString returns an optimized version of the function

June 17, 2007

I was working an a user script for Gmail (I’ll link to it when it’s done), and could not understand why some code didn’t run on a specific page.

One of my tricks of seeing a Javascript function body when the function is dynamically added is to call the function toString() (e.g. in Firebug console). Regretfully, Firebug doesn’t tab-complete functions’ methods, so I have to manually type “toString()”. (I opened issue 959 for this.)

However, in the code I wrote, I accidentally wrote something like:

var count = getCount();
if(count == 0)
    count == 1;

Note the bug in the last line, where the comparison operator (==) is used instead of the assignment operator (=). Surely, my mistake. However, when I look at the text that was returned from the call to Function.prototype.toString() for that function, I saw it is:

var count = getCount();
if (count == 0) {
}

So the Javascript engine noticed that nothing really useful is going on in the body of that if, and simply removed it, and that is reflected in what Function.prototype.toString returns. I didn’t knew that.

PS

Naturally tools like JSLint warn about this problem:

Expected an assignment or function call and instead saw an expression.

count == 1;

But this is a minor user script, and I never considered passing such scripts through JSLint. I might consider starting doing it for my scripts…

Flix.co.il on Firefox

June 6, 2007

Flix.co.il is a an Israeli video sharing site, and recently I noticed that when someone links to a movie over there, my Firefox fails to display it, and I have to revert to IE to display. That doesn’t happen that much for me to install the IETab extension. but today, I decided to check what’s going on, and see if a user script can be written to overcome it.

One of the strangest things was that I saw no complaint about it in the Israeli Blogosphere, and I thought that if there was a problem with Firefox support on that site, bloggers would have rant about it, and someone would even write a user script to solve it.

However, when I checked it I found out that the site uses Javascript libraries from plaxo.com when the “send movie to a friend” feature is used, in order to import their address book. Normally, this should not be a problem, but SmartFilter defines Plaxo.com as “Social Networking” and such sites are blocked by our IT people as non-job related site, so when the browser requests it, it gets an error HTML, and if it tries to treat the returned page as JavaScript code, errors occurs. For some reason I didn’t quite understand, IE can cope with that JavaScript error, but Firefox stop execution the code in the page, so the movie Flash <object> and <embed> tags are not created, and the movie is not displayed.

After that was found, it was easy to write a user script that creates that movie Flash <embed> tag if the page failed to do it.

Checking it some more, I saw that if you refresh the page several times the movie start playing. This might be related to the fact that the plaxo.com libraries are loaded in an <iframe>, and the load order might change.

Greasemonkey wish: Auto-update user scripts

May 1, 2007

I’m the kind of guy who likes to know when a new version of a software I use is released, and update whenever I can (I guess this is why I installed Office 2007 when it was in Beta). This is why I use Update Notifier Firefox extension, which automatically checks for Firefox add-ons when Firefox starts.

I think Greasemonkey should evolve to support such mechanism for user scripts – each user script should be able to mention its current version and its URL as part of its metadata and then Greasemonkey can periodically check if the user script is updated. As it is now, I might find a script on userscripts.org and install it, but I won’t know that the script was later improved by its author. I think this would be great.

update: while wondering around userscripts.org, I found this script which seems to do what I want. I’ll look into it tomorrow.

Annoying meebo message on Firefox close or restart

April 28, 2007

Every once in a while I need to restart Firefox (e.g. to update an extension, or to remove Firefox memory consumption), or I need to restart my machine and close Firefox. Since I always have a meebo tab open, when I close Firefox I get this annoying message from meebo (which uses the onbeforeunload event):

If you leave, your meebo IM sessions will not be preserved!

Today I realized that this message is almost never useful – I never closes the meebo tab by mistake – I only do it when I want to close (or restart) the browser, and then I realized a quick user script can solve the problem. I checked into Meebo code, and so that the string is a member of the meebolang object that holds language-dependent strings. This string (meebolang.navigateAway) is returned in the onbeforeunload event, so to hide it, I can simply change that member.

A few trials taught me that in Firefox, the onbeforeunload event must return undefined (and not null or false) for the warning unload message to disappear. Once that was achieved, implementing the required user script was easy.

Post-Subscription Disorder

April 25, 2007

On regular basis, I add new items to two sites on a regular basis – Bloglines (my RSS feed reader) and del.icio.us (my bookmark keeper).

When I subscribe to a new blog in Bloglines (see below how I do it), Bloglines takes me to my feeds page at Bloglines, and after I add a new URL to del.icio.us (using the Post to del.icio.us bookmarklet), del.icio.us takes me back to the site I just added.

I would like it to be the other way around:

When I subscribe to a site/blog, this is probably after I took a quick glance at it, and decided its interesting, and after I subscribed, I would probably want to continue reading it (or close the window). Besides, my first open tab is the Bloglines tab, and I always keep it open, so I don’t need another Bloglines tab.

On the other hand, when I add a URL to del.icio.us, that’s usually after I’m more or less done with it for now, and wanted to file it for later use. Since I don’t usually have a del.icio.us tab open, I would like to be taken to my del.icio.us main page, where I can do various actions like seeing what other bookmarked pages I have that are related to the new page I just added, or seeing how many other people bookmarked this page, or taking a look at the pages I added recently, to see if any of them might be of interest now, e.g. because I wanted to e-mail it to someone but didn’t do it yet for some reason.

Once I have time, I might look into these sites and see if I can write a user script that can change this annoying post-subscription behavior.

PS 1:

I used to subscribe to new blogs in Bloglines using the Subscribe button in Google Toolbar, but as I’m writing this post, I’ve realized that when I do it, it always opens the subscribe window in a new tab, which is not always what I want. I think I’ll switch to using the RSS icon next to the down arrow of Firefox’s address combo-box, where left click open the subscribe window in the same tab, and middle click opens it in a new tab.

PS 2:

Why Bloglines? I tried Google Reader in its first version, and didn’t really like it, so I reverted back to Bloglines, which I really liked. When the second version of Google Reader was released, and everyone said it’s awesome, I tried to use it, but it remembered all my old subscriptions from my previous trial, and the option to delete them all and start over didn’t really work. While writing this post I tried it again, and this time it worked, so I might give it another try some day. In fact, I do like the frames structure of Bloglines, so Google will have to really impress me for me to make the move.

UserScripts.org

January 22, 2006

I just realized that a neat way to publish my GreaseMonkey userscripts would be to store them in userscripts.org.

This way, I don’t have to keep them in my own site, and I can just mention them here and link to them.

The first example is the simple userscript I wrote to hide the sponsor in SitePoint articles.
If you’re interested, you can always lookup my profile.

User script to make Joel on Software blog posts print better

November 1, 2005

Joel on Software blog sometimes links to Joel articles and the articles have a “Printer Friendly Version” link below the subtitle. But the posts themselves, which are sometimes quite long, don’t have a “printer friendly version”. At least I didn’t find one.
So I created a Greasemonkey user script to hide all the irrelevant stuff and leave only the post itself, so it takes less paper and ink to print.

This is of course only for personal use, as I remove the copyright string.
This is subject to change as Joel is currently redesigning his site.

Paul Graham for Print user script

September 7, 2005

I finally set down to write the Paul Graham for Print user script.
I was always annoyed by the formatting of his articles, which might make it easier to read on screen, but take a lot of space when printed.
This script hides the unrelated links and trailing empty lines from the article, and makes the text occupy 100% of the page width.

Enjoy.