When trying to call the MyApp_HighlightAllOccurencesOfString () function from a WebView

String textToSearch = editTextSearchInText.getText().toString(); webSettings.setJavaScriptEnabled(true); article_web_view.evaluateJavascript("MyApp_HighlightAllOccurencesOfString('" + textToSearch + "')", new ValueCallback<String>() { @Override public void onReceiveValue(String s) { Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show(); } }); 

It returns null, and in the console the following:

 "Uncaught ReferenceError: MyApp_HighlightAllOccurencesOfString is not defined", source: (1) 

Tell me, please, how to correctly call the JS function from WebView? Why does the function undefined error occur? JS code from WebView below:

 <script > var MyApp_SearchResultCount = 0; function MyApp_HighlightAllOccurencesOfStringForElement (element, keyword){ if (element) { if (element.nodeType == 3) { while (true) { var value = element.nodeValue; var idx = value.toLowerCase().indexOf(keyword); if (idx < 0) break; var span = document.createElement("span"); var text = document.createTextNode(value.substr(idx, keyword.length)); span.appendChild(text); span.setAttribute("class", "MyAppHighlight"); span.style.backgroundColor = "#C5DFEA"; text = document.createTextNode(value.substr(idx + keyword.length)); element.deleteData(idx, value.length - idx); var next = element.nextSibling; element.parentNode.insertBefore(span, next); element.parentNode.insertBefore(text, next); element = text; window.scrollTo(0, findPos(span)); MyApp_SearchResultCount++; } } else if (element.nodeType == 1) { if (element.style.display != "none" && element.nodeName.toLowerCase() != "select") { for (var i = element.childNodes.length - 1; i >= 0; i--) { MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i], keyword); } } } } } function findPos (obj) {var curtop = -100; if (obj.offsetParent) { do { curtop += obj.offsetTop; } while (obj = obj.offsetParent); return[curtop]; }} function MyApp_HighlightAllOccurencesOfString (keyword) {MyApp_RemoveAllHighlights(); MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());}function MyApp_RemoveAllHighlightsForElement (element) { if (element) { if (element.nodeType == 1) { if (element.getAttribute("class") == "MyAppHighlight") { var text = element.removeChild(element.firstChild); element.parentNode.insertBefore(text, element); element.parentNode.removeChild(element); return true; } else { var normalize = false; for (var i = element.childNodes.length - 1; i >= 0; i--) { if (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) { normalize = true; } } if (normalize) { element.normalize(); } } } } return false;} function MyApp_RemoveAllHighlights () { MyApp_SearchResultCount = 0; MyApp_RemoveAllHighlightsForElement(document.body); } function findImage (x, y){ return document.elementFromPoint(x, y).src; }</script > 

    1 answer 1

    If someone faces a similar problem, the solution was suggested here:

    https://stackoverflow.com/questions/42656305/calling-the-js-function-from-webview-produces-an-error-function-is-not-defined/42656992#42656992