te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Detect if a specific button has been clicked in Android WebView - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if a specific button has been clicked in Android WebView - Stack Overflow

programmeradmin4浏览0评论

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.

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?

Thanks for your help!

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.

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?

Thanks for your help!

Share Improve this question asked Nov 15, 2016 at 21:55 Cédric PortmannCédric Portmann 1,0341 gold badge10 silver badges22 bronze badges 1
  • hi did you find the solution ? i need to do same..please let me know – Mohd Sher Khan Commented May 19, 2022 at 6:19
Add a ment  | 

2 Answers 2

Reset to default 8
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
   view.loadUrl("javascript:document.getElementById('buy_button').onclick = function(){alert('my message');}");
}
});

This tells your webview that when it is finished loading it needs to execute your custom javascript code. In this case the js code searches the page for element with id "buy button" and sets the onclick handler to alert your message.

If you specifically need to toast then you will need to setup a javascript interface and then call a method on their instead. make sense?

Let me provide you a full example. For your specific website of http://store.nike./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./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();
    }

}

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

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')

Info: the \n and the weird bination of strings inside the clickListener method are set like this, because of the IDE. ( ";" are causing problems inside a string).

发布评论

评论列表(0)

  1. 暂无评论