crosseds crosseds - 23 days ago 8
Javascript Question

Chrome extension get email from page

So the idea is to make chrome extension that search for email address in web email client and then show info about customer from database.

What I managed to find was to get current tab html source with this:
Getting the source HTML of the current page from chrome extension

Then use split, regex or something to get an email and just call to some php script which would parse info. Everything would be great but we're using "Squirrel Mail" which uses iframes so obviously all I get is some headers and iframe links.

Could anyone point me to the right direction? Maybe it's possible to get tabs text rather than html source? Never had any experience with chrome extensions.. And any help to make it faster would be great. I obviously know certain location of the email that I want to get.

Casraf almost sorted it I'm just stuck with getting frame in this certain situation without since it doesn't have an id selector.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head>
<meta name="robots" content="noindex,nofollow">
<title>SquirrelMail</title>
</head><frameset cols="150, *" id="fs1">
<frame src="left_main.php" name="left" frameborder="1">
<frame src="right_main.php" name="right" frameborder="1">
</frameset>
</html>

Answer

Let's say your iframe has a selector you can use.

var iframe = document.querySelector('#iframe'),
    doc = iframe.contentDocument || iframe.contentWindow.document;

Then you can use JS to find elements in the iframe's page and fetch content from there. For example, if in the iframe's HTML you have a <span class="my-email-selector">email@address.com</span>, you could do something like this:

var email = doc.querySelector('.my-email-selector').innerText;

Which will now contain the email.

Notice the doc variable is a document type so it should have all the regular document methods (location, querySelectot[All], etc)

Edit: Since you have frames and not iframes, and they do have a name attribute, it's really easy to access them:

var frame_left = window.frames['left'],
    frame_right = window.frames['right'];