将自定义CSS和javascript注入WebView [英] Inject Custom CSS and javascript into WebView

查看:340
本文介绍了将自定义CSS和javascript注入WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将自定义CSS注入到WebView中,例如更改 background-color: =nofollow> http://www.apple.com/ 。 Javascript也会很好,因为在将来我想控制的javascript。

How can I inject custom CSS into a WebView like changing the background-color: of http://www.apple.com/. Javascript would also be nice because in the future I would like to have control of the javascript.

推荐答案

最好的方法是由Rob Keniger描述,并附上以下答案: http://stackoverflow.com/a/2475623/307881 。这种技术也应该为javascript工作。如果它不工作的蝙蝠尝试在WebView完成加载后修改DOM(见下文)

The best way would be the one delineated by Rob Keniger, with this answer: http://stackoverflow.com/a/2475623/307881. This technique should also work for javascript. If it doesn't work off the bat try modifying the DOM after the WebView has finished loading (see below)

也可以直接评估javascript字符串而不访问DOM。

You can also directly evaluate a javascript string without accessing the DOM.

首先,获取您的javascript字符串,然后,当WebView完成加载其内容时,使用委托方法 WebFrameLoadDelegate

First, get your javascript string, then, when your WebView finishes loading its content, use the delegate method of WebFrameLoadDelegate:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
    [sender stringByEvaluatingJavaScriptFromString:jsStringToInject];
}

这篇关于将自定义CSS和javascript注入WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