2

I've been trying to inject some js into a webview in my app. Have been using
onReceivedTitle method of WebChromeClient since I want to execute the js while the page is loading.

This has been working until now. But recently, I observed that onReceivedTitle is not called we reload the webpage, similar to window.location.reload.

Firstly, I can't understand why it shouldn't be called. Or it should be and it's a bug?

Secondly, now that we know it's not called, where else can I inject by js?

Thanks.

Yash
  • 5,225
  • 4
  • 32
  • 65
  • I think this is the issue : https://cs.chromium.org/chromium/src/content/browser/web_contents/web_contents_impl.cc?l=4760 – Yash Jun 25 '18 at 05:35

2 Answers2

0

Apparently, this is not considered an Android bug, but rather a Chrome or Chromium-based WebView bug. It should be reported to http://crbug.com/.

I'll suggest you to try a workaround with custom WebViewClient:

You'll have to use a custom WebViewClient to get this done. You will override the onPageFinished() method so when a new page finishes loading you can set the webview to the appropriate title.

Below is a sample implementation of the above:

WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        ExperimentingActivity.this.setTitle(view.getTitle());
    }
});

This answer here will prove to be useful:https://stackoverflow.com/a/8193479/9080948

  • yeah, that's what I did actually, with a small difference. Instead of setting the title, I directly injected the script – Yash Jun 25 '18 at 06:46
  • could you please share your code to give a better picture of your question.And are you using WebViewClient now? – alamshahbaz16497 Jun 25 '18 at 07:12
0

Seems like this is a bug in chromium. I worked around this by injecting my scripts on onPageFinished method of WebviewClient.

Yash
  • 5,225
  • 4
  • 32
  • 65