是否有可能调用addJavascriptInterface由onCreateWindow创建的WebView? [英] Is it possible to call addJavascriptInterface on a WebView created by onCreateWindow?

查看:421
本文介绍了是否有可能调用addJavascriptInterface由onCreateWindow创建的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的浏览器应用程序,我的 onCreateWindow 是这样的:

I have a simple browser app, my onCreateWindow looks like this:

if (userGesture) {
            WebView childView = new WebView(webBrowserActivity);
            WebSettings settings = childView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setSupportZoom(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setBuiltInZoomControls(true);
            settings.setSupportMultipleWindows(true);
            settings.setLoadWithOverviewMode(true);
            settings.setUseWideViewPort(true);
            childView.addJavascriptInterface(new MyInterface(),"NAME");
            <--- code in here to add the view to a ViewPager.
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(childView);
            resultMsg.sendToTarget();
            Log.v(TAG, "Opening popup for " + childView.getUrl());
            webBrowserActivity.invalidateOptionsMenu();
            return true;
        } else {
            return false;
        }

我已经尝试了一下后加入这个接口上下行,我也打过电话重装()使用loadURL()页面后加载,因为文件说的JavaScript接口之前,必须加载页面增加。这些都没有帮助。有没有人成功地做到了这一点?

I have tried adding this interface a bit later down the line, I have also tried calling reload() or loadUrl() after the page is loaded because the documentation says the javascript interface must be added prior to loading the page. None of this has helped. Has anyone done this successfully?

感谢。

编辑:我应该提到我看到更多的4.4,我使用桌面版Chrome调试器,我可以进入控制台,并请参阅 window.NAME 对象在第一网页视图,而不是在第二个。在4.3似乎相当频繁工作。

I should mention I see this more on 4.4, I am using the desktop debugger for Chrome and I can go into the console and see the window.NAME object on the first webview and not on the second one. On 4.3 it seems to work fairly often.

我编辑上面的code,以显示我使用的设置。当加入到活动开始时,时生成的第一个WebView中的JavaScript界面​​,我增加了的WebView的作品就好。

I edited the code above to show the settings I am using. The javascript interface I am adding to the WebView works just fine when added to the first WebView that gets created when the Activity starts.

推荐答案

不幸的是,在奇巧的WebView,使得它不可能注册弹出窗口上的JavaScript接口的错误。没有为我担心,除了你的建议,为保持在弹出的网址,从onCreateWindow返回false,然后创建一个全新的WebView和URL加载弹出到其中的问题没有已知的解决方法。

Unfortunately there is a bug in the KitKat WebView that makes it impossible to register a JavaScript interface on the popup window. There is no known workaround for the issue I'm afraid, other than as you suggest, keep the URL for the popup, return false from onCreateWindow and then create a whole new webview and load the pop up URL into it.

这篇关于是否有可能调用addJavascriptInterface由onCreateWindow创建的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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