I am parsing a website and want to read out one specific HTML part of that website. I am looking for one (and one only) <frame>
tag:
<frame scrolling="auto" ... src=”I need this text">
So I need basically only the text in src="". But how can I do this as simple as possible? So far I tried to analyze the string by:
NSString *html = [self.webview stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
But html stays empty and I wouldn't know how to continue. So how can I make this as simple as possible using my webview?
Something like this might work:
document.addEventListener("DOMContentLoaded", function() {
var allFrames = document.querySelectorAll("frame");
if (allFrames.length) {
var src = allFrames[0].src;
console.log(src);
var request = new XMLHttpRequest();
request.open("GET", "my-app://localhost/?src=" + encodeURIComponent(src));
request.send(null);
} else {
console.log("no frames on the page " + location.href);
}
});
Run this code using stringByEvaluatingJavaScriptFromString from the webViewDidFinishLoad callback, and handle the output data from the script using shouldStartLoadWithRequest callback.
You see the log and debug the script using Safari Web Inspector (for both simulator and device).