Run custom javascript code after loading any website - javascript

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

Related

How to call Javascript function from Android without onPageFinished

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.

Redirect to specific url if page viewed from android by detecting it's package name from website (html or javascript)?

Is it possible Redirect to specific url if page viewed from android by detecting it's package name (or another identificator information) from website (html or javascript)?
I think what you are looking is this meta tags.
I think this idea is impossible if you brows your web page from external browser (like google chrome).
So I suggest to brows the web page in a webview inside your android application and make your code listen for this webview by extend WebViewClient and call onPageFinished() :
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
In the web app you can make a specific page for redirecting (for example www.your_website.com/redirecting.html) , and make the app do what you want when surf this link :
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
if(url == "www.your_website.com/redirecting.html"){
//do what you want...
}
}
});

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;
}

Categories

Resources