【问题标题】:Android prevent WebView navigationAndroid阻止WebView导航
【发布时间】:2015-04-23 15:27:16
【问题描述】:

我正在使用 Android 5.0 和 WebView 组件的最新更新。我尝试将远程网站(我无法修改此网站源代码)嵌入到 WebView 中。

下面是我的代码

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains(URL_SERVLET)) {
            //Do nothing 
            Log.d("DEBUG", "load IMAGE");
            //view.stopLoading();
        } else {
            Log.d("DEBUG", "load URL into WebView");
            view.loadUrl(url);
        }
        return true;
    }
    // Called when all page resources loaded
    public void onPageFinished(WebView view, String url) {
        webView.setVisibility(View.VISIBLE);
        splashscreenLayout.setVisibility(View.GONE);
    }
});

在网站方面,我得到一个在弹出窗口中打开另一个 url(使用 javascript)的按钮。当我通过 Android Webview 点击它时,这个新的 URL 会替换 webview 中的当前 URL。我只是希望当我点击这个按钮时什么都没有发生。

它适用于此代码:此 url 未加载。但是 webview 变成了白色,就像一个空白屏幕。

我尝试调用 stopLoading(),但没有成功。

我不能调用 webview.reload(),因为远程网站是用 jsp 编写的,带有一个提供(我猜是动态生成)html 页面的 url。如果我调用 reload(),WebView 将重新初始化到第一页(登录页)。

我也尝试保存 webview 状态并恢复它,但它不起作用。

有没有办法“阻止”更改 url 并保持 webview 指向第一个 url 而无需重新加载?

【问题讨论】:

    标签: javascript android webview


    【解决方案1】:

    有一种有点骇人听闻的方法可以做到这一点,那就是编写一些阻止页面导航并将其注入 WebView 的 javascript。

    javascript 看起来像 here 发布的答案

    var location = window.document.location;
    
    var preventNavigation = function () {
        var originalHashValue = location.hash;
    
        window.setTimeout(function () {
            location.hash = 'preventNavigation' + ~~ (9999 * Math.random());
            location.hash = originalHashValue;
        }, 0);
    };
    
    window.addEventListener('beforeunload', preventNavigation, false);
    window.addEventListener('unload', preventNavigation, false);
    

    注意:另一种侵入性较小的方法可能是让您的 javascript 简单地禁用相关的特定按钮。

    然后,要将其注入您的 WebView,您可以按照发布的示例 here

    WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowUniversalAccessFromFileURLs(true);
    myWebView.setWebViewClient(new WebViewClient() {
       @Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
          return false;
       }
    
       @Override
       public void onPageFinished(WebView view, String url) {
          super.onPageFinished(view, url);
    
          injectScriptFile(view, "js/script.js"); // see below ...
    
          // test if the script was loaded
          view.loadUrl("javascript:setTimeout(test(), 500)");
       }
    
       private void injectScriptFile(WevView view, String scriptFile) {
          InputStream input;
          try {
             input = getAssets().open(scriptFile);
             byte[] buffer = new byte[input.available()];
             input.read(buffer);
             input.close();
    
             // String-ify the script byte-array using BASE64 encoding !!!
             String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
             view.loadUrl("javascript:(function() {" +
                          "var parent = document.getElementsByTagName('head').item(0);" +
                          "var script = document.createElement('script');" +
                          "script.type = 'text/javascript';" +
             // Tell the browser to BASE64-decode the string into your script !!!
                          "script.innerHTML = window.atob('" + encoded + "');" +
                          "parent.appendChild(script)" +
                          "})()");
          } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
          }
       }
    });
    
    myWebView.loadUrl("http://www.example.com");
    

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 2018-05-04
      • 2018-12-02
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多