AndyM AndyM - 1 month ago 15
Javascript Question

Get gmail message body of an open mail with a chrome extension

For a project I need to get the body of the currently open message and pass that message to a webservice for processing and storing some data of it.

I thaught the best way to do it is to use an extension to grab the message body (and some additional information) and then send that as a webrequest to a service for insertion into the database.

But I have no idea how I should handle this. I followed some tutorials to get me started on chrome plugins but I can't find how to get the body of the message.

I have found that you need content scripts for it, but even then I have no clue.

Can anyone give me a headstart on this?

Answer

If I had to do this, I would try and find out the ID of the DIV that contains the body on the GMAIL client... Using jQuery to grab the HTML or text contents of a DIV is real simple... take a look: http://api.jquery.com/html/

A simple jQuery selector would look something like this:

$("#gmailsBodyDivID").html()

If jQuery is not an option, you can get the DIV using JavaScript's getElementByID (http://www.w3schools.com/jsref/met_doc_getelementbyid.asp)

Finally, as mentioned by DudeOnRock, you should use an Ajax request to your webservice.

The hard part is to know how Gmail names the DIV you're looking for... I just took a quick look and on mine, there was some weird naming...

<div id=":w1" class="ii gt adP adO"><div id=":w2">

I'm not sure what the ":w1" or ":w2" stand for, nor how to use them with a jQuery selector... maybe someone more knowledgeable might shed some light there.

...so I guess you should focus on that first...

If IDs prove hard, you might want to use a jQuery selector by class names...

good luck with that!

Comments