webview 不显示 javascript windows.open()

发布于 2025-01-03 04:02:22 字数 3366 浏览 3 评论 0原文

我有一个 WebView,其中显示我无法控制的网页内容。内容显示正常,但有生成弹出窗口的链接。执行此操作的 javascript 函数如下所示:

function getEntry(id) {
var win = window.open('', 'Booking',
'resizable=yes,scrollbars=yes,status=no,width=500,height=400');
win.document.location = '/some/url/1-' + id ;
}

我无法轻松更改此设置,如果负责我下载页面的人员更改它,我想我的应用程序会严重失败......

我的 WebView 活动中的设置如下所示:

    _webview = new WebView(this);
    setContentView(_webview);

    final Activity activity = this;
    _chromeClient = new MyChromeClient();

    _webview.setWebChromeClient(_chromeClient);

    //I experimented with changing user agent, in case that would have any effect, it didn't...
    _webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");

    _webview.setWebViewClient(new MyWebViewClient());
    _webview.getSettings().setJavaScriptEnabled(true);
    _webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    _webview.getSettings().setSupportZoom(true);
    _webview.getSettings().setBuiltInZoomControls(true);
    _webview.getSettings().setDomStorageEnabled(true);
    //Cache settings...
    _webview.getSettings().setAppCacheMaxSize(1024*1024*8);
    _webview.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
    _webview.getSettings().setAllowFileAccess(true);
    _webview.getSettings().setAppCacheEnabled(true);

