开发者

How to use WebView to detect dynamic html updates?

开发者 https://www.devze.com 2023-03-30 05:33 出处:网络
Trying to monitor and control some projectors, each with a web server built in. The status of the projectors is updated to a page dynamically through javascript.

Trying to monitor and control some projectors, each with a web server built in. The status of the projectors is updated to a page dynamically through javascript.

I managed to retrieve the html and zero in on the status I was looking for using a WebView object. However, when the status changes, I will need to refresh my values, but I can't' seem to find any event that would detect such changes.

For example, when I first access the power on/off status, it's shown as Unknown and won't be updated to on/off until several seconds after the - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame event.

I can setup a timer to poll the values, but I would 开发者_开发知识库prefer to have it trigger by some sort of an event (or observe any value change). Are there such events? I looked in into WebView, WebFrame, but can't seem to find any.

Or is there a way to observe a javascript variable? The values I'm looking for are stored in javascript variables.

Or perhaps any other ways to do this?


If you can find any JS hooks / dom events that are fired when your content changes you can open a URL with a custom scheme (e.g. "refresh://content") from JS and implement the webView:shouldStartLoadWithRequest:navigationType: in which you can check the scheme.

If you can't find any proper events you should really just poll the JS variable values every 5-10 seconds via [webView stringByEvaluatingJavaScriptFromString:@"js_variable_name"]


If you are not in control of the JavaScript I doubt you can raise an event in response to an AJAX call. I guess you'll have to periodically look for changes using NSTimer scheduledTimerWithTimeInterval:. Example, to get the value of varName run the following:

NSString *value = [webView stringByEvaluatingJavaScriptFromString:@"varName"];
0

精彩评论

暂无评论...
验证码 换一张
取 消