How to call Javascript function from Android without onPageFinished - javascript

There are a lot of answer but none of them work . All of them work with this funciotn:
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webview.loadUrl("javascript:Test('Andrdoid-->Html')");
}
});
but it work one time after load page in webview . we want use it during app is open by user.
some says use this line
webview.loadUrl("javascript:Test('Andrdoid-->Html')");
without onPageFinished . but it cant help.
Javascript:
funtion Test(){
alert("hi");
}

webview.evaluateJavascript("Test('Andrdoid-->Html')", null);
loadUrl will reload the page and call onPageFinished() again.
You may try evulateJavascript.
evaluateJavascript, will run JavaScript asynchronously and avoid blocking the UI thread.

well, the best way to communicate with webview is to make a bridge between the native application and web app,
check this library it will help you to communicate with the web app.

Related

Android WebView push notification?

I need to send a notification (not necessarily a push notification) through an android webview. I saw that the Notification API was not compatible with Android Webview on MDN. The other APIs I saw seemed to be based off of window.notification.
Does anyone know of any API or JS that sends a notification through an android webview?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase. Would that be helpful?
Thanks for your answers!
I don't have enough "reputation" to post a comment but could that be useful to you?
Android Push Notification with WebView?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example
Read the documentation entry for Binding JavaScript code to Android code.
This allows you to use javascript to trigger the execution of android code.
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.
JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called. In this example show a toast.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑
Then you can trigger the android code from javascript like this:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself. But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.
You would need to provide the notifications for example as a .json file in JSON format. You can upload that .json file to your webserver somewhere. Then pass the content to android and parse it accordingly.
One Simple workaround use JSInterafce - communicate webview to native.tutorial
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.

unable to execute javascript code in Android WebView from Service

I'm attempting to create and Android service that performs a task using JavaScript. I came across this post which describes how to run JavaScript code inside of a WebView within a Service using the WindowManager. I am able to create a WebView with an .html and .js file with no problem. It is once I try to pass data from the android .java service to the WebView that I run into an issue.
I have tried doing so in this fashion:
final WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
...
wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/www/test.html");
windowManager.addView(wv, params); // params set using method from linked post above
wv.evaluateJavascript("console.log('hello');", null);
wv.loadUrl("javascript:console.log('blah')");
Neither the call to evaluateJavascript() nor loadUrl() appear to have any effect on the WebView (I access the console using the chrome developer tools).
I have tested that in test.html I can add a <script> tag and output text to the console with no issue.
I've also tried calling the functions before adding the view to no avail.
What kind of data you want to pass to Javascript? You could use the WebView.addJavascriptInterface() to "Plant" methods on the HTML document so you can call them from Javascript, invoke in native and return data back to Javascript. Will that help?
If you're trying to execute this from a Service, you'll need to post a Runnable on the UI thread. Read the doc for evaluateJavascript()
It explicitly says it must be called on the UI thread. I think you can just do:
wv.post(new Runnable(){
#Override public void run()
{
wv.evaluateJavascript();
}
});
Other than that, should you include tags?

Android Headless Browsing through WebView?

I am trying to create an Android app for a Website, Which is not mine. But is a search engine for Restaurants. They have no API to work with. And i want to heedlessly browse their website and put the search query in the HTML Form and Click the Submit Button. And then Parse the Results and Use it with my Application Code. After doing loads of research here, i am finally asking for it. Question 1, Question 2, Question 3 and many more that i have looked so far. So all i know so far is if i want to do the same on Google.com i would write:
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.google.com/");
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//Load HTML
myWebView.loadUrl("javascript:document.getElementById('q') =" + "StackOverFlow" + "; document.getElementByName('btnK').click();");
}
});
In the above code i am trying to put the search term "StackOverFlow" and Click the Search Button. But its not working. Kindly Help me out in this code or either point me in the right direction.
It's been a while, but for the sake of letting others know, webviews no longer use loadUrl to run Javascript. Try using evaluateJavascript.
Since you've also mentioned headless browsing, I would recommend overriding shouldInterceptRequest in your client to redirect all unnecessary files (such as css, images, and perhaps js depending on the site) to a blank inputstream
myWebView.loadUrl("http://www.google.com/");
after overriding onPageFinished method not before

How do I run a function cordova if the command embedded in external sites?

I want to run a function to open the link in the android browser system. Here is an illustration of my questions. See the illustration What should I do? I only use javascript not java. Please help
Some time ago I encountered the same problem. To do this I modified the InAppBrowser source code.
you should override the shouldOverrideUrlLoading method in the InAppBrowserClient class found in InAppBrowser.java
This will allow you to hook in to the request before the url is being loaded and choose an alternate behavior. In your case loading the URL in the system browser.
Your code will look something like this:
#Override
public boolean shouldOverrideUrlLoading (WebView view, String url){
if(url.equals("Your URL to be loaded")){
openExternal(url);
return true;
}
return false;
}

Run custom javascript code after loading any website

I am working on taking readings about web browser performance and so need to access the window.performance object of the browser.
To collect this data i have written a javascript file, collect.js which i need to add to the DOM of the page that i need to test eg. www.google.com, www.facebook.com and so on...
Also i need to run this test for about 1000 websites, any manual approach is out of the question. I need it to be automated somehow.
How could i go about doing this?
EDIT: I need to run these tests on an android browser, so i need mobile oriented solutions.
You can create a simple android app with a WebView component. This way you can control which URLs are loaded and also insert your JS code.
http://developer.android.com/guide/tutorials/views/hello-webview.html
EDIT
You can run any javascript like this:
Implement a custom WebView:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// Execute your javascript below
view.loadUrl("javascript:...");
}
}
If you are looking for an automated solution, try PhantomJs this provides an automated headless web browser. Also has access to network traffic
perhaps you can try "bookmarklet"
http://www.bookmarklets.com/
the advantage over greasemonkey script is that it can run on
firefox and explorer

Categories

Resources