html - How parse DOM to get emails Javascript -
i'am building chrome extension parses entire dom/html ,
replace found email(multiple emails) following div:
<div class="email_tmp"> found_email <span>save email</span></div>
example:
<body> <div>some text...</div> <div>text a@a.com text</div> <div>some text...</div> <p>more text</p> <div><div><span>text b@b.com text</span></div></div> <span>last text</span> </body>
replaced to:
<body> <div>some text...</div> <div>text <div class="email_tmp"> a@a.com <span>save email</span></div> text</div> <div>some text...</div> <p>more text</p> <div><div><span>text <div class="email_tmp"> b@b.com <span>save email</span></div> text</span></div></div> <span>last text</span> </body>
how can search , replace found email entire div
, string found_email email too?
want replace found email(s) string, nothing more...
appreciate help.
here total solution looking
html
<div id="main"> sdfsdsdfsdfsdf a@a.com sdfsdfsdfsdfsdfsdf </div>
javascript
var page_content = document.getelementbyid('main').innerhtml; var found_email = "<div class='email_tmp'> found_email <span>save email</span></div>"; //gives array of emails var email = page_content.match(/([a-za-z0-9._-]+@[a-za-z0-9._-]+\.[a-za-z0-9._-]+)/gi); //replaces emails desired content var result = page_content.replace(email, found_email); //replaces changed html 'main' div document.getelementbyid('main').innerhtml = result;
update:
if want replace text without adding class or tags content of html, gets real complicated write vanilla script same. in case highly suggest use this library found perfect solution problem.
its library called findandreplacedomtext
uses inbuilt methods solve purpose. need give find
(what find) , replace
(replacing html) so,
findandreplacedomtext(document.getelementbyid('t'), { find: /([a-za-z0-9._-]+@[a-za-z0-9._-]+\.[a-za-z0-9._-]+)/gi, replace: '<div class='email_tmp'> found_email <span>save email</span></div>' });
you can revert if face problems in implementing library.
also must read article - replacing-text-in-the-dom-solved
Comments
Post a Comment