Run custom javascript code after loading any website

99封情书 提交于 2019-12-12 02:57:28

问题


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.


回答1:


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:...");       
    }
}



回答2:


If you are looking for an automated solution, try PhantomJs this provides an automated headless web browser. Also has access to network traffic




回答3:


perhaps you can try "bookmarklet" http://www.bookmarklets.com/

the advantage over greasemonkey script is that it can run on firefox and explorer



来源:https://stackoverflow.com/questions/9634409/run-custom-javascript-code-after-loading-any-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!