From 8ab07277cd9bf8aa81de89fcc1d54dde503c28e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar?= Date: Thu, 24 Nov 2016 18:50:49 +0100 Subject: [PATCH] CB-12184 executeScript leads to a null pointer on exception on Android. This closes #199 --- README.md | 8 +++--- src/android/InAppBrowser.java | 48 +++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 1e0645c..18aadd2 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i ## InAppBrowser.addEventListener -> Adds a listener for an event from the `InAppBrowser`. +> Adds a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`) ref.addEventListener(eventname, callback); @@ -338,7 +338,7 @@ function executeScriptCallBack(params) { ## InAppBrowser.removeEventListener -> Removes a listener for an event from the `InAppBrowser`. +> Removes a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`) ref.removeEventListener(eventname, callback); @@ -438,7 +438,7 @@ The function is passed an `InAppBrowserEvent` object. ## InAppBrowser.executeScript -> Injects JavaScript code into the `InAppBrowser` window +> Injects JavaScript code into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`) ref.executeScript(details, callback); @@ -480,7 +480,7 @@ Due to [MSDN docs](https://msdn.microsoft.com/en-us/library/windows.ui.xaml.cont ## InAppBrowser.insertCSS -> Injects CSS into the `InAppBrowser` window. +> Injects CSS into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`) ref.insertCSS(details, callback); diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 07ac31e..ef37fed 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -323,29 +323,33 @@ public class InAppBrowser extends CordovaPlugin { * which should be executed directly. */ private void injectDeferredObject(String source, String jsWrapper) { - String scriptToInject; - if (jsWrapper != null) { - org.json.JSONArray jsonEsc = new org.json.JSONArray(); - jsonEsc.put(source); - String jsonRepr = jsonEsc.toString(); - String jsonSourceString = jsonRepr.substring(1, jsonRepr.length()-1); - scriptToInject = String.format(jsWrapper, jsonSourceString); - } else { - scriptToInject = source; - } - final String finalScriptToInject = scriptToInject; - this.cordova.getActivity().runOnUiThread(new Runnable() { - @SuppressLint("NewApi") - @Override - public void run() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - // This action will have the side-effect of blurring the currently focused element - inAppWebView.loadUrl("javascript:" + finalScriptToInject); - } else { - inAppWebView.evaluateJavascript(finalScriptToInject, null); - } + if (inAppWebView!=null) { + String scriptToInject; + if (jsWrapper != null) { + org.json.JSONArray jsonEsc = new org.json.JSONArray(); + jsonEsc.put(source); + String jsonRepr = jsonEsc.toString(); + String jsonSourceString = jsonRepr.substring(1, jsonRepr.length()-1); + scriptToInject = String.format(jsWrapper, jsonSourceString); + } else { + scriptToInject = source; } - }); + final String finalScriptToInject = scriptToInject; + this.cordova.getActivity().runOnUiThread(new Runnable() { + @SuppressLint("NewApi") + @Override + public void run() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + // This action will have the side-effect of blurring the currently focused element + inAppWebView.loadUrl("javascript:" + finalScriptToInject); + } else { + inAppWebView.evaluateJavascript(finalScriptToInject, null); + } + } + }); + } else { + LOG.d(LOG_TAG, "Can't inject code into the system browser"); + } } /**