

Injecting JavaScript into WebView does not work on Android 2.3.6
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
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.
Recommend
-
99
-
66
ASP.NET Core comes with a thin but rich and powerful built-in dependency injection mechanism we can use to inject instances to controllers and some other parts of web applications. Although constructor injection is the mo...
-
45
r/phishing: Phishing - Questions about Phishing scams, reporting Phishing, and general discussion.
-
14
Injecting Machine Learning And Bayesian Optimization Into HPC
-
11
Injecting Files into an Archive Format I Don’t Understand Somewhat recently I was a part of the group re...
-
7
Injecting Additional Data into Laravel Queued Jobs laravel tips...
-
7
最近要对一个网页的源代码进行检测,Android Webview中没有直接获取网页源代码的接口,传统的addJavascriptInterface方法存在安全隐患,所以研究了一下Java和Javascript的安全交互。 Android Webview漏洞 Android Webview有两个非...
-
6
Injecting JavaScript Into Web View In iOS Using a web view to display some frequently updated content such as FAQ is a very common tactic used by most iOS developers. By doing so, developers can easily update the web view...
-
3
Party time: Injecting code into Teleparty extension Teleparty, formerly called Netflix Party, is a wildly popular browser ext...
-
5
You’ll be injecting robots into your bloodstream to fight disease soon ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK