0

I try to load this javascript in my webview:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);

    website = "https://www.blizz-z.de";
    myWebView = findViewById(R.id.blizzView);

    WebSettings settings = blizzView.getSettings();
    settings.setJavaScriptEnabled(true);

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        // Notify the host application that a page has finished loading.
        public void onPageFinished(WebView view, String url)
        {
            myWebView.loadUrl(
                    "javascript:(function() {" +
                        "setInterval(function() {" +
                            + "jQuery('#myInput').css('background', '#'+(Math.random()*0xFFFFFF<<0).toString(16));"
                        + "}, 1000);"
                    + "});"
            );
        }
    }

    ...

}

But it is not getting executed. If I execute the script in my desktop browser, then it works. It changes the background color of my search bar on my website just for test purposes.

Is setInterval not supported in webView?

Update:

I tried it with the function js from @mohkamfer's answer:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);

    website = "https://www.blizz-z.de";
    blizzView = findViewById(R.id.blizzView);

    WebSettings settings = blizzView.getSettings();
    settings.setJavaScriptEnabled(true);

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        // Notify the host application that a page has finished loading.
        public void onPageFinished(WebView view, String url)
        {
            js(myWebView, "(function() {" +
                        "setInterval(function() {" +
                            + "jQuery('#myInput').css('background', '#'+(Math.random()*0xFFFFFF<<0).toString(16));"
                        + "}, 1000);"
                    + "});"
            );
        }
    }

    ...

}

public void js(WebView view, String code)
{
    String javascriptCode = "javascript:" + code;
    if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(javascriptCode, new ValueCallback<String>() {

            @Override
            public void onReceiveValue(String response) {
                Log.i("debug_log", response);
            }
        });
    } else {
        view.loadUrl(javascriptCode);
    }
}

But it makes no difference.

2 Answers 2

1

Are you deploying on API 19 or higher? If so you'll have to use WebView#evalulateJavascript instead of WebView#loadUrl

I always use this method to simplify and quicken things a bit

public void js(String code) {
    if (Build.VERSION.SDK_INT >= 19) {
        this.evaluateJavascript(code, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String response) {
            }
        });
    } else {
        this.loadUrl("javascript:" + code);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I get cannot resolve Method "evaluteJavascript"
Are you sure you referenced it from myWebView instance instead of this?
I replaced this with "myWebView` now I get "Cannot resolve symbol ValueCallback"
I had to import android.webkit.ValueCallback
Nevermind, I figured out how to use your code, I had to add another parameter "WebView view" to your function. I tried it but the javascript code is not called.
|
0

I figured it out... I just had to call the code with jQuery(document).ready(function() {. The code was executed but did not changed anything because it was executed before the DOM was ready...

Solution:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen);

    website = "https://www.blizz-z.de";
    myWebView = findViewById(R.id.blizzView);

    WebSettings settings = blizzView.getSettings();
    settings.setJavaScriptEnabled(true);

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        // Notify the host application that a page has finished loading.
        public void onPageFinished(WebView view, String url)
        {
            js(myWebView, "jQuery(document).ready(function() {" +
                        "setInterval(function() {" +
                            + "jQuery('#myInput').css('background', '#'+(Math.random()*0xFFFFFF<<0).toString(16));"
                        + "}, 1000);"
                    + "});"
            );
        }
    }

    ...

}

public void js(WebView view, String code)
{
    String javascriptCode = "javascript:" + code;
    if (Build.VERSION.SDK_INT >= 19) {
        view.evaluateJavascript(javascriptCode, new ValueCallback<String>() {

            @Override
            public void onReceiveValue(String response) {
                Log.i("debug_log", response);
            }
        });
    } else {
        view.loadUrl(javascriptCode);
    }
}

Credits to @mohkamfer for contributing the js function.

2 Comments

That's great, as a rule of thumb always print/log a statement to make sure everything is working!
@mohkamfer, yes I was setting a breakpoint and debugged the app. But I thought the JS is not working at all because nothing happened even though your js function was entered and evaluateJavascript was called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.