Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-5756

InAppBrowser example fails on Android 4.4

    XMLWordPrintableJSON

Details

    Description

      1. Create cordova project and add InAppBrowser plugin
      2. Replace index.html contents with the Full Example for executeScript in the API docs at http://cordova.apache.org/docs/en/3.3.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser
      3. Build run the application.
      4. When the InAppBrowser loads the it displays the string http://cordova.apache.org/images/cordova_bot.png.

      The example is suppose to load the Apache site and replace the logo with the cordova bot. If you open the AndroidManifest.xml and change the targetSdkVersion from 19 to 18 it works as expected.

      The problems seems to be caused by a change in behaviour in 4.4 with using loadUrl("javascript:..."). See the loadUrl method in https://android.googlesource.com/platform/frameworks/webview/+/android-4.4_r1.1/chromium/java/com/android/webview/chromium/WebViewChromium.java. If the target sdk is kitkat the result of the JS Url replaces the content of the current page...

      The fix seems to be to use a new 4.4 method to the webview called evaluateJavaScript.
      http://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript(java.lang.String,%20android.webkit.ValueCallback<java.lang.String>)

      In InAppBrowser.java:

      Change this:

      final String finalScriptToInject = scriptToInject;
              // This action will have the side-effect of blurring the currently focused element
              this.cordova.getActivity().runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      inAppWebView.loadUrl("javascript:" + finalScriptToInject);
                  }
              });
       

      To:

              final String finalScriptToInject = scriptToInject;
              
              	// This action will have the side-effect of blurring the currently focused element
              	this.cordova.getActivity().runOnUiThread(new Runnable() {
                  	@Override
                  	public void run() {
                  		if (cordova.getActivity().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.KITKAT)
                  		{
                  			inAppWebView.loadUrl("javascript:" + finalScriptToInject);
                  		} else {
                  			inAppWebView.evaluateJavascript(finalScriptToInject, null);
                  		}
                  	}
              	});
      

      I haven't tried any other plugins but it might be neccessary to change other plugins to use evaluateJavascript instead of loadUrl("javascript:...").

      Attachments

        Activity

          People

            iclelland Ian Clelland
            mpridham Marcus Pridham
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: