1

I have loaded my Webpage in Android WebView using webview.loadUrl() and I am trying to inject a Javascript into the WebView using webview.evaluateJavascript().

But the problem here is, the Javascript is available in a remote location/url. I want to get the javascript from a remote location and inject it into WebView.

I have searched related to this question but all I have found is loading from string.

2
  • 1
    You will need to download it yourself with an http library like okhttp Commented Sep 10, 2019 at 1:39
  • 1) get Javascript from remote location/url. 2) inject it. Commented Sep 10, 2019 at 6:01

2 Answers 2

1

You can do it like this :

private static class WC extends WebViewClient {
    Handler handler1 = null;
    WebView webView = null;
    byte[] b = null;

    public WC(Handler handler, WebView webView) {
        this.handler1 = handler;
        this.webView = webView;
    }

    @Override
    public void onPageFinished(final WebView view, String url) {
        super.onPageFinished(view, url);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    injectScriptFromNetWork("http://192.168.216.254:4000/test.js");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void injectScriptFromNetWork(String urlStr) throws IOException {

        URL url = new URL(urlStr);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        int code = urlConnection.getResponseCode();
        if (code == 200) {
            InputStream inputStream = urlConnection.getInputStream();
            b = new byte[inputStream.available()];
            inputStream.read(b);
        }

        handler1.post(new Runnable() {
            @Override
            public void run() {
                String jsSource = Base64.encodeToString(b, Base64.NO_WRAP);
                webView.loadUrl("javascript:(function() {" +
                        "var parent = document.getElementsByTagName('head').item(0);" +
                        "var script = document.createElement('script');" +
                        "script.type = 'text/javascript';" +
                        "script.innerHTML = window.atob('" + jsSource + "');" +
                        "parent.appendChild(script)" +
                        "})()");
            }
        });
    }
}

MainActivity.java code like this:

handler = new Handler(getMainLooper());
webView.setWebViewClient(new WC(handler, webView));
webView.loadUrl("file:///android_asset/index.html");

Forgave me, this is my first time answering questions on stackoverflow.

Sign up to request clarification or add additional context in comments.

1 Comment

SO users always try to improve things, no apologies required. You can visit StackOverflow code of conduct to know how to write good question/answer. Your answer is primarily code-based. try to explain it rather giving a code snippet only. It will help the community for future reference.
1

To enable javascript try this.

WebView myWebView = (WebView) findViewById(R.id.webview);

myWebView.getSettings().setJavaScriptEnabled(true);

myWebView .loadUrl("url");

To get the javascript from a remote location see This solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.