Adding Persistent Searches to Gmail #

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:

  • Persistent Searches ScreenshotIf 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.

Toggle Font ScreenshotThe 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 XMLHttpRequest object and use it to fetch the search results for each saved search. The response contains in it the total number of messages that matched the query. It would've been nice to use the DOM (and then a JavaScript eval()) to parse it, but this turned out to be more difficult than expected (XMLHttpRequest only provides a parsed DOM for XML documents).

Rather than specifying all of the CSS properties inline or via the JavaScript 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.

45 Comments

This is great man. And how come you work for google. It's my dream.

I have a very very small correction for your script. Just to say I'll correct someone from google once.

When you specify the class for the div where you put the image for toggle font and the text itself, you specify the class as 'al'. When it should be specified as 'ar'. Couse of this the link gets a little bit above where it should be.

Just change the line 544 as follow and it should be fine.

toggleFontLink.className = "ar";

Great script thanks.
I couldn't get this to work at all. It installs ok, but nothing happens - there is no Searches panel. I can see three possible reasons for this:

1: I'm using FireFox 1.5. I don't know if it's been tested on 1.5 or not, or whether this would cause any issues

2: Google may have changed the name of the labels panel that your script is looking for. They've done it before and it would very well explain what I'm seeing

3: I'm in the UK, so Google have put up a slightly different page. As far as I know, the only real difference is that they are thinking of a domain change to googlemail.co.uk because there's already a gmail.co.uk which has confused users of both systems as well as invited a lawsuit.

Personally I reckon that they've just changed the name of that element. Is anyone else having the same trouble?
I had the same trouble, it's either because of Firefox 1.5 or GreaseMonkey 0.6.4 (or both).
Anyway, I *kinda* fixed it: http://home.arcor.de/deckman/gmailtweaks.user.js

It's not beautiful or anything, but it's working for me. I couldn't get the result count to work though. (stupid XPCNativeWrapper)
super script thankyou.
If I change name to name with cyrillic symbols - its displays incorrcectly
@Alex: That works much better, cheers. Though for some reason it shows 'false' whenever I click a search, then switches to the search results. Still useable, just seems strange. A debugging message, perhaps?
Hello -- thanks for this script, I love it! I was wondering if there was a way to edit where the searches box appears? Right now it appears above "contacts," but I'd prefer it to appear below 'invite a friend' as it appears in the pic on this page.

thanks again, this is really great.
Greg, if you click "Edit Searches" there's a set of radio buttons that allow you to control where the box appears.
Mihai - what does the bottom line in the Edit screen do? ("Recent Searches: Position _Above _Below - Number __")

Thanks for updating everything... I'm now finally using Fx1.5 since all of my preferred customizations are available!
Brad, that's a later addition to the script. In addition to letting you save searches, it can also see the most recent ones in case you want to run them again (saved searches are excluded). Those preferences let you control where this list appears and how many searches are saved.
this rocks! i was frustrated today for the nth time that i couldn't get a list of unread messages with a single click. thanks!
How do you edit a Greasemonkey script to make minor changes to it?
There is a minor typo in your Script..., and for some reason, I am having trouble editing a search with the criteria "find:name of person"... The typo reads "Seaches" Location, instead of what it should be, "Searches" location.
wow another very cool mod for gmail. great work!!!
I love this script, and blogged about it here:

http://kay.smoljak.com/archives/?persistent-searches-in-gmail



But as of this morning, it's no longer working. I'm assuming that Google have changed something in the code that's stopping the script from running. It's the only Greasemonkey script that's enabled in my browser (FF 1.5.8). I'm totally stuck without my saved searches!



Is anyone else having the same problem?
This looks like great script. But it doesn't work for me :( I have FF 2.0 and Greasemonkey 0.6.6.
I really waiting for fixing bugs
This script is the fucking balls. THANKS.
Luke Baker modified your original script to use a dummy contact instead of a cookie to store the searches (something your original blog posting said you'd like to do).

http://lukebaker.org/archives/2005/05/25/persistent-persistent-searches/

Couldn't you use his code specific to that to update your most recent one to do the same? (I love both, but haven't figured how to bring the searches in my contact list from his, into your newest script).
Hi, Mihai:

