Android 在webview外部js中注入,执行函数,并引发警报
我想在 webview 中注入外部 javascript 并执行其函数之一 execute()
。完成后会发出警报并且字符串返回到活动
这就是我的做法,但它似乎不起作用(js已经过测试)
view.loadUrl("javascript:(function() { var script=document.createElement('script');script. type='text/javascript';script.src=" + jsFileURL + ";" + "document.getElementsByTagName('head').item(0).appendChild(script);window.HTMLOUT.showHTML(execute()) ;})()");
这就是我实现 HTMLOUT 的方式,而警报在 ChromeClient 中被覆盖
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); browser.setWebViewClient(new mWebViewClient()); browser.setWebChromeClient(new mChromeClient()); browser.loadUrl(“文件:///android_asset/Travian comx2b.htm”);
I want to inject an external javascript in webview and execute one of its functions execute()
. After completion an alert is raised and the string returns to the activity
this is how I do it but it doesn't seem to work (the js is already tested)
view.loadUrl("javascript:(function() { var script=document.createElement('script');script.type='text/javascript';script.src=" + jsFileURL + ";" + "document.getElementsByTagName('head').item(0).appendChild(script);window.HTMLOUT.showHTML(execute());})()");
this is how I implement HTMLOUT, while the alert is overrided in ChromeClient
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); browser.setWebViewClient(new mWebViewClient()); browser.setWebChromeClient(new mChromeClient()); browser.loadUrl("file:///android_asset/Travian comx2b.htm");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,经过多次尝试,我找到了解决方法,但不幸的是不是“解决方案”。我使用了此加载
view.loadUrl("javascript:" + src + "execute(); " + "");
而源src
来自文本文件 < code>script.js 其中包括我的 javascript(函数和普通命令)script.js 的示例(小心行结尾“;”)
远程脚本的一种解决方案,我尚未测试过它是解析远程脚本(例如作为输入流),然后将其作为纯文本包含在内。
Ok after many many attempts I found a workaround, but unfortunately not the "solution". I used this load
view.loadUrl("javascript:" + src + " execute(); " + "");
while the sourcesrc
comes from a text filescript.js
which includes my javascript (both functions and plain commands)a sample of the script.js (be careful on line endings ";")
One resolution for a remote script, which I haven't tested it, is to parse the remote script (e.g. as inputstream) and then included it as plain text.
我认为你必须将 jsFileUrl 用单引号引起来。
I think u have to enclose the jsFileUrl in single quotes.
请查看此http://lexandera.com/2009/ 01/adding-alert-support-to-a-webview/
谢谢
please check out this http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
Thanks