Skip to content

Instantly share code, notes, and snippets.

@robertocapah
Created November 26, 2019 06:49
Show Gist options
  • Save robertocapah/868387f0950bf6892a0339b28541525e to your computer and use it in GitHub Desktop.
Save robertocapah/868387f0950bf6892a0339b28541525e to your computer and use it in GitHub Desktop.
Intercept webview form
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_connect);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri callbackUri = Uri.parse(url);
String uid = callbackUri.getQueryParameter("uid");
if (uid != null) {
System.out.println("uid: " + uid);
Intent callbackIntent = new Intent(ConnectActivity.this, OauthCallbackActivity.class);
callbackIntent.putExtra("uid", uid);
startActivity(callbackIntent);
finish();
return false;
}
String urls = webView.getUrl();
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.evaluateJavascript(
"(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String html) {
Log.d("HTML", html);
// code here
}
});
super.onPageStarted(view, url, favicon);
}
@Override
public void onFormResubmission(WebView view, Message dontResend, Message resend) {
super.onFormResubmission(view, dontResend, resend);
dontResend.getData();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
getJs(view);
}
});
webView.addJavascriptInterface(this,"KAMPRET");
webView.loadUrl(Constants.AUTHORIZE_URL);
}
void getJs(WebView web){
String a ="(function () { " +
"document.getElementsByClassName('btn btn-primary')[0].addEventListener('click', function(){ " +
" var empID = document.getElementById('empID').value;" +
" var pass = document.getElementById('Password').value;" +
"KAMPRET.action(empID + '<==>'+ pass); } )"+
"})()";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// web?.evaluateJavascript(function, ValueCallback { value: String -> Log.d("=/=>", value) })
web.evaluateJavascript(a,null);
}
}
@JavascriptInterface
public void action(String str) {
Log.d("==>", str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment