Skip to content

Commit a543f59

Browse files
committed
Add Android support for postMessage API
1 parent 25756f5 commit a543f59

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/android/ThemeableBrowser.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4949
import android.view.inputmethod.EditorInfo;
5050
import android.view.inputmethod.InputMethodManager;
5151
import android.webkit.CookieManager;
52+
import android.webkit.JavascriptInterface;
5253
import android.webkit.ValueCallback;
5354
import android.webkit.WebChromeClient;
5455
import android.webkit.WebSettings;
@@ -72,7 +73,6 @@ Licensed to the Apache Software Foundation (ASF) under one
7273
import org.apache.cordova.PluginManager;
7374
import org.apache.cordova.PluginResult;
7475
import org.apache.cordova.Whitelist;
75-
import org.apache.cordova.inappbrowser.InAppBrowser;
7676
import org.json.JSONException;
7777
import 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);

www/themeablebrowser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ var modulemapper = require('cordova/modulemapper');
2525
var urlutil = require('cordova/urlutil');
2626

2727
function ThemeableBrowser() {
28-
this.channels = {};
28+
this.channels = {
29+
'loadstart': channel.create('loadstart'),
30+
'loadstop' : channel.create('loadstop'),
31+
'loaderror' : channel.create('loaderror'),
32+
'exit' : channel.create('exit'),
33+
'message' : channel.create('message')
34+
};
2935
}
3036

3137
ThemeableBrowser.prototype = {

0 commit comments

Comments
 (0)