Android - after loading the URL using webview I can change the background color

I have a webview and am loading the external HTML form of the site. I am trying to change the background color using javascript function:

function changeBGC(color){ document.bgColor = color; } 

and it won’t work. but if I load locally, then I can change the background color. Is there some kind of protection preventing me from modifying the webpage that I upload to the webview from the outside?

+4
source share
1 answer

You can run javascript using the WebViewClient example, here .

Javascript code that changes the background color of a document .

So, all together:

When launching WebView:

 WebView webview = new WebView(); webview.setWebViewClient(new WebClient()); webView.getSettings().setJavaScriptEnabled(true); webview.loadUrl("stackoverflow.com"); 

Your webview client:

 public class WebClient extends WebViewClient { int color; public WebClient(int color) { this.color = color; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { String command = "javascript:document.body.style.background = " + color + ";"; view.loadUrl(command); } } 
+5
source

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


All Articles