6

I am working on an app which has an activity/fragment that displays web pages for certain sites. Android webview (chrome) is popping up alert dialogs for subscribing to push notifications in some of them, which is causing weird flashing/flickering issue.

This is the section for webview and it's settings

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setAppCacheMaxSize(5 * 1024 * 1024);
    webSettings.setAppCachePath("");
    webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webSettings.setUserAgentString(mUserAgent);

    webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            BaseWebViewFragment.this.onReceivedTitle(title);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressChanged(view, newProgress);
        }
    });

I need to disable these popups completely but cannot find a way to do it.

I tried disabling JS completely, but it causes loading issue for some of the sites.

I also tried overriding the onJS...() methods for WebChromeClient and canceling/confirming the result but to no avail.

Whats the working approach for this?

d1xlord
  • 239
  • 3
  • 4
  • 12

1 Answers1

0

Similar to this you should get html as string and insert this script tag in its head

  <script type = "text/javascript">
    window.alert = function() {};
  </script>

so if your html string is htmlStr, it's something like this:

String script = "<script type = \"text/javascript\">\n" +
                "        window.alert = function() {}\n" +
                "      </script>";
String target = "<head>";

int ind = htmlStr.indexOf(target);
StringBuilder htmlBuilder = new StringBuilder(htmlStr);
htmlBuilder.insert(ind + target.length(), script);
String newHtml = htmlBuilder.toString();

and then load the newHtml into WebView by something like this:

webView.loadDataWithBaseURL(null, newHtml, "text/html", "UTF-8", null);
saiedmomen
  • 1,401
  • 16
  • 33