Android : Inject JavaScript before loading the URL in webview - javascript

I'm developing one application in which a URL is loaded in webview of the app. Through JavaScript I'm slicing out the header and footer of the webpage and showing content in the webview. My problem is that, it takes some time to slice out the header and footer of the webpage, for about 1-2 secs the header is visible and hide that immediately. I'm attaching my WebViewClient class :
private class CustomWebClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
doGet(url);
return true; // super.shouldOverrideUrlLoading(view, url);
}
public void onLoadResource(WebView view, String url) {
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('header')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('footer')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('section').search_again.style.display = 'none'; "
+ "})()");
}
}
On onLoadResource() method I'm injecting JS.
Anyone has got some idea about the problem I'm facing, please help me in overcoming this problem.

You can use a css file with media queries for android to hide the header/footer over css

Related

WebView with Javascript keep reloading

I am using a WebView to display a website into my application.
When I'm loading my WebView, I first need to fill the authentification form and then redirect to the original URL.
My WebView keep reloading again and again.
Could anyone explain to me how to use a WebView with "automatic" filling please?
final WebView mWebView;
//Show webview into the app via a popup
AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(context);
mAlertDialog.setTitle(R.string.menuBuyCredit);
mWebView = new WebView(context);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(ConstantsClass.URL_BUY_CREDIT);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//In order to automatically fill the form
final String mJavaScript = "javascript:" +
"document.getElementById('Login').value = '" + ClientSingleton.getInstance().getmLogin() + "';" +
"document.getElementById('Password').value = '" + ClientSingleton.getInstance().getmPassword() + "';" +
"document.getElementById('Btn_Envoyer').click()";
view.loadUrl(mJavaScript);
view.loadUrl(ConstantsClass.URL_BUY_CREDIT);
view.pageDown(true);
}
});
mAlertDialog.setView(mWebView);
mAlertDialog.setNegativeButton(R.string.closeDrawer, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mAlertDialog.show();
} ```
You should use something like
Put this in global
boolean loadedAlready = false;
inside onPageFinished method
if (view.getUrl().equals(ConstantsClass. URL_BUY_CREDIT) && !loadedAlready) {
loadedAlready= true;
view.load(ConstantsClass.URL_BUY_CREDIT);
}

Android Javascript function only works on initial load

In my Android Application, I have a WebView, before it displays anything from it though, I made it so that certain elements on my site do not display with javascript.It however only works on the first load, if you actually navigate through the site(click on a post for example) it will display all of the elements I don't want it to. It was working perfectly fine before, and now I cannot figure out why it isn't working. I'd appreciate any help!
mWebView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
return false;
}
#Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.INVISIBLE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url)
{
mWebView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('fa fa-youtube')[0].style.display='none'; " +
"document.getElementsByClassName('fa fa-twitter')[0].style.display='none'; " +
"document.getElementsByClassName('menu-primary-container')[0].style.display='none'; " +
"document.getElementsByClassName('menu-toggle')[0].style.display='none'; " +
"document.getElementsByClassName('widget_slider_area')[0].style.display='none'; " +
"})()");
bar.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
super.onPageFinished(view, url);
}
});
mWebView.loadUrl("http://mywebsite.com");

Javascript not working in Webview

I have a simple javascript where I want to give value to these input which is username and password. I open the website by using webview. When the webview fully loaded, the username and password place doesn't change at all.
The website : http://www.malaysiakini.com/login/en/form
My source code for the javascript :
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript: {" +
"$('input#username').val(" + user + ");\n" +
"$('input#username').val(\"password\");\n" +
"document.querySelectorAll(\"button[type='submit']\")[0].click();}");
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript: {" +
"$('input#username').val('" + user + "');\n" +
"$('input#username').val(\"password\");\n" +
"document.querySelectorAll(\"button[type='submit']\")[0].click();}");
}
Silly me, I missed the single quotes

Unable to set text in android webview using javascript

I have a webview with following settings :
mView.getSettings().setLoadWithOverviewMode(true);
mView.getSettings().setUseWideViewPort(false);
mView.getSettings().setJavaScriptEnabled(true);
mView.getSettings().setAllowContentAccess(true);
mView.getSettings().setDomStorageEnabled(true);
After loading a webpage, when I try to execute Javascript:
mView.loadUrl("javascript:document.querySelector('input[type=password]').value ='" + password + "'");
It opens a new tab or window instead of setting the value of password field.
Output of document.querySelector('input[type=password]') (from console) :
<input type="password" name="userpassword" id="userpassword" maxlength="6" style="width: 128px; height: 18px">
I tried executing the same javascript from console. It works!
I have also configured webviewclient:
mView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, " shouldOverrideUrlLoading :" + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d(TAG, "onPageStarted " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
}
});
loadUrl method, loads the provided URL in the WebView. What you have to do is execute the evaluateJavascript method.
webView.evaluateJavascript("document.querySelector('input[type=password]').value ='" + password + "'", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
/// This callback is called after a result is returned by the script.
}
});
As of Android Kitkat (4.4, API level 19), you can use evaluateJavascript.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("js_code", null);
} else {
webView.loadUrl("javascript:js_code");
}
You are forgetting quotes on your CSS selector.
It should be:
document.querySelector('input[type="password"]');
See jsfiddle

javascript interface in not working in below 5.0

I have used below code to avoid white space when my webview expands
it is working well in 5.0
I have added suppresslint #SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" }) to avoid error
But still white space remains in Android 4.2,4.3 devices
can someone suggest me to solve this issue.
web_content_url.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
pbar2.setVisibility(view.VISIBLE);
view.loadUrl(s[1]);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " + url);
pbar2.setVisibility(view.GONE);
view.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
pbar2.setVisibility(view.VISIBLE);
}
});
web_content_url.addJavascriptInterface(this, "MyApp");

Categories