Y
Y
ywitodenasuby2018-10-22 17:22:09
Android
ywitodenasuby, 2018-10-22 17:22:09

How to pass result from JavascriptInterface to OnCreate?

I'm trying to get the html code of the page loaded in the WebView. Everything is fine, but I don't understand how to pass code from JavascriptInterface to OnCreate. Please do not kick much, I'm a beginner.

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, "[]");
            }
            @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));
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bask, 2018-10-23
@ywitodenasuby

Lots of options. One of them is to use events.
Define an interface:

interface IMyListener {
        public void showHtml(String html);
    }

    public IMyListener myListener;

Then in onCreate you define it:
@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);
....
}

And further, in the MyJavaScriptInterface in the showHTML method, you pass html as a parameter:
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));
            }
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question