Django prepopulated_fields not working?

Note – Just skip to the end of this article if you’re eager to find out the reason.

Yesterday I found that Django even had a special field SlugField for post slugs. I’m just starting with Django so there’s a lot I don’t know yet. It’s cool that they also designed a “prepopulated_fields” property in the admin module.

class ArticleAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("title",)}

Continue reading Django prepopulated_fields not working?

Writing Greasemonkey User Scripts for Firefox and Chrome

Greasemonkey was initially only a Firefox extention but due to its popularity it’s quickly adopted by major browsers. Since Opera takes a very small market share, here on the topic of cross browser user scripts, I’m only going to talk about Chrome and Firefox. As for Internet Explorer, I just don’t like it.

Now Chrome doesn’t need an extension to enable user scripts. It had the functionality built in. But the way it handles the scripts is quite different from Firefox’s. At first when I wanted to test if my Greasemonkey script worked in Chrome, I tried to figure out where it stored the script. That’s the way I always do in Firefox – just right click on a user script and edit, save and refresh to see it in effect right away.

Continue reading Writing Greasemonkey User Scripts for Firefox and Chrome

Google Reader and Readability, Kindle

Readability has browser extensions for reading current page or sending current web page to Kindle, but I read a lot in Google Reader where many articles are on the same page. It seems that Readability staff considered this feature and did investigate on that. But until now I didn’t find a mechanism for Reader’s native send to feature to work with Readability. So I spent several hours writing this Greasemonkey script, so far very happy with it.

Click here to install. It supports both Chrome and Firefox. After it’s installed, open Google Reader and view any entry, you’ll find Readability’s button on the right of the actions bar below the entry content. Like this:

Google Reader Readability Send To Kindle

If you don’t already know Readability, you should try it. After clicking the button, the article is saved to your reading list in your Readability account, and Readability sends (after some processing to make it “readable”) it to [your name]@free.kindle.com. Then when you connect your Kindle to a WIFI spot, the articles are automatically downloaded.

Basically I utilized the JavaScript from Readability’s publisher tool. Luckily they didn’t obfuscate the code and it indeed has good readability :) Anyway I think Readability should develop a page which can be used in Google Reader’s native send to popup. That would be easy and better than my script, since I have to update it every time Google Reader interface changes or Readability updates their JavaScript library.

A minor issue of readability is that it doesn’t extract the redirected URL so all the articles from blogs using feedburner’s service will seem to be from feedproxy.google.com and in readability’s account you’ll see feedburner’s favicon displaying before most items.

Updated “Google Reader Unread Count in Gmail” Script

Nearly 3 years ago I wrote a Greasemonkey script to show Google Reader’s unread count in Gmail’s top navigation bar. I was quite satisfied at first and shared it on userscripts.org, but later I found that it was too time-consuming for me since I always wanted to monitor the number and click through when it turned “1”. So I disabled it and forgot about it.

But today after cleaning up the spam comments, I found a comment about this script. In fact someone also posted in the userscripts.org forum and even named it as one of the favourates. Thanks dear users! I knew I had to make an update and it turned out to be quite simple.

So go to the script page and install it. Should you have any problem with it, just leave a comment below.

Google Reader API changed a little

Recently I noticed that one of my Greasemonkey script called “Google Reader Unread Count in Gmail” was displaying the wrong count. I was too busy but at least two users urged me to solve the problem so I took a look into the issue, which turned out to be very simple and easy to fix.

In fact Google changed the xml schema of the output so my script was displaying a timestamp. That’s why the count look so strange :)

Now the source code on userscripts.org is updated(here’s the diff). Please update your local version if you’re facing the same problem.

DOMContentLoaded and Greasemonkey

A very common purpose of Greasemonkey scripts is modifying the DOM structure of the document, mostly adding something new.

I’ve written several scripts before and have been doing such modifications in the “load” event handler, as in this script. The problem is that “load” event is fired after all images on the page have been downloaded completely, so users may suffer a great delay to see the changes occur if there’re many images.

jQuery users know that jQuery has a function “ready”. It won’t wait for the images to load. We are writing Greasemonkey scripts, so we only care about Firefox. Firefox has a “DOMContentLoaded” event explained in detail here.

Fired on a Window object when a document’s DOM content is finished loaded, but unlike “load”, does not wait till all images are loaded. Used for example by GreaseMonkey to sneak in to alter pages before they are displayed.

So when writing my last script I tried to rely on this event:

window.addEventListener('DOMContentLoaded', function(e) {
	...
}, false);

But the codes in the handler were not executed.

I was confused and did some searching. Finally I got this page saying that “the code in a Greasemonkey user script gets invoked when the DOMContentLoaded event fires”. That explains all. The “DOMContentLoaded” event was already fired so the codes never had a chance to get executed.

The solution, of course, is pulling the handler codes out of the wrapper. If your script doesn’t really rely on the “load” event, don’t put them into its handler because there may be a obvious delay.