Inject Custom CSS and javascript into WebView

删除回忆录丶 提交于 2019-12-22 01:29:46

问题


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.


回答1:


The best way would be the one delineated by Rob Keniger, with this answer: https://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)


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

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];
}


来源:https://stackoverflow.com/questions/11730986/inject-custom-css-and-javascript-into-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!