Posted By

wizard04 on 12/12/08


Tagged

javascript sort gmail bookmarklet


Versions (?)

Sort Gmail Messages by Subject


 / Published in: JavaScript
 

This is a very simple bookmarklet that will temporarily sort the visible messages in Gmail by subject. You'll have to run it again each time Gmail refreshes the list.

[Sort Gmail Messages](javascript:(function(){function%20f(r){var%20t=r.getElementsByTagName("td");var%20d,s;var%20i,j;for(i=0;i<t.length;i++){if(t[i].hasAttribute("role")&&t[i].getAttribute("role")=="link"){d=t[i].getElementsByTagName("div");for(j=0;j<d.length;j++){if(d[j].className=="y6"){s=d[j].getElementsByTagName("span");if(s.length)return%20s[0].innerHTML}}}}return%20""}function%20ds(a,b){a=a[0].toLowerCase();b=b[0].toLowerCase();if(a<b)return%20-1;if(a>b)return%201;return%200}function%20sl(m){var%20l=Array.prototype.slice.call(m.getElementsByTagName("tr"),0);if(!l.length)return;var%20b=l[0].parentNode;var%20s=[];for(var%20i=0;i<l.length;i++){s[i]=[f(l[i]),l[i]]}s=s.sort(ds);for(i=0;i<s.length;i++){b.appendChild(s[i][1])}}var%20d=document.getElementById('canvas_frame').contentWindow.document;var%20t=d.getElementsByTagName('table');for(var%20i=0;i<t.length;i++){if(t[i].className=='F%20cf%20zt'){sl(t[i]);break}}})() "Drag this into your bookmarks!")

roryok found a few caveats when the list is sorted:

  1. Shift-clicking to select multiple messages will no longer work.
  2. If you archive or delete messages, they will not disappear.
  1. /****************************************
  2. A very simple bookmarklet that will temporarily sort the *visible* messages in Gmail by subject. You'll have to run it again each time Gmail refreshes the list.
  3.  
  4. There are a few caveats when the list is sorted:
  5.  
  6. 1. Shift-clicking to select multiple messages will no longer work.
  7. 2. If you archive or delete messages, they will not disappear.
  8. /****************************************/
  9.  
  10. javascript:(function(){function%20f(r){var%20t=r.getElementsByTagName("td");var%20d,s;var%20i,j;for(i=0;i<t.length;i++){if(t[i].hasAttribute("role")&&t[i].getAttribute("role")=="link"){d=t[i].getElementsByTagName("div");for(j=0;j<d.length;j++){if(d[j].className=="y6"){s=d[j].getElementsByTagName("span");if(s.length)return%20s[0].innerHTML}}}}return%20""}function%20ds(a,b){a=a[0].toLowerCase();b=b[0].toLowerCase();if(a<b)return%20-1;if(a>b)return%201;return%200}function%20sl(m){var%20l=Array.prototype.slice.call(m.getElementsByTagName("tr"),0);if(!l.length)return;var%20b=l[0].parentNode;var%20s=[];for(var%20i=0;i<l.length;i++){s[i]=[f(l[i]),l[i]]}s=s.sort(ds);for(i=0;i<s.length;i++){b.appendChild(s[i][1])}}var%20d=document.getElementById('canvas_frame').contentWindow.document;var%20t=d.getElementsByTagName('table');for(var%20i=0;i<t.length;i++){if(t[i].className=='F%20cf%20zt'){sl(t[i]);break}}})()
  11.  
  12.  
  13. //Expanded:
  14.  
  15. javascript:(function(){
  16.  
  17. function findTitle(r)
  18. {
  19. var td = r.getElementsByTagName("td");
  20. var div, span;
  21. var i, j;
  22. for(i=0; i<td.length; i++)
  23. {
  24. if(td[i].hasAttribute("role") && td[i].getAttribute("role") == "link")
  25. {
  26. div = td[i].getElementsByTagName("div");
  27. for(j=0; j<div.length; j++)
  28. {
  29. if(div[j].className == "y6")
  30. {
  31. span = div[j].getElementsByTagName("span");
  32. if(span.length) return span[0].innerHTML;
  33. }
  34. }
  35. }
  36. }
  37. return "";
  38. }
  39. function doSort(a,b)
  40. {
  41. a = a[0].toLowerCase();
  42. b = b[0].toLowerCase();
  43. if(a<b) return -1;
  44. if(a>b) return 1;
  45. return 0;
  46. }
  47. function sortList(m)
  48. {
  49. var list = Array.prototype.slice.call(m.getElementsByTagName("tr"), 0);
  50. if(!list.length) return;
  51. var tbody = list[0].parentNode;
  52. var sortList = [];
  53. for(var i=0; i<list.length; i++)
  54. {
  55. sortList[i] = [findTitle(list[i]),list[i]];
  56. }
  57. sortList = sortList.sort(doSort);
  58. for(i=0; i<sortList.length; i++)
  59. {
  60. tbody.appendChild(sortList[i][1]);
  61. }
  62. }
  63.  
  64. var doc = document.getElementById('canvas_frame').contentWindow.document;
  65. var tables = doc.getElementsByTagName('table');
  66. for(var i=0; i<tables.length; i++)
  67. {
  68. if(tables[i].className=='F cf zt')
  69. {
  70. sortList(tables[i]);
  71. break;
  72. }
  73. }
  74.  
  75. })()

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on July 9, 2009

