Inject Multiple JavaScript to Multuple Page of WebView Android -
say, set webview example.com clicked on page , load example.com/about. in webview i've injected javascript modify html on example.com, , worked, , problem is, how inject javascript modify example.com/about. mean how inject multiple javascript multiple page of webview?
public class mainactivity extends appcompatactivity { webview mwebview; private int getscale() { display display = ((windowmanager) getsystemservice(context.window_service)).getdefaultdisplay(); int width = display.getwidth(); double val = new double(width) / new double(100); val = val * 100d; return val.intvalue(); } @override protected void oncreate(bundle savedinstancestate) { this.requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mwebview = (webview) findviewbyid(r.id.activity_main_webview); // enable javascript websettings websettings = mwebview.getsettings(); mwebview.setwebviewclient(new webviewclient()); websettings.setjavascriptenabled(true); websettings.setallowuniversalaccessfromfileurls(true); mwebview.setwebviewclient(new webviewclient()); mwebview.setwebviewclient(new webviewclient() { @override public void onpagefinished(webview view, string url) { // inject css when page done loading injectcss(); mwebview.loadurl("javascript:var greeting = function (name) {\n" + " console.log(\"great see you,\" + \" \" + name);\n" + "};"); super.onpagefinished(view, url); //inject js edit html injectscriptfile(view, "script.js"); // see below ... // test if script loaded view.loadurl("javascript:settimeout(test(), 500)"); } private void injectscriptfile(webview view, string scriptfile) { inputstream input; try { input = getassets().open(scriptfile); byte[] buffer = new byte[input.available()]; input.read(buffer); input.close(); // string-ify script byte-array using base64 encoding !!! string encoded = base64.encodetostring(buffer, base64.no_wrap); view.loadurl("javascript:(function() {" + "var parent = document.getelementsbytagname('head').item(0);" + "var script = document.createelement('script');" + "script.type = 'text/javascript';" + // tell browser base64-decode string script !!! "script.innerhtml = window.atob('" + encoded + "');" + "parent.appendchild(script)" + "})()"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return false; } }); mwebview.loadurl("example.com"); mwebview.getsettings().setbuiltinzoomcontrols(true); mwebview.getsettings().setdisplayzoomcontrols(false); } private void injectcss() { try { inputstream inputstream = getassets().open("style.css"); byte[] buffer = new byte[inputstream.available()]; inputstream.read(buffer); inputstream.close(); string encoded = base64.encodetostring(buffer, base64.no_wrap); mwebview.loadurl("javascript:(function() {" + "var parent = document.getelementsbytagname('head').item(0);" + "var style = document.createelement('style');" + "style.type = 'text/css';" + // tell browser base64-decode string script !!! "style.innerhtml = window.atob('" + encoded + "');" + "parent.appendchild(style)" + "})()"); } catch (exception e) { e.printstacktrace(); } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); mwebview.savestate(outstate); } @override protected void onrestoreinstancestate(bundle savedinstancestate) { super.onrestoreinstancestate(savedinstancestate); mwebview.restorestate(savedinstancestate); } @override public boolean oncreateoptionsmenu(menu menu) { super.oncreateoptionsmenu(menu); // add menu items, second value id, use in oncreateoptionsmenu menu.add(0, 1, 0, "back"); menu.add(0, 2, 0, "refresh"); menu.add(0, 3, 0, "forward"); getmenuinflater().inflate(r.menu.menu_main, menu); return true; // end of menu configuration } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case 3: //if id equals 3 , go forward mwebview.cangoforward(); item.seticon(r.drawable.forward); return true; } return super.onoptionsitemselected(item); } @override public boolean onkeydown(int keycode, keyevent event) { // enables browsing previous pages hardware button if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { // check if key event key , if there's history mwebview.goback(); return true; } // if wasn't key or there's no web page history, bubble default // system behavior (probably exit activity) return super.onkeydown(keycode, event); } }
Comments
Post a Comment