0

let's say that i have google page loaded in webview, then i searched for something,

So the page changed, and there's a text in this page that i want to get it in my android app, in textView by using the code below

    webview.loadUrl("javascript:api.getSessionID(document.querySelector(bla bla bla)");

class JSInterface {
        @JavascriptInterface

        public void getSessionID(String id) {

            Toast.makeText(context,id, Toast.LENGTH_LONG).show();
        }

How can i inject this js in the shown page in webview, not in the page that loaded first in webview which is google search page

Google just an example, i'm trying to save a session id in sharedPreferences when a user loggin in the account, which is the second page

Note: i have all the necessary codes in my full code in order to inject js and get string from it

Note2: if i reloaded the page, i will sign out

Appreciate any help !

M J
  • 174
  • 1
  • 12

2 Answers2

2

your WebView first of all, we must tell the WebView to enable the JavaScript execution:

webview.getSettings().setJavaScriptEnabled(true);

Then we must set the WebViewClient in order to receive the onPageFinished event.

webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPag`enter code here`eFinished(view, url);
        webView.loadUrl(
            "javascript:(function() { " +
                "var element = document.getElementById('hplogo');"
                + "element.parentNode.removeChild(element);" +
            "})()");
    }
});
  • Thank you for your answer, i didn't put the full code in my question, i was asking where to put the `webView.loadUrl( "javascript:(function()` to be injected in the next page, i have everything working in webview,onPageFinished and settings... – M J Jul 15 '19 at 05:31
  • @MJ in `onPageFinished` first check whether it is *next page*. – Vladyslav Matviienko Jul 15 '19 at 05:33
  • `onPageFinished` will run when the url in webview fully loaded, but what i'm asking is when i enter another page and i like to get a string from that web page, how can i do that ? – M J Jul 15 '19 at 05:35
  • @MJ, what does `enter another page` mean? When you start loading another page? – Vladyslav Matviienko Jul 15 '19 at 05:38
  • the first page is login page [check website](https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?ident=cin) that will loadUrl in the webview, after the user login, it will redirect to the next home page, which have a session id in it, that i get it using js in chrome console, but, i don't know how to inject that code in that home page – M J Jul 15 '19 at 05:42
0

To acheive this you can evaluate javascript as,

webview.evaluateJavascript("Your JS here which will return session id",
                    html1 -> Log.i("HTML", html1));//session id will be printed

Also, make sure you have enabled java script to run in webview webView.getSettings().setJavaScriptEnabled(true);

EDIT

Second approach, If you want to implement an interface which will give callback do change in your webpage as,

if (typeof YourListener != 'undefined')//check if yourlistner is undefined
                yourListener.onTextChange('theReturnValue');
            });

In above code the interface is called from webpage same as we do in android. Just apply JS at 'theReturnValue' here you need to retun required value from webpage.

And then add to webview that interface as,

webview.addJavascriptInterface(new WebAppInterface(getActivity(), webview), "YourListener");

also, add interface as,

public class WebAppInterface {

    Context mContext;
    WebView mView;

    /**
     * Instantiate the interface and set the context
     */
    WebAppInterface(Context c, WebView w) {
        mContext = c;
        mView = w;
    }

    /**
     * Show a toast from the web page
     */
    @JavascriptInterface
    public void onTextChange(String html) {
        //you will get the callback text here
    }
}
niks
  • 173
  • 10
  • If it's not working let me know. Do you have access to html webpage to make changes? – niks Jul 15 '19 at 05:21
  • Thank you niks for your answer, but i didn't get it, so, what `html1 -> Log.i("HTML", html1)` do ? I think you didn't understand my question, i know how to get a string from js injected code, but i'm looking for injecting a js code in a page shown in the webview, – M J Jul 15 '19 at 05:28
  • ohh ok do you have access to HTML webpage? – niks Jul 15 '19 at 05:35
  • No unfortunately – M J Jul 15 '19 at 05:35
  • So I think this is the way you can inject JS and HTML is just the output of your JS. So in your case, if on the browser your JS returns a session ID then it will return the same by running above code. which then you can set to text view. – niks Jul 15 '19 at 05:43
  • where i will put your code in order to inject in the needed webpage shown in webview ? not in the first page loaded in a webview – M J Jul 15 '19 at 05:47
  • Yes so in that case you need to have access and make changes in HTML please check my edit if it solves your problem. – niks Jul 15 '19 at 05:50
  • can you please explain to me this part `if (typeof YourListener != 'undefined') yourListener.onTextChange('the return value'); });` – M J Jul 15 '19 at 05:57
  • please check this https://stackoverflow.com/a/23827672/1557563 where you will get which URL is loaded and current page. if your current page is Homepage just apply the script it will give you session id. The evaluateJavascript run exactly the same as when you run a script in chrome console. – niks Jul 15 '19 at 05:58
  • looks like you didn't understand my question, thank you anyway – M J Jul 15 '19 at 06:03
  • updated the comment basically it's just simple interface which will return the value which you want. In your case, you should write js there and return the session id. – niks Jul 15 '19 at 06:03
  • well, what I understood is you need session id from the page which is loaded after a successfull login. And if I miss understood Sorry for that. :) – niks Jul 15 '19 at 06:07
  • that's exactly what i need, but your answers indicates that you didn't understand the question, anyway, if you can help by explain the way to do that without puting alot of codes that's will help, thanks – M J Jul 15 '19 at 06:16
  • Well, See the first thing is you don't have access to HTML so my second approach will not work. But as per my knowledge, the first answer should solve your problem if in onPageFinished method you check if URL is the afterLoginPage URL and then evaluate the JS to get the session id. that's all I can suggest to you for now. – niks Jul 15 '19 at 06:28