0

I need some help with Android's WebView. I am trying to create an app which loads a webpage, I cannot change. The webpage does load a number of javascript files and I am trying to overwrite the behavior of one of the existing objects in them. The problem is that in one of the scripts, this object gets initialized with a constructor and gets reset like:

obj.init();//invokes the method in my ObjInterface class which I inject
obj = new ObjectClass();
obj.init();//invokes the original ObjectClass method

Passed this line, my interface is no longer called and the obj behaves as per default. I have this flags set:

settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDomStorageEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);

How could I achieve this? Regards

Bilbo
  • 63
  • 1
  • 5

1 Answers1

0

I am not sure to understand completely your problem. Are you trying to inject an entire javascript class?

Don't know if you can but have you tried to inject converting in string? Something like this:

inner class BaseWebViewClient : WebViewClient() {
      override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                
                view.loadUrl("javascript: $yourCode")
    }
}

hope this helps

snor09
  • 31
  • 2
  • Hi, thank you for the reply. Yes, basically I am trying to inject an entire class and this is possible with the webview's #addJavascriptInterface and it works until the object is created -> https://developer.android.com/guide/webapps/webview#BindingJavaScript – Bilbo Jun 28 '22 at 12:11