Archive for November, 2008

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.

Two weeks ago we went to Mount Huang (Huangshan in Chinese) and spent 3 days there (2 days on the mountains). The trip was wonderful and Mount Huang is really impressive.

During the shot trip we saw all kinds of beautiful sceneries like sea of clouds, sunrise, sunset, various strangely-shaped pines, peculiarly-shaped granite peaks, etc.

We walked on the dangerous roads hanging on the middle of the cliffs, which was very exciting. You’d better not look downwards, and pray the road is solid enough.

绝壁栈道

Below is the carved steps to Celestial Peak. It’s like a ladder stretching into the fog. It’s almost vertical but don’t be too frightened, as there’re ropes beside it for travellers to grab.

从北面上天都峰的路

Sea of clouds appears in the morning. As you can see in the pictures, our standing ground is higher than the clouds. The picture on the right is a scenery called “monkey watching the sea”.

云海 猴子观海

If you’re interested, view more photos here: http://www.flickr.com/photos/qingbo/sets/72157609353527815/