UIWebView JavaScript 注入
我已经看过足够多的博客了,所以我很确定它是如何编码的......但他们似乎都没有提到 javascript 注入在 Obj-C 代码中的位置?基本上我想做的就是去掉用户可以导航到的所有页面上的标题(或者至少是最初加载的页面......如果这是一个问题,我稍后会担心后续页面。保持简单,愚蠢的 )。这是我的代码:
[self stringByEvaluatingJavaScriptFromString:@"document.getElementById('login').childNodes[1].innerHTML=''"];
我只是很难找到一个可以真正发挥作用的地方。我主要将其归因于没有完全理解 UIWebView。我想我应该把这篇文章发布在这里,然后同时阅读它是如何工作的,也许我自己就能弄清楚。
I've been looking at enough blogs so I'm pretty sure how it's coded... but none of them seem to mention where the javascript injection goes in the Obj-C code? Basically all I want to do is get rid of a header on all the pages the user can navigate to (or at least the page that initially loads... I'll worry about subsequent pages later if it's an issue. keep it simple, stupid ). Here's the code I have:
[self stringByEvaluatingJavaScriptFromString:@"document. getElementById('login').childNodes[1].innerHTML=''"];
I'm just having a hell of a time finding a place where it should actually work. I attribute it mostly to not fully understanding UIWebView. I figured I'd post this here and then in the meantime read up on how that works and maybe figure it out on my own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 javascript 代码应该在整个 HTML 文档(及其所有 DOM 对象)加载后执行。
因此,您应该将
stringByEvaluatingJavascriptFromString
调用粘贴到 UIWebView 的委托方法- (void)webViewDidFinishLoad:(UIWebView *)webView
中(当然,您需要在一个对象中实现此代码将被设置为 UIWebView 的委托,对于任何委托方法以及每次使用委托模式时)Your javascript code should be executed one your whole HTML document has been loaded (and all its DOM objects).
Thus you should paste your
stringByEvaluatingJavascriptFromString
call in the UIWebView's delegate method- (void)webViewDidFinishLoad:(UIWebView *)webView
(of course you need to implement this code in an object that will be set as the delegate of the UIWebView, as for any delegate method and for every time you use the delegate pattern)