Updated

Posted By: wafflestomper on August 26, 2009

I like the idea behind this and I'd like to get it to work. I assumed that I could drag the link at the top of the page to my bookmark bar and click it when I'm on my gmail page, although that didn't work. What do I need to do to get this working on my gmail account?

Posted By: wafflestomper on August 26, 2009

I like the idea behind this and I'd like to get it to work. I assumed that I could drag the link at the top of the page to my bookmark bar and click it when I'm on my gmail page, although that didn't work. What do I need to do to get this working on my gmail account?

Posted By: wafflestomper on August 26, 2009

I like the idea behind this and I'd like to get it to work. I assumed that I could drag the link at the top of the page to my bookmark bar and click it when I'm on my gmail page, although that didn't work. What do I need to do to get this working on my gmail account?

Posted By: wizard04 on August 26, 2009

Well, Google changes the Gmail page every now and then. I'll try to update it when I get a chance.

Posted By: wizard04 on August 27, 2009

Okay, the code was correct; I just forgot to change the link. It works now.

Posted By: roryok on October 27, 2009

I had to change one line of code in this to get it working...

changed

document.getElementById('canvas_frame').contentWindow.document.getElementById(":op");

to

document.getElementById(":on");

Posted By: roryok on October 27, 2009

sorry, thats wrong it should just change ":op" to ":on"

UPDATE:

change document.getElementById('canvasframe').contentWindow.document.getElementById(":op"); to document.getElementById('canvasframe').contentWindow.document.getElementById(":on");

Posted By: wizard04 on October 27, 2009

Thanks, roryok. It's still working for me, but I've had that problem before - sometimes an element gets renamed just for the hell of it and I haven't figured out why. I'll take a look at it when I get a chance.

Posted By: roryok on October 27, 2009

I've also just modified this to sort by sender, http://snipplr.com/view/21960/sort-gmail-messages-by-sender/

(my first snipplr post! yay!)

Posted By: wizard04 on October 27, 2009

Huh. For me, :op is still correct; :on is the cell containing the first checkbox. Out of curiosity, which theme are you using?

Posted By: roryok on October 27, 2009

i'm using the 'classic' theme... I wonder if it has something to do with labs features?

Posted By: wizard04 on October 28, 2009

It is the labs! I'll figure out which one it is...

Posted By: wizard04 on October 28, 2009

Out of the labs that I have enabled, the Quick Links one is causing the problem.

Posted By: wizard04 on October 28, 2009

Okay, now it finds the table by its class. See if it works for you.

Posted By: novida on March 27, 2010

I have highlighted the contents of line 10 and pasted it my Safari browser but I can't get my Gmail to sort by subject. Is there something I missed?

Posted By: wizard04 on March 29, 2010

It still works in Safari 3.2 Win on my computer. You could try logging in with labs turned off; sometimes those change IDs and stuff so scripts don't work. https://mail.google.com/mail/?labs=0

Posted By: Jimadine on January 22, 2014

Anyone have a working version of this? I get "Uncaught TypeError: Cannot read property 'contentWindow' of null" console error in Chrome, and the messages don't sort. Thanks.

Posted By: Jimadine on January 27, 2014

I got this working by removing line 64 and changing line 65 from 'doc' to 'document'. Reason changes needed: it seems the element with ID 'canvas_frame' no longer exists. One thing I've found is that if you sort your messages then click on a message to go into it, the message shown isn't the one you clicked on! Also if you drag and drop a message say from your Inbox into a label, the message doesn't immediately disappear as it would normally; a manual page refresh is required, though at least the correct message is moved. I was thinking about making this into a Greasemonkey script, but given the oddities I've described above I don't think I'll bother.

You need to login to post a comment.