Thanks. This is great. One question: in the "Muted but unread" search, what does the label "^g" mean?
Thanks.

Len
Len, that's the system (hidden) label that Gmail applies to muted conversations (see http://mail.google.com/support/bin/answer.py?answer=47787 for more information). Searching for is:muted is the equivalent.
Great script, thanks.

Seems you removed the result count?

I prefer to have the recent searches sorted with the most recent one on top. Perhaps you'd consider adding an option for this?

Until then, this is a unidiff to reverse the sorting: http://pastie.textmate.org/48861
is there possibility to create search for non-labeled messages?
Great script. This has made gmail a bit easier to use, definitely.

Just one question that I can't seem to figure out. How do I create a search to look for all unread messages in my Sent Mail?
I prefer not to have the toggle font link. Could you either make a way to hide it or make it a separate script so users can choose whether they want it or not. Thanks!
I had an idea (but not the skills to implement it) about the saved searches.

Could you make the searchbox into a list instead with greasemonkey? then you wouldn't need the links inte the sidebar which take up space.

Just a thought...
I liked this script very much. However, I used it through "Better Gmail" from Lifehacker.com.

Now that google has updated their gmail, lifehacker was forced to upgrade "Better Gmail" to "Better Gmail 2"... however, your script... one of my favorites, was gone. I loved it. Please please update it to work with better gmail, and the updated (newer) version of gmail!

Thank you for your hard work!
Saved searches is something I've wanted forever, I'm so glad it's been implemented. Do you have any plans to update this for the new version of gmail? Previously I had been using the old version because this script is worth not upgrading for, but the new colored labels are a killer app enough that I find myself switching back and forth often now. I'd give anything for a new version, until google realizes that this is a necessary feature and implements it.
Great script.

Just to let you know that it doesn't work anymore since latest Gmail update. One can still use it by choosing to use the older version though.
Hi there!

Thanks for this great script...
I am sure you have already noticed but it doesn't work with the new Gmail layout (I call it the "glossy" Gmail)... it still works if I select "Older version" from the top right of the page. It still works on my Gmail hosted domain which has the old layout. Is there an easy fix for this?

I also wanted to know what "Add Focus" and "Muted but unread" meant.

Again, thanks for this great script! Now I am completely free of desktop clients.
Is it possible to get this to work with GreaseKit in Safari as well as Firefox? Most scripts work in both seamlessly, but for some reason this one doesn't...
this does not seem to be working with the new version of gmail using Google apps
any plans on an update?
i miss having my saved searches available.
Same here! I miss this script!
Please please please convert it to make it work with the new GMail! Thanks!!
Love to have this again in the new interface!
Not only do i miss this script, but I wish there were persistent searches in google reader!
I've never used this script.....but damn I sure could benefit from its usefulness :P
Anyone could, I wish it was updated for the new gmail. :(
Grazi. Io lo usavo nella vecchia versione. Mi piacerebbe averlo per Gmail2. Indispensabile!
I'm using a firefox-plugin called inFormEnter which can copy any text into a form-element, for instance is:unread

it adds a few more clicks but it's not difficult to use
how about for the new gmail interface
for the new gmail interface, use the "quick links" feature from gmail labs.
Ditto the "quick links" feature - make searches savable and clickable from left pane in gmail.
I am here because Google retired their "fixed width font" option, so I am trying to find a way to do this on my own. It appears this script is now obsolete. Anybody have any leads on script that works with current gmail version?
PLEASE get this working again man. i really want to set up saved searches for the different "stars" so that I can have a different folder for each star, instead of one "starred" label......is there a way to do this without your script!?
Anonymous: The "Quick Links" Gmail Lab should let you accomplish that.

Post a Comment