Imagine this: you come across a webpage that says "Just send a message to user@example.com" but to actually send the email, you need to highlight the address and then cut and paste it into the recipient field of a new compose window of your email client of choice.
Obviously life would be easier if it were simply a mailto: link, so you could click on it and have a new message created automatically. How do I build an extension that turns email addresses into clickable mailto: links?
I was originally going to ask if there was an extension to enable similar functionality for unlinked Twitter @username mentions but I thought this email address problem would be a simpler situation.
Use- No, this breaks page's, especially because event listeners are removed and attributes are also replaced.innerHTML
to replace the emailRecursively loop through all nodes:
nodeType
value..nodeType === 1
(element), call the function again (recursion)..nodeType === 3
(text node):exec
method to find one email address. Use the result's.index
property to know the starting position of the email address, andresult[0].length
to know the length of the address.splitText
method cut the text node in three parts.<a>
element.Demo
Not a chrome extension, but it shows how a chrome extension would behave: http://jsfiddle.net/ckw89/
Chrome extension
(the regexp is based on MongoEngine's
EmailField
pattern):script.js
This script will work immediately when used as a Content script, because its only interaction with the page is the DOM. For completeness, here's the contents of the
manifest.json
file:Performance notice
The current script replaces all nodes in the live document. Consider moving the root node (eg
<body>
) to a document fragment before manipulating.