Quiero obtener el código del elemento tocándolo en una vista web y enviando el código a mi aplicación. Lo logré, pero cada vez que hago clic en un elemento que tiene un pariente como un div, ambos se seleccionan.
ingrese la descripción de la imagen aquí
**Código Java**
final WebSettings settings = myWebView.getSettings(); settings.setJavaScriptEnabled(true); myWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); final String injectedJs = "javascript:(function(){" + injectedJs() + "})()"; myWebView.loadUrl(injectedJs); } }); myWebView.addJavascriptInterface( new Object() { @JavascriptInterface public void onClick(String param) { Toast.makeText(MainActivity.this, param, Toast.LENGTH_LONG).show(); } }, "appHost" ); myWebView.loadUrl("file:///android_asset/data.html");Código JavaScript
var lastSelectedItem = null; var showHighlighted = function(/* HTML element */view, /*boolean */highlighted) { if (view) { view.style.cssText = highlighted? 'background-color: #ADD8E6; ' :' '; } }; /* This new method, _addEventListener and _eventClick are the same as yours */ Object.prototype.each = function (fn, bind) { for (var i = 0; i < this.length; i++) { if (i in this) { fn.call(bind, this[i], i, this); } } }; var _addListener = document.addEventListener || document.attachEvent, _eventClick = window.addEventListener ? 'click' : 'onclick'; /* I changed the element selection criteria, but you can change it back easily. I am adding event listeners all the leaf elements in the DOM. */ var elements = document.body.getElementsByTagName('*'); elements.each(function (el) { _addListener.call(el, _eventClick, function () { /* First, deal with the previously selected item*/ showHighlighted(lastSelectedItem, false); /* Update the selected item reference */ lastSelectedItem = el; /* Finally, deal with the previously selected item*/ showHighlighted(lastSelectedItem, true); appHost.onClick(el.outerHTML.replace(el.innerHTML , ' ')); }, false); });Como puede ver en la imagen, hago clic en el botón y el código obtiene tanto el botón como el div. ¿Cómo puedo hacer que solo obtenga el botón, incluso si el botón tiene un pariente?