An interesting scenario arises when the following code is executed from www.example.com. It fetches the complete html source code of www.example.com/example.html and displays it in an alert message.
function process(){
url = "http://www.example.com/example.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
alert(xhr.responseText)
}
}
xhr.send();
}
process();
Now, the task at hand is to extract an email address from the html source code with a format like this:
<input type="hidden" id="email2" name="email2" value="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6b3aeb7bba6bab396b3aeb7bba6bab3f8b5b9bb">[email protected]</a>" />
If we were on the webpage itself, a simple command such as:
document.getElementById('email2').value
would suffice. However, given the challenge, how can we efficiently parse the email address into a variable using the variable that contains the entire html source code - xhr.responseText
?