Scripting Gmail part 2


UPDATE: Haha, alas this does not work all the time. Yep. See my next post… sigh.

After some time poking around with Firebug, here’s the updated code that also handles the “reply” text-editing area!

/* gmail seems to cycle between frames 'v1', 'v2' and 'v3'. try all */
var success = false;
var editorNames = [ 'hc_compose', 'ta_compose', 'hc_0', 'ta_0' ];
for (var i=1; i<=3 && !success; i++)
{
    /* if the frame is offscreen, it's not the active composing window */
    /* do this with a DOM element */
    var fe = top.main.document.getElementById('v'+i);
    if (fe && fe.offsetLeft >= 0)
    {
        /* the element name of the edit box could be one of 4 values, depending
           on rich text/plain text, as well as whether replying or composing.
           try them all. */
        var d = top.main.frames["v"+i].document; /* note we have to use oldschool frames[] syntax */
        var ed = null;
        for (var j=0; j<editorNames.length; j++)
        {
            ed = d.getElementById(editorNames[j]);
            if (ed != null)
            {
                break;
            }
        }
 
        if (ed != null)
        {
            /* found it! rich text? or plain? */
            switch (ed.nodeName)
            {
                case "TEXTAREA":
                {
                    /* plain text */
                    /* do something with 'ed' */
                    success = true;
                    break;
                }
 
                case "IFRAME":
                {
                    /* rich text */
                    /* do something with 'ed' */
                    ed.contentDocument.execCommand( /* something something */ );
                    success = true;
                    break;
                }
            }
        }
    }
}

Phew!

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Deeper into the rabbit hole: Gmail scripting part 3!
Scripting Gmail with Bookmarklets

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

[...] « Scripting Gmail part 2 [...]