Javascript injection into webview

2020-03-23 17:55发布

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="">

1条回答
男人必须洒脱
2楼-- · 2020-03-23 18:22

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);")
查看更多
登录 后发表回答