开发者

Can I inject / run Javascript in different-domain frames in UIWebView?

开发者 https://www.devze.com 2023-03-20 20:20 出处:网络
So I can run Javascript in the page that\'s loaded in a UIWebView by usin开发者_运维知识库g -stringByEvaluatingJavaScriptFromString:, but that will only run the Javascript in the top frame in the brow

So I can run Javascript in the page that's loaded in a UIWebView by usin开发者_运维知识库g -stringByEvaluatingJavaScriptFromString:, but that will only run the Javascript in the top frame in the browser. I'm able to navigate from the top frame to other same-domain frames via window.frames, but that doesn't work for different-domain frames. Is there any way to access different-domain frames in UIWebView?

I've tried checking whether the parameter to UIWebViewDelegate's -webViewDidFinishLoad: callback has a different Javascript context, but it looks like even when the callback is called for a frame that has finished loading the Javascript context is still that of the top frame.

Thanks for any assistance.


For security reasons javascript communication between frames is restricted to frames with that load their content from the same host (with the same protocol and port).

So the thing you try to do might just not be possible. However with HTML5 there is a feature called postMessage which allows communication across different hosts. If you have control over the HTML content that you are loading into your web view you might want to check that out.


I realise this was a while ago but postMessage may do what you're after if you can control execution of JS on the child frame. You can use it to effectively pass messages back and forwards, and because you can build a rudimentary trust model around its use potentially even eval things safely.

If you never found a solution to this I'd revisit @joern's suggestion!

0

精彩评论

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