Created
April 5, 2020 03:45
-
-
Save plateaukao/a0aa25ae61381036f2360d457255e4b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
webView.setWebViewClient(new WebViewClient() { | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
injectCSS(); | |
injectJS(); | |
super.onPageFinished(view, url); | |
} | |
}); | |
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); | |
webView.loadUrl("javascript:(function() {" + | |
"var parent = document.getElementsByTagName('head').item(0);" + | |
"var style = document.createElement('style');" + | |
"style.type = 'text/css';" + | |
"style.innerHTML = window.atob('" + encoded + "');" + | |
"parent.appendChild(style)" + | |
"})()"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void injectJS() { | |
try { | |
InputStream inputStream = getAssets().open("jscript.js"); | |
byte[] buffer = new byte[inputStream.available()]; | |
inputStream.read(buffer); | |
inputStream.close(); | |
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP); | |
webView.loadUrl("javascript:(function() {" + | |
"var parent = document.getElementsByTagName('head').item(0);" + | |
"var script = document.createElement('script');" + | |
"script.type = 'text/javascript';" + | |
"script.innerHTML = window.atob('" + encoded + "');" + | |
"parent.appendChild(script)" + | |
"})()"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment