public class MainActivity extends AppCompatActivity{
String TAG = "111232312312";
MyJavaScriptInterface MJInterface = new MyJavaScriptInterface();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView simpleWebView=(WebView) findViewById(R.id.wvBrowser);
simpleWebView.getSettings().setJavaScriptEnabled(true);
simpleWebView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36");
simpleWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, url+ " All the cookies in a string:" + cookies);
simpleWebView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
Log.d(TAG, "[[[" + MJInterface.htmlString + "]]]");
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Log.d(TAG, "ERROR");
super.onReceivedError(view, request, error);
}
});
simpleWebView.addJavascriptInterface(MJInterface, "HTMLOUT");
simpleWebView.loadUrl("https://www.youtube.com");
}
class MyJavaScriptInterface {
public String htmlString = "";
@JavascriptInterface
public void showHTML(String html) {
htmlString = html;
Pattern p = Pattern.compile("data-sitekey=(.*?)\"><");
Matcher m = p.matcher(htmlString);
while(m.find()) {
Log.d(TAG,m.group(0));
Log.d(TAG,m.group(1));
}
}
}
}
interface IMyListener {
public void showHtml(String html);
}
public IMyListener myListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListener = new IMyListener() {
@Override
public void showHtml(String html) {
Log.d(TAG, "showHtml: " + html);
}
};
final WebView simpleWebView=(WebView) findViewById(R.id.webview);
simpleWebView.getSettings().setJavaScriptEnabled(true);
....
}
class MyJavaScriptInterface {
public String htmlString = "";
@JavascriptInterface
public void showHTML(String html) {
htmlString = html;
Pattern p = Pattern.compile("data-sitekey=(.*?)\"><");
Matcher m = p.matcher(htmlString);
while (m.find()) {
myListener.showHtml(m.group(1));// <=================== вот здесь!
Log.d(TAG, m.group(0));
Log.d(TAG, m.group(1));
}
}
}