javascript - Injecting JQuery into Android Webview -
i working on project translate webpage , displaying in android app. have done injecting javascript webview android using following code:
@override public void onpagefinished(webview view, string url) { string jap = "お問い合わせ"; string eng = "contact us"; mwebview.loadurl("javascript:(function(){" + "var jpf = '"+jap+"';" + "var jp = new regexp(jpf,'g');" + "var en = '"+eng+"';" + "document.body.innerhtml = " + "document.body.innerhtml.replace(jp,en);" + "})()"); }
however, ineffective method, because search each term , replace it. because of this, tried similar jquery's dictionary:
@override public void onpagefinished(webview view, string url) { mwebview.loadurl("javascript (function(){" + "var mydictionary = {" + "jp : {" + "'お問い合わせ' : 'contact us'" + "}" + "}" + "$.tr.dictionary(mydictionary);" + "$.tr.language('jp');" + "var tr = $.tr.translator();" + "alert(tr('お問い合わせ'))" + "})()"); }
this not seem work. please help.
note: planning fill dictionary more words one. first code, have made loop changes variables jap
, eng
, inefficient. chose not include code increase simplicity. thanks
straight source: https://developer.chrome.com/multidevice/webview/gettingstarted#enable_javascript
enable javascript
webviews don't allow javascript default. run web application in web view, need explicitly enable javascript adding following lines oncreate method:
// enable javascript websettings websettings = mwebview.getsettings(); websettings.setjavascriptenabled(true);
Comments
Post a Comment