Update on 12/23/2005: The script has been updated to be compatible with Firefox 1.5. See this entry for more information.
Persistent searches (a.k.a. smart folders or saved searches) seem to be the feature du jour of email clients. Thunderbird has them, Evolution has them, and Mail.app soon will. On the other hand, Gmail is the web mail app to use. While one doesn't normally think of web apps as having such advanced power user features, it recently occurred to me that it should be possible to add persistent searches to Gmail:
- If you haven't already, install the excellent greasemonkey Firefox extension.
- Open up this user script (in Firefox).
- From the "Tools" menu, select "Install User Script.." and confirm all of the various prompts.
- Go to your Gmail account (some refreshing may be necessary).
- There should now be a "Searches" box on the left size, below the "Labels" and "Invite a friend" ones.
- Clicking on a search executes the saved query. To refresh result counts, click on the refresh icon in the upper right corner.
- Use the "Edit searches" link to customize your persistent searches.
- As a bonus feature, all threads now have a "Toggle font" link which switches the message font to a fixed size one - great for reading source code.
There are some caveats. Saved searches are stored in a cookie. This means that you cannot easily share them between computers. Ideally this could be remedied by storing the searches within Gmail itself (perhaps as a dummy contact or a special filter), but I'm not quite sure how to do that yet. Furthermore, result counts may not be accurate beyond a certain limit (e.g. Gmail itself reports "about 80" results when there are in fact 77). In general, the smaller the result size, the more accurate the search is.
The user script has a pretty straightforward implementation. It looks for the "Labels" box, and if it finds one, it inserts a "Searches" one. As previously mentioned, I store all the searches in a cookie. To actually perform a search, I created a an
eval()) to parse it, but this turned out to be more difficult than expected (
XMLHttpRequest only provides a parsed DOM for XML documents).
style object, an approach which separated appearance from structure was used. Effectively, a style sheet was embedded into the user script and inserted upon initialization. This style sheet was also used for the toggling of the font (the message body always appears to be in a
<div> of class
mb). It also has the advantage of making the script self-contained, since it doesn't depend on an external CSS file. Also for the same goal of encapsulation, the font toggling icon was embedded in the script itself via a data: URL (generated with hixie's tool).
Disclaimer: I happen to work for Google. This script was produced without any internal knowledge of Gmail, and is not endorsed by Google in any way. If you have any problems with it, please only contact me.