How to insert javascript script in webview before loading html in android 7

I will present a modified example of my problem to simplify this question, but the essence in my full example is the same:

I need to load the html code inside webview, this code assumes some javascript functions will be declared. Sample HTML code:

<html> <body><h1>JavaScript Math.random()</h1> <p>A random number:</p> <p id="demo"></p> <script>showRandomValue();</script> </body> </html> 

in this example, the code accepts a declared function called showRandomValue() . Therefore, I need to add this function before the code is executed. This is the FUNCTION I want to enter:

 <script> function showRandomValue() { document.getElementById("demo").innerHTML = Math.random(); } </script> 

So far, I have done this before loading the code in webview:

 public static final String JAVASCRIPT_FUNCTION = "javascript:function showRandomValue() {\n" + " document.getElementById(\"demo\").innerHTML = Math.random();\n" + " }"; webView.loadUrl(JAVASCRIPT_FUNCTION); webView.loadData(HTML_CODE, "text/html", "UTF-8"); 

This worked for me until I wanted to upgrade mySetkVersion to 24. If I set targetSdkVersion 24 , this code will stop working because when the code loads, the function was not found.

Revision of android webview documentation i found this:

Compatibility Note. Applications targeting N or later, JavaScript state from an empty WebView is no longer saved in navigations such as loadUrl (String). For example, global variables and functions defined before calling loadUrl (String) will not exist on the loaded page. Applications should use addJavascriptInterface (Object, String) instead to save JavaScript objects in navigations.

My problem is that with addJavascriptInterface you can declare a custom object so that I can declare:

 webview.addJavascriptInterface(new MyInterface(),"javaInterface"); 

and call its method, for example, javaInterface.getRandomValue() . But I can not declare global functions with this method.

It is important . I cannot edit or modify the HTML code because it comes from a third-party server.

How can I make this code work again in android 7+?

This is a complete code example if you want to test it:

 public class MainActivity extends Activity { private WebView webView; String HTML_CODE = "<html>\n" + "<body><h1>Hello World</h1>\n" + "<p>A random number:</p>\n" + "<p id=\"demo\"></p>\n" + "<script>showRandomValue();</script>\n" + "</body>\n" + "</html>"; public static final String JAVASCRIPT_FUNCTION = "(function showRandomValue() {document.getElementById(\"demo\").innerHTML = Math.random();})"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("javascript:" + JAVASCRIPT_FUNCTION); webView.loadData(HTML_CODE, "text/html", "UTF-8"); } } 

Compiling this code with targetSdkVersion 23 will work, and no random number will be displayed with targetSdkVersion 24

+6
source share
1 answer

I know that you said that you can’t change the HTML ... but if you load loadData with preloaded content, I don’t see any reason against entering the desired function in the html head in the script tag. Everything that you do is already a little hacked.

-one
source

Source: https://habr.com/ru/post/1014215/


All Articles