I have developed the gui with html and javascript. I want to load that in my webview and call a javascript function when a URL is called. I have written the following code. But my javascript function is not called at all. I have tried various options. Please help.
public class HelloWebView extends Activity {
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(new JavaScriptHandler(this), "Android");
webview.loadUrl("file:///android_asset/index.html");
webview.setWebViewClient( new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println( "shouldOverrideUrlLoading [" + url + "]" );
webview.loadUrl( "javascript:sendData()" );
return true;
}
public void onPageFinished(WebView view, String url) {
System.out.println( "onPageFinished [" + url + "]" );
webview.loadUrl( "javascript:function sendData(){}" );
}
});
webview.setWebChromeClient(new WebChromeClient() {
#Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
/* Do whatever you need here */
return super.onJsAlert(view, url, message, result);
}
});
}
public class JavaScriptHandler {
Context mContext;
public JavaScriptHandler(Context context) {
mContext = context;
}
#JavascriptInterface
public void returnResult() {
}
}
}
Javascript:
sendData : function()
{
alert("Inside SendData()");
if( this.sendQueue.length > 0 )
{
var url = this.sendQueue[0];
this.sendQueue.splice( 0, 1 );
window.location = url;
}
}
Related
I need to get a link address from a webpage that is generated by javascript clicking on a button.
I click on the link in this way
webView.loadUrl("javascript:(function(){" +
"var All = document.getElementsByTagName('img');\n" +
"for (var i = 0; i < All.length; i++) {\n" +
"if (All[i].getAttribute('title') == 'Download') {\n" +
"var imgButton=All[i];\n" +
"e=document.createEvent('HTMLEvents');\n" +
"e.initEvent('click',true,true);\n" +
"imgButton.dispatchEvent(e);\n" +
"}\n" +
"}})()");
the problem is that after running this code, the event OnPageFinished is never called. I had try with OnProgressChanged using WebChromeClient but I had the same result.
I need to obtain the html code of the webpage after that the javascript is runned Any idea about how can I do it?
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialog = new ProgressDialog(Activity.this);
progressDialog.setMessage("Loading...Please wait");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
view.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Oh no! "+description , Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description,failingUrl);
}
});
Here is the method
#SuppressWarnings("unused")
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
#Keep
#JavascriptInterface
public void showHTML(String html) {
try {
Log.d("html",html);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
I've looked at a few SO posts regarding this issue, and below is what I have came up with:
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("http://google.com");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String password = "a123345";
//webView.loadUrl("javascript:document.getElementById('lst-ib').value = '" + "hi" + "';");
webView.loadUrl("javascript:var uselessvar = document.getElementByName('q').value='" + password + "';");
Log.d("d","Done");
}
}
But nothing I have tried has worked. Any idea what I am missing?
Few things about this
Try to avoid using Thread.sleep in main thread
Dont load script with constant time
You can use of callback like WebViewClient callbacks which have at least onPageFinished method which you can use to load the JS
Add setJavaScriptEnabled to run the JS
Example :
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
String password = "a123345";
view.loadUrl("javascript:(function() { " +
"document.getElementsByName('q')[0].value = '" + password + "'; " +
"})()");
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://google.com");
I'm loading data from website to a webview in my application.And i need to make the video plays inside the webview.my problem is when i don't use setJavaScriptAsEnable the images and the video loads just fine but i can't play the video inside the webview it gives me the following error
An error occurred. try watching this video on youtube or enabe JavaScript.
however when i use setJavaScriptAsEnable(true) all the images and videos inside the webview disappears like the following image.
here's my code for the webview
void setupWebView(final WebView webView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// add padding to the html page
webView.loadUrl("javascript:(function(){ document.body.style.paddingBottom = '30px'})();");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String request) {
if (request.contains("http")) {// TODO: 14/08/2017 use regs
Log.d("url", request);
new CheckIfImageAsyncTask().execute(request);
return true;
}
return false;
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains("http")) {
new CheckIfImageAsyncTask().execute(url);
return true;
}
return false;
}
});
// settings
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
String data = "<div>" + post.getContent() + " </div>";
webView.loadDataWithBaseURL("file:///android_asset/", getHtmlData(data), "text/html", "utf-8", null);
}
private class CheckIfImageAsyncTask extends AsyncTask<String, Void, Boolean> {
private String url;
#Override
protected Boolean doInBackground(String... strings) {
url = strings[0];
URLConnection connection = null;
boolean isImage = false;
try {
connection = new URL(url).openConnection();
String contentType = connection.getHeaderField("Content-Type");
isImage = contentType.startsWith("image/");
} catch (IOException e) {
e.printStackTrace();
}
return isImage;
}
#Override
protected void onPostExecute(Boolean isImage) {
try {
if (isImage) {
Intent intent = new Intent(PostDetailsActivity.this, FullScreenImageActivity.class);
intent.putExtra("coverImage", url);
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
FirebaseCrash.report(new Exception(e.getMessage()));
}
}
}
i searched a lot and i can't figure it out. any help will be appreciated. thanks in advance.
I am trying to pass a value from a custom javascript file I have included after loading a URL in a webview.
I have checked this reference but it doesn't work when I am loading a website apart from a local index.html file.
Here is my activity code where I have created the Javacript Interface:
public class MainActivity extends AppCompatActivity {
public static String URL = "https://www.ornativa.com";
private WebView mWebView;
private RelativeLayout mLoader;
private Toolbar mToolbar;
JSInterface JSI = new JSInterface(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createWebView();
}
#SuppressLint("SetJavaScriptEnabled")
public void createWebView(){
mWebView = (WebView)findViewById(
R.id.webView);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
mToolbar.setTitle(getResources().getString(R.string.app_name)+" | "+getResources().getString(R.string.app_name_hindi));
mLoader = (RelativeLayout) findViewById(R.id.loader);
// Add javascript support to the webview
mWebView.getSettings().setJavaScriptEnabled(true);
// Enable pinch zoom controls on webview
mWebView.getSettings().setBuiltInZoomControls(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
{ WebView.setWebContentsDebuggingEnabled(true); }
}
// Add a WebViewClient
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// Inject CSS when page is done loading
injectCSS();
injectJS();
super.onPageFinished(view, url);
mLoader.setVisibility(View.GONE);
}
});
mWebView.addJavascriptInterface(JSI, "Android");
// Load a webpage
mWebView.loadUrl(URL);
}
public void injectCSS(){
try {
InputStream inputStream = getAssets().open("styles.css");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
mWebView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var style = document.createElement('style');" +
"style.type = 'text/css';" +
// Tell the browser to BASE64-decode the string into your script !!!
"style.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(style)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
private void injectJS() {
try {
InputStream inputStream = getAssets().open("script.js");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
mWebView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(script)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(R.string.app_name);
builder.setMessage("Do you really want to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public class JSInterface {
private Context mContext;
public JSInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
}
}
And this is my script.js file:
document.getElementById("aadhaarNo").type= 'tel';
var myElem = document.getElementById('actionMessages');
Android.showToast("Hello World!");
if (myElem === null) {
console.log('does not exist');
}
else{
if (myElem.classList.contains('success')){
console.log('success');
}
}
I am trying to call the function from the script file to send the value to the Toast but the Toast doesn't appear.
Change "Context" to "Activity" in class JSInterface. Check the below edited code..
public class JSInterface {
private Activity mContext;
public JSInterface(Activity c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
}
I have a code here where I want to retrieve an element from the webview of android and I can't seems to figure out what's wrong. It might be some silly mistakes but I just can't see why and what's wrong. Thanks.
private void retrieveContentWebView(String address){
Log.d("Web Download"," Inside ");
Log.d("Web Download ","address : " + address);
WebView webView;
webView = new WebView(context);
webView.loadUrl(address);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(this, "myjava");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("Web Download"," Inside Page Started");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.d("Web Download"," Inside Page Finished");
view.loadUrl("javascript: {" +
"var titleTexts = document.getElementsByTagName('title')[0]" +
"myjava.onData(titleTexts.text);}");
}
});
}
//This method will be registered as a JavaScript interface
#JavascriptInterface
#SuppressWarnings("unused")
public void onData(final String value){
Log.d("Web Download"," Inside Handler");
final String getValue = value;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
Log.d("Web Download HTML :",getValue);
}
}, 5000);
}
Try adding this to your WebView:
webView.setWebChromeClient(new MyCustomChromeClient(this));
Create the MyCustomChromeClient for yourself.