Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript injection into webview

I know it exists a lot of questions about that but i don't understand why my following code does not work anymore

Here is my code :

private void init() {
    webview.setWebViewClient(new FormWebViewClient());
    webview.postUrl(url, EncodingUtils.getBytes(data, "BASE64"));
}

private class FormWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            // progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LOGD(TAG, "Url : " + url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:" +"document.getElementsByClassName('my_class_name')[0].value = '" + myValue + "';" +
    }
}

My original webview is overrided and it displays only myValue in the page instead of plenty of informations.

If anybody knows why i have this behavior ...

Thx


EDIT :

and the part of html

<input type="text" size="20" maxlength="19" autocomplete="off" name="CARD_NUMBER" id="CARD_NUMBER" class="my_class_name" value="">
like image 596
mrroboaat Avatar asked Dec 17 '14 10:12

mrroboaat


People also ask

How to inject JavaScript Android WebView?

Add a new method to inject javascript file. Call both methods: injectCSS() and injectJS() after page finishes loading. webView. setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { injectCSS(); injectJS(); super.

How do I inject JavaScript into WebView react native?

Use injectJavaScript as per (Guide > Communicating between JS and Native > The injectJavaScript method)[https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#the-injectjavascript-method"]. In your case, that would be something like this. webview. injectJavaScript(jsCode)

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.


1 Answers

Finally, I've found the answer :

I have to add void(0); at the end of JavaScript instruction like this :

view.loadUrl("javascript:" +"document.getElementsByClassName('my_class_name')[0].value = '" + myValue + "';void(0);")
like image 145
mrroboaat Avatar answered Oct 13 '22 06:10

mrroboaat