How to keep persistent CSS in WebView

This is the code I'm trying to execute; onPageFinished (WebView view, string url) I use to enter my own css to an external site. Unfortunately, with Webview, it first renders the page and then applies CSS. How can I wait until a page is displayed and then javascript is applied before it is displayed?

Is there a simpler method with WebClient HTTP Get and redirecting CSS to local?

Thanks for your suggestions in advance!

com.webkitdemo; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class WebKitDemo extends Activity { final Activity activity = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); //addition final webview final WebView webView = (WebView)findViewById(R.id.web_engine); /* JavaScript must be enabled if you want it to work, obviously */ webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle(" Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle(R.string.app_name); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } //additions @Override public void onPageFinished(WebView view, String url) { webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('body')[0].style.color = 'green'; " + "})()"); } // @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { webView.loadUrl(url); return true; } }); webView.loadUrl("http://code.google.com"); }} 
+4
source share
1 answer

Add this before changing the code style. Thus, it will be executed only after the page has finished loading.

 var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = yourfunctionhere(); } else { window.onload = function() { yourfunctionhere(); oldonload(); } } 
-1
source

All Articles