javascript interface in not working in below 5.0 - javascript

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");

Related

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");

"Uncaught TypeError: window.HTMLOUT.showHTML is not a function"

I am trying to inject JavaScript for reading on specific value while loading webView.
These are the properties i used for my webView.
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
and i am injecting javacript in my onPageFInished() method.
#Override
public void onPageFinished(final WebView view, final String url) {
webView.post(new Runnable() {
#Override
public void run() {
webView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('input')[0].value+'</head>');");
}
});
super.onPageFinished(view, url);
}
Below code is MyJavaScriptInterface.
public class MyJavaScriptInterface{
#JavascriptInterface
public void showHTML(String html_data) {
if(html_data.contains("response_code")){
Log.e(TAG, " ======> HTML Data : "+ html_data);
new MakeQueryPayment().execute();
}
}
}
Error i captured from the Logcat.
01-08 17:56:43.701 I/chromium(27026): [INFO:CONSOLE(1)] "Uncaught TypeError: window.HTMLOUT.showHTML is not a function", source: (1)
I m facing this problem only in Samsung Galaxy Tab A, Model Number is SM-T550 , Android Version is 5.0.2. In other devices which we have it's working fine. Can any one please help me out from this.
Thanks in advance.
I try on Galaxy Tab 4, this code running well on this device.
WebView properties
WebView webView = new WebView(this);
setContentView(webView);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.loadUrl("http://stackoverflow.com/questions/34746626/uncaught-typeerror-window-htmlout-showhtml-is-not-a-function");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
JavaScript Interface
public class MyJavaScriptInterface{
#JavascriptInterface
public void showHTML(String html_data) {
Log.e("", " ======> HTML Data : "+ html_data);
}
}
For me, as it was working fine before upgrading to the latest targetSDK, I just had to add the annotation #JavascriptInterface on all my functions.
Example:
#JavascriptInterface
public void eventDragStart() {
// do somthing
}

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 injection into webview

I know it exists a lot of questions about that but i don't understand why my following code does not work anymore
Here is my code :
private void init() {
webview.setWebViewClient(new FormWebViewClient());
webview.postUrl(url, EncodingUtils.getBytes(data, "BASE64"));
}
private class FormWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// progressBar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
LOGD(TAG, "Url : " + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:" +"document.getElementsByClassName('my_class_name')[0].value = '" + myValue + "';" +
}
}
My original webview is overrided and it displays only myValue in the page instead of plenty of informations.
If anybody knows why i have this behavior ...
Thx
EDIT :
and the part of html
<input type="text" size="20" maxlength="19" autocomplete="off" name="CARD_NUMBER" id="CARD_NUMBER" class="my_class_name" value="">
Finally, I've found the answer :
I have to add void(0); at the end of JavaScript instruction like this :
view.loadUrl("javascript:" +"document.getElementsByClassName('my_class_name')[0].value = '" + myValue + "';void(0);")

Android : Inject JavaScript before loading the URL in webview

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

Categories