WebView中单击了特定按钮

WebView中单击了特定按钮

本文介绍了检测是否在Android WebView中单击了特定按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个android应用.这个应用程式包含一个WebView,并显示我不拥有的网站.(例如,WebStore)

I am currently working on an android app. This app consists of a WebView and displays a website which I don't own. (F.e. a WebStore)

该应用程序应该能够识别/检测用户何时单击此网站上的特定按钮(例如购买产品"按钮).如果用户单击此类按钮,则该应用程序将弹出一个吐司.

The app is supposed to recognize/detect when a user clicks a specific button on this website (f.e a "Buy Product" button). If a user clicks such a button, then the app makes a toast pop up.

我知道每个网站的情况都会有所不同,但是我将如何对点击检测进行总体编程?我读了一些有关注入JavaScript等的文章,但是我真的不明白这应该如何工作.有没有办法说:如果用户单击id = buy_button的按钮,则显示吐司?

I know this will be different for every website, however how would I program the click detection in general? I read a few articles about injecting JavaScript etc, however I dont really understand how this is supposed to work. Is there a way to say: If a user clicks on the button with id=buy_button, then show toast?

感谢您的帮助!

推荐答案

让我为您提供完整的示例.对于您的 http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-craft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158

Let me provide you a full example.For your specific website of http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-craft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158

{
    ...

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(this), "ButtonRecognizer");

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            loadEvent(clickListener());
        }

        private void loadEvent(String javascript){
            webview.loadUrl("javascript:"+javascript);
        }

        private String clickListener(){
            return getButtons()+ "for(var i = 0; i < buttons.length; i++){\n" +
                    "\tbuttons[i].onclick = function(){ console.log('click worked.'); ButtonRecognizer.boundMethod('button clicked'); };\n" +
                    "}";
        }

        private String getButtons(){
            return "var buttons = document.getElementsByClassName('add-to-cart'); console.log(buttons.length + ' buttons');\n";
        }
    });

    webview.loadUrl("http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-cra\u200C\u200Bft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158");

    ...
}


class MyJavaScriptInterface {

    private Context ctx;

    MyJavaScriptInterface(Context ctx) {
        this.ctx = ctx;
    }

    @JavascriptInterface
    public void boundMethod(String html) {
        new AlertDialog.Builder(ctx).setTitle("HTML").setMessage("It worked")
                .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
    }

}

这会将按钮的onClick更改为所需的内容.

That'll change the onClick for the button to what you need.

对于其他所有人,似乎存在与getElementById()有关的特定页面问题或与Android有关的问题,但是按类获取元素(getElementsByClassName())的工作符合预期.此外,可能有必要将getElementsByClassName()替换为getElementsByName(),例如在以下网站上: https://www.digitec.ch/de/s1/product/lexon-flip-wecker-3522142 .您将把getElementsByName('AddProductToCart')放在哪里的

For everyone else, it looks like there's either a page-specific issue with getElementById() or an Android issue, but getting the elements by class (getElementsByClassName()) worked as expected.Furthermore, it might be necessary to replace getElementsByClassName() by getElementsByName() such as for example on this website: https://www.digitec.ch/de/s1/product/lexon-flip-wecker-3522142.Where you would put getElementsByName('AddProductToCart')

信息:由于IDE的缘故,设置了\ n和clickListener方法中的字符串的怪异组合.(;"在字符串内部引起问题).

这篇关于检测是否在Android WebView中单击了特定按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:40