5

Injecting JavaScript into WebView does not work on Android 2.3.6

 2 years ago
source link: https://www.codesd.com/item/injecting-javascript-into-webview-does-not-work-on-android-2-3-6.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Injecting JavaScript into WebView does not work on Android 2.3.6

advertisements

the problem I'm having is injecting some JavaScript into a WebView. I only have this problem on Android version 2.3.6 and below I'm assuming, don't have a test device lower than that though. My code works fine in Android 4+ so I'm not quite sure why it's failing. It seems to "submit" the form but doesn't fill out the username and password field in 2.3.6 so it always fails. My main goal is to simulate a form fill-out and submit in a WebView with two EditTexts so the user doesn't have to interact with the WebView itself. When the user hits the login button it runs this code:

    //Set what needs to be filled out in the WebView
    String javaScript = "(function(){ " +
        "document.getElementById('user').value = '" + mUsername + "'; " +
        "document.getElementById('password').value = '" + mPassword + "'; " +
        "document.getElementById('form').submit(); " +
        "})()";
    //Load the javascript here
    mWebView.loadUrl("javascript: " + javaScript);

I'm also setting my WebView settings to this:

    //Setup the WebView options/settings
    ...
    mWebView.getSettings().setSaveFormData(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    ...

I've tried searching for any reason why this wouldn't work on 2.3.6 but haven't come up with anything. Is there some restriction in version 2.* that isn't present in 4+?

Edit: The button that runs the above code is only enabled once the page is finished loading. So when this button is clicked the above JS injection is run.

    @Override
    public void onPageFinished(WebView view, String url) {
        ...
        //Only allow the user to inject the JS when the page has loaded
        if(url.equals("...")
            button.setEnabled(true);
        ...
    }

SOLUTION: I'm not sure if this actually was the cause but changing the single quotes in the JS code to use double quotes seemed to make it run. Something like:

    String javaScript = ... + "document.getElementById(\"user\").value = \"" + mUsername + "\"; "


There aren't any restrictions that I'm aware of but it's not uncommon to encounter issues stemming from differing Javascript engines in various versions of Android. It is likely simply a matter of trying different syntax to achieve the same result in an attempt to hit upon a version of the logic that makes that version of Javascript happy.

Including JQuery and writing your function using Jquery syntax might be a solution, too. As JQuery itself seems pretty good at dealing with these differences.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK