Gmail2 Saved Searches by N-Dream

By N-Dream Last update Jul 2, 2008 — Installed 1,215 times.

Archived Comments (locked)

in
Subscribe to Archived Comments 9 posts, 5 voices



Jesse Andrews Admin

The following is an archive of comments made before threaded discussions was implemented (November 16th, 2008)

 
N-Dream Script's Author

I adopted Edo78 comments, should now work also with new version

 
Edo78 Scriptwright

I'm working on a complete rewrite version of this script with some new features. Stay tuned.

 
Saibot Scriptwright

Nicely done, Edo78.

Any way to reduce the font size and/or position the box above the labels box?

 
Edo78 Scriptwright

Yeah, I've done it !!!

// Copyright 2008 by N-Dream.com (Andrin von Rechenberg)
//
// ==UserScript==
// @name          Gmail2 Saved Searches
// @namespace     http://mail.google.com/
// @description   Adds a Saved Search nav box to Gmail
// @include       http://mail.google.com/*
// @include       https://mail.google.com/*
// ==/UserScript==

window.addEventListener('load', function() {
  if (unsafeWindow.gmonkey) {
    unsafeWindow.gmonkey.load('1.0', init)
  }
}, true);

function hostId() {
  var id = "s";
  var l = top.location.href;
  var n = l.indexOf("mail.google.com/a/");
  if (n != -1) {
    l = l.substr(n + 18);
    l = l.substr(0, l.indexOf("/"));
    id += l;
  }
  return id;
}

function getData() {
  return GM_getValue(hostId(), "to me directly|#search/to%3Ame\n");
}

function addSearchElement(gmail, module, text, search) {
  var div = gmail.getNavPaneElement().ownerDocument.createElement('div');
  with(div.style) {
    background = 'white';
    color = 'rgb(125,108,58)';
    padding = '4px';
  }

  // Creating delete button
  var del = document.createElement('span');
  with(del.style) {
    cursor = 'pointer';
  }
  del.appendChild(document.createTextNode('x'));
  var newStyle = del.style.cssText + 'float:right;'
  del.setAttribute('style', newStyle);

  var h = hostId();
  del.addEventListener('click', function(evt) {
    window.setTimeout(function() {
      var parts = getData();
      parts = parts.split("\n");
      newparts = "";
      for (var i = 0; i < parts.length; ++i) {
        if (parts[i].indexOf(text + "|") != 0) {
          newparts += parts[i] + "\n";
        }
      }
      GM_setValue(h, newparts);
      redraw(gmail, module);
    }, 0);
  }, false);

  
  // Creating label
  var label = document.createElement('span');
  label.appendChild(document.createTextNode(text));
  with(label.style) {
    cursor = 'pointer';
    textDecoration = 'underline';
  }

  label.addEventListener('click', function(evt) {
    top.location.hash = search;
  }, false);

  // Appending children
  div.appendChild(del);
  div.appendChild(label);
  module.getContentElement().appendChild(div);
}

function addFooterElement(gmail, module) {
  var div = gmail.getNavPaneElement().ownerDocument.createElement('div');
  with(div.style) {
    background = 'white';
    color = 'rgb(125,108,58)';
    padding = '4px';
    fontSize = '80%';
  }

  // Creating help button
  var help = document.createElement('span');
  with(help.style) {
    cursor = 'pointer';
  }
  help.appendChild(document.createTextNode('?'));
  var newStyle = help.style.cssText + 'float:right;'
  help.setAttribute('style', newStyle);
  help.addEventListener('click', function(evt) {
    window.location.href = "http://userscripts.org/scripts/show/20214";
  }, false);

  // Creating Save Button
  var save = document.createElement('span');
  save.appendChild(document.createTextNode("Save this search..."));
  with(save.style) {
    cursor = 'pointer';
    textDecoration = 'underline';
  }
  var h = hostId();
  save.addEventListener('click', function(evt) {
    window.setTimeout(function() {
      var name = "";
      var current = getData();
      while (name == "" || 
            name.indexOf("|") != -1 ||
            current.indexOf(name + "|") != -1) {
        name = prompt("Please enter a name for this search", name);
        if (name == null) {
          return;
        }
        if (name.indexOf("|") != -1) {
          alert("| is not allowed.")
        }
        if (current.indexOf(name + "|") != -1) {
          alert("'" + name + "' is already taken.")
        }
      }
      GM_setValue(h, current + name + "|" + top.location.hash + "\n");
      redraw(gmail, module);
    }, 0);
  }, false);

  // Appending children
  div.appendChild(help);
  div.appendChild(save);
  module.getContentElement().appendChild(div);
}

function redraw(gmail, module) {
  var m = module.getContentElement();
  while (m.childNodes.length) {
    m.removeChild(m.firstChild);
  }
  window.setTimeout(function() {
    var parts = getData();
    parts = parts.split("\n").sort();
    for (var i = 0; i < parts.length; ++i) {
      var n = parts[i].indexOf("|");
      if (n != -1) {
        addSearchElement(gmail, module, parts[i].substr(0, n),
          parts[i].substr(n + 1));
      }
    }
    addFooterElement(gmail, module);
  }, 0);
}

function init(gmail) {
  var module = gmail.addNavModule('Searches', '', 'rgb(251,216,117)');
  redraw(gmail, module);
}

Maybe it can be better than this but it's just my second greasemonkey script ...

 
Edo78 Scriptwright

I'm trying to fix this script but it has a lot of problem (I think because of gmail/greasemonkey update) one of that I can't yet fix completely :-(

The biggest problem is related to a greasemonkey update and is described here http://wiki.greasespot.net/0.7.20080121.0_compa...

I'm not to good in javascript programming (not yet ;-) at least) and change the getData function into

function getData() {
  var value;
  window.setTimeout(function() {
    value = GM_getValue(hostId(), "to me directly|#search/to%3Ame\n");
  }, 0);
  while(typeof(value)=="undefined") {
    /* if you know how to use a "sleep" like function you can safely remove this crappy alert*/
    alert("this alert is useful only to wait for the setTimeou to retrive the searches");
  }
  return value;
}

that crappy alert is really annoying but it's the only method I find in this short time (I'm not a javascript programmer and I'm at work).

The second problem is a little one, probably coming from a gmail update ... every window.location must be changed in top.location because gmail load itself in a frame.

Hoping this can help.
Meanwhile I try to solve the "alert" problem ...

 
Edo78 Scriptwright

I have the same problem described by tcustomgolf ...
I try it in a clean FF profile only with greasemonkey and your script :-(

 
tcustomgolf User

Not working for me...I get the arrow and searches bar, but when I try to open the bar to expand, nothing shows....and I disable all other GM scripts..

 
Saibot Scriptwright

FYI, there is a conflict between this script and the TopLabels script. When TopLabels is installed, the saved searches do not load properly.

For me, Saved Searches is preferable to TopLabels, but it'd be nice to be able to use both.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel