I am building a Chrome Extension that should be able to pull any email address listed on the background page.
I found that using a RegEx to scan the whole document is a very expensive operation and the website crashes.
Why am I scanning the whole document?
Remember, the extension is working on top of websites I did not build, so I do not know where the developer put the email. It could be on an tag, or a
tag, or in a ... the options are infinite. Therefore, I can't do a simple document.getElementsByTagName() or any similar query.
One way I thought of reducing the operation cost was to identify in which HTML tag the email(s) are in. I thought of using an Xpath and select all nodes with text that contains an '@', since '@' is a character that all emails have... then, I only have to compare those nodes' textContent with my email RegEx to see if there is a match.
Still, when I use a
Xpath = //text()[contains(.,'@')]
or any variant of that expression, I don't only get the nodes with visible text containing an '@', but also the nodes that have hidden information containing @.
Botton line, the operation is still very costly and is crashing the background pages.
I did use this procedure to fetch phone numbers and it works just fine. I imagine it is because scanning for numeric values is not as expensive since 99% of the website content is letters.
Am I thinking correctly about how to approach this? Does anyone have a better alternative?