I would like to preemptively get the HTML code of a webpage that is to be loaded in a webView
, parse it using regex, and display only the HTML code that I want, while letting the webpage still think it has loaded everything.
Is there any way to do that in the WebViewClient.onLoadResource()
or similar methods?
EDIT: I tried this:
class MyJavaScriptInterface
{
@SuppressWarnings("unused")
public void showHTML(String html, Context context)
{
new AlertDialog.Builder(context)
.setTitle("HTML")
.setMessage(html)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.create();
pageHTML = html;
}
}
@Override
public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
webview.getSettings().setJavaScriptEnabled(true);
MyJavaScriptInterface interfaceA = new MyJavaScriptInterface();
webview.addJavascriptInterface(interfaceA, "HTMLOUT");
WebViewClient anchorWebViewClient = new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
webview.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
Pattern pattern = Pattern.compile("<h2>Winning Sc.+</h2></div>(.+)<br>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
matcher.find();
The interface is never called