MyWebClient:

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onLoadResource(WebView view, String url) {
        Log.d("MyWebViewClient",url);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        showProgressDiag();
        Log.d("MyWebViewClient","shouldOverride... : " + url);
        view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageFinished(WebView view, String url){
        hideProgressDiag();
    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        if(failingUrl.equals("file:///android_asset/html/error.html")){
            hideProgressDiag();
            Toast.makeText(_context, "Error! Check internet connection, or try again later...", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(_context, failingUrl, Toast.LENGTH_SHORT).show();

            view.loadUrl("file:///android_asset/html/error.html");
        }

    }

}

MyChromeClient:

private class MyChromeClient extends WebChromeClient{

    @Override
    public void onProgressChanged(WebView view, int progress) {
     Pdiag.setProgress(progress * 100);
    }

}

当单击指向 javascript 函数的链接之一时,所发生的情况是 WebView 变为灰色,而不经过shouldOverrideUrlLoading()。当我按下后退键时,应用程序退出,这意味着 WebView 的导航历史记录中没有放置任何内容。有时什么也没有发生,但随后 shouldOverrideUrlLoading() do run 并从 Log.d() 我可以看到正确的 URL弹出窗口已提供给 WebView。

问题是,在极少数情况下它显示得很好,但我不知道如何重现它,也不知道它是否真的显示在弹出窗口中。

我迷失了......并且非常沮丧......想看一部糟糕的情景喜剧:(

编辑: 实际上,也许 URL 并不是那么正确......在 Firefox 中,URL 以“X<<<<”结尾但在我的 Log.d() 输出中,它以“X%3C%3C%3C%3C”结尾...我将调查是否可以更改它...

编辑2: 不,什么都没做...该 URL 与 Firefox 中的 URL 相同...

I have a WebView in which i display web content which i have no control over. The content displays fine, but have links which spawn a popup window. The javascript function that does that looks like this:

function getEntry(id) {
var win = window.open('', 'Booking',
'resizable=yes,scrollbars=yes,status=no,width=500,height=400');
win.document.location = '/some/url/1-' + id ;
}

I can't easily change this, and if the people responsible for the page i download would change it, i guess my app would fail miserably...

My WebView setup in the activity looks like this:

    _webview = new WebView(this);
    setContentView(_webview);

    final Activity activity = this;
    _chromeClient = new MyChromeClient();

    _webview.setWebChromeClient(_chromeClient);

    //I experimented with changing user agent, in case that would have any effect, it didn't...
    _webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");

    _webview.setWebViewClient(new MyWebViewClient());
    _webview.getSettings().setJavaScriptEnabled(true);
    _webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    _webview.getSettings().setSupportZoom(true);
    _webview.getSettings().setBuiltInZoomControls(true);
    _webview.getSettings().setDomStorageEnabled(true);
    //Cache settings...
    _webview.getSettings().setAppCacheMaxSize(1024*1024*8);
    _webview.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
    _webview.getSettings().setAllowFileAccess(true);
    _webview.getSettings().setAppCacheEnabled(true);

MyWebClient:

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onLoadResource(WebView view, String url) {
        Log.d("MyWebViewClient",url);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        showProgressDiag();
        Log.d("MyWebViewClient","shouldOverride... : " + url);
        view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageFinished(WebView view, String url){
        hideProgressDiag();
    }
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        if(failingUrl.equals("file:///android_asset/html/error.html")){
            hideProgressDiag();
            Toast.makeText(_context, "Error! Check internet connection, or try again later...", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(_context, failingUrl, Toast.LENGTH_SHORT).show();

            view.loadUrl("file:///android_asset/html/error.html");
        }

    }

}

MyChromeClient:

private class MyChromeClient extends WebChromeClient{

    @Override
    public void onProgressChanged(WebView view, int progress) {
     Pdiag.setProgress(progress * 100);
    }

}

When clicking one of the links that points to the javascript function all that happens is that the WebView turns grey, without going through shouldOverrideUrlLoading(). When i hit the back key the app exits, meaning that nothing was placed in the nav history of the WebView. Sometimes nothing happens, but then the shouldOverrideUrlLoading() do run and from a Log.d() i can see that the correct URL for the popup has been given to the WebView.

The thing is, on very rare occasions it shows up fine, but i have no clue how to reproduce it, and wether it actually shows in a popup.

I'm lost... And quite frustrated... Thinking of watching a bad sitcom instead :(

EDIT:
Actually, maybe the URL wasn't all that correct after all... In Firefox the URL ends with "X<<<<" but in my Log.d() output it ends with "X%3C%3C%3C%3C"... I'll investigate if i could change that...

EDIT 2:
Nope, didn't do anything... The URL is identical to the one in Firefox...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

溺ぐ爱和你が 2025-01-10 04:02:22

首先,您需要在 WebView 上进行以下设置:

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);

然后您需要附加一个覆盖 onCreateWindowWebChromeClient。您实现此方法可以创建一个新的 Web 视图,并将其显示在对话框中:

webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            WebView newWebView = new WebView(MyActivity.this);
            WebSettings webSettings = newWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            // Other configuration comes here, such as setting the WebViewClient

            final Dialog dialog = new Dialog(MyActivity.this);
            dialog.setContentView(newWebView);
            dialog.show();

            newWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onCloseWindow(WebView window) {
                    dialog.dismiss();
                }
            });

            ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
            resultMsg.sendToTarget();
            return true;
        }

});

不要忘记将新的 Web 视图设置为 resultMsg,将其发送到其目标并返回 true,如下所示API 文档

First of all, you need to set the following settings on your WebView:

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);

Then you need to attach a WebChromeClient that overrides onCreateWindow. Your implementation of this method can create a new web view, and display it inside a dialog:

webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
            WebView newWebView = new WebView(MyActivity.this);
            WebSettings webSettings = newWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            // Other configuration comes here, such as setting the WebViewClient

            final Dialog dialog = new Dialog(MyActivity.this);
            dialog.setContentView(newWebView);
            dialog.show();

            newWebView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onCloseWindow(WebView window) {
                    dialog.dismiss();
                }
            });

            ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
            resultMsg.sendToTarget();
            return true;
        }

});

Don't forget to set the new web view to the resultMsg, send it to its target and return true, as mentioned in the API documentation.

§普罗旺斯的薰衣草 2025-01-10 04:02:22

请检查添加此 -

    getSettings().setSupportMultipleWindows(true);

Please check with adding this -

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