Javascript injection into webview

半城伤御伤魂 提交于 2019-12-19 07:39:46

问题


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:


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);")


来源:https://stackoverflow.com/questions/27523040/javascript-injection-into-webview

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