@@ -49,6 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4949import android .view .inputmethod .EditorInfo ;
5050import android .view .inputmethod .InputMethodManager ;
5151import android .webkit .CookieManager ;
52+ import android .webkit .JavascriptInterface ;
5253import android .webkit .ValueCallback ;
5354import android .webkit .WebChromeClient ;
5455import android .webkit .WebSettings ;
@@ -72,7 +73,6 @@ Licensed to the Apache Software Foundation (ASF) under one
7273import org .apache .cordova .PluginManager ;
7374import org .apache .cordova .PluginResult ;
7475import org .apache .cordova .Whitelist ;
75- import org .apache .cordova .inappbrowser .InAppBrowser ;
7676import org .json .JSONException ;
7777import org .json .JSONObject ;
7878
@@ -94,6 +94,7 @@ public class ThemeableBrowser extends CordovaPlugin {
9494 private static final String LOAD_START_EVENT = "loadstart" ;
9595 private static final String LOAD_STOP_EVENT = "loadstop" ;
9696 private static final String LOAD_ERROR_EVENT = "loaderror" ;
97+ private static final String MESSAGE_EVENT = "message" ;
9798
9899 private static final String ALIGN_LEFT = "left" ;
99100 private static final String ALIGN_RIGHT = "right" ;
@@ -838,6 +839,24 @@ public void onPageFinished(String url, boolean canGoBack, boolean canGoForward)
838839 settings .setDisplayZoomControls (false );
839840 settings .setPluginState (android .webkit .WebSettings .PluginState .ON );
840841
842+ // Add JS interface
843+ class JsObject {
844+ @ JavascriptInterface
845+ public void postMessage (String data ) {
846+ try {
847+ JSONObject obj = new JSONObject ();
848+ obj .put ("type" , MESSAGE_EVENT );
849+ obj .put ("data" , new JSONObject (data ));
850+ sendUpdate (obj , true );
851+ } catch (JSONException ex ) {
852+ LOG .e (LOG_TAG , "data object passed to postMessage has caused a JSON error." );
853+ }
854+ }
855+ }
856+ if (Build .VERSION .SDK_INT >= 17 ){
857+ inAppWebView .addJavascriptInterface (new JsObject (), "cordova_iab" );
858+ }
859+
841860 //Toggle whether this is enabled or not!
842861 Bundle appSettings = cordova .getActivity ().getIntent ().getExtras ();
843862 boolean enableDatabase = appSettings == null || appSettings .getBoolean ("ThemeableBrowserStorageEnabled" , true );
@@ -1396,6 +1415,11 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
13961415 public void onPageFinished (WebView view , String url ) {
13971416 super .onPageFinished (view , url );
13981417
1418+ // Alias the iOS webkit namespace for postMessage()
1419+ if (Build .VERSION .SDK_INT >= 17 ){
1420+ injectDeferredObject ("window.webkit={messageHandlers:{cordova_iab:cordova_iab}}" , null );
1421+ }
1422+
13991423 try {
14001424 JSONObject obj = new JSONObject ();
14011425 obj .put ("type" , LOAD_STOP_EVENT );
0 commit comments