Android 在webview外部js中注入,执行函数,并引发警报

发布于 2024-10-13 05:59:39 字数 695 浏览 4 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-10-20 05:59:39

好吧,经过多次尝试,我找到了解决方法,但不幸的是不是“解决方案”。我使用了此加载 view.loadUrl("javascript:" + src + "execute(); " + ""); 而源 src 来自文本文件 < code>script.js 其中包括我的 javascript(函数和普通命令)

//get script
InputStream is;
        String src= "";
        try {
            is = getAssets().open("travianbot.js");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            // Convert the buffer into a string.
            src = new String(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

script.js 的示例(小心行结尾“;”)

function addItem(v, link, x, y, result) {
    //function commands  
}
function popup() {
    alert(execute().split("@"));
}     
function execute(){  
    //function commands
    additem(...);
}
// plain commands
.......

远程脚本的一种解决方案,我尚未测试过它是解析远程脚本(例如作为输入流),然后将其作为纯文本包含在内。

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 source src comes from a text file script.js which includes my javascript (both functions and plain commands)

//get script
InputStream is;
        String src= "";
        try {
            is = getAssets().open("travianbot.js");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            // Convert the buffer into a string.
            src = new String(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

a sample of the script.js (be careful on line endings ";")

function addItem(v, link, x, y, result) {
    //function commands  
}
function popup() {
    alert(execute().split("@"));
}     
function execute(){  
    //function commands
    additem(...);
}
// plain commands
.......

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.

余生共白头 2024-10-20 05:59:39

我认为你必须将 jsFileUrl 用单引号引起来。

script.src='" + jsFileURL + "';"

I think u have to enclose the jsFileUrl in single quotes.

script.src='" + jsFileURL + "';"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文