You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2019/01/10 09:30:28 UTC

[GitHub] ianhowe76 closed pull request #395: Android: Use evaluateJavascript execute script (not prompt)

ianhowe76 closed pull request #395: Android: Use evaluateJavascript execute script (not prompt)
URL: https://github.com/apache/cordova-plugin-inappbrowser/pull/395
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java
index 895b5436d..cdad01af9 100644
--- a/src/android/InAppBrowser.java
+++ b/src/android/InAppBrowser.java
@@ -73,6 +73,7 @@ Licensed to the Apache Software Foundation (ASF) under one
 import org.apache.cordova.LOG;
 import org.apache.cordova.PluginManager;
 import org.apache.cordova.PluginResult;
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -268,11 +269,15 @@ public void run() {
             });
         }
         else if (action.equals("injectScriptCode")) {
-            String jsWrapper = null;
-            if (args.getBoolean(1)) {
-                jsWrapper = String.format("(function(){prompt(JSON.stringify([eval(%%s)]), 'gap-iab://%s')})()", callbackContext.getCallbackId());
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && args.getBoolean(1)) {
+                runJavascriptWithResult(args.getString(0), callbackContext);
+            } else {
+                String jsWrapper = null;
+                if (args.getBoolean(1)) {
+                    jsWrapper = String.format("(function(){prompt(JSON.stringify([eval(%%s)]), 'gap-iab://%s')})()", callbackContext.getCallbackId());
+                }
+                injectDeferredObject(args.getString(0), jsWrapper);
             }
-            injectDeferredObject(args.getString(0), jsWrapper);
         }
         else if (action.equals("injectScriptFile")) {
             String jsWrapper;
@@ -411,6 +416,35 @@ public void run() {
         }
     }
 
+    private void runJavascriptWithResult(String scriptToInject, CallbackContext callbackContext) {
+        if (inAppWebView!=null) {
+            final String finalScriptToInject = scriptToInject;
+            final CallbackContext finalCallbackContext = callbackContext;
+            final String callbackId = callbackContext.getCallbackId();
+
+            this.cordova.getActivity().runOnUiThread(new Runnable() {
+                @SuppressLint("NewApi")
+                @Override
+                public void run() {
+                    inAppWebView.evaluateJavascript(finalScriptToInject, new ValueCallback<String>() {
+                        @Override
+                        public void onReceiveValue(String s) {
+                            PluginResult pluginResult;
+                            try {
+                                pluginResult = new PluginResult(PluginResult.Status.OK, new JSONArray("[" + s + "]"));
+                            } catch(JSONException e) {
+                                pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
+                            }
+                            finalCallbackContext.sendPluginResult(pluginResult);
+                        }
+                    });
+                }
+            });
+        } else {
+            LOG.d(LOG_TAG, "Can't inject code into the system browser");
+        }
+    }
+
     /**
      * Put the list of features into a hash map
      *
diff --git a/tests/tests.js b/tests/tests.js
index 1a0619d59..fc6602365 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -27,6 +27,9 @@ var isIos = cordova.platformId === 'ios';
 var isAndroid = cordova.platformId === 'android';
 var isBrowser = cordova.platformId === 'browser';
 
+var longStringLength = 10240 + 11;
+var longString = (new Array(longStringLength)).join('x');
+
 window.alert = window.alert || navigator.notification.alert;
 if (isWindows && navigator && navigator.notification && navigator.notification.alert) {
     // window.alert is defined but not functional on UWP
@@ -337,10 +340,10 @@ exports.defineManualTests = function (contentEl, createActionButton) {
                 var code = '(function(){\n' +
                   '    var header = document.getElementById("header");\n' +
                   '    header.innerHTML = "Script literal successfully injected";\n' +
-                  '    return "abc";\n' +
+                  '    return "' + longString + '";\n' +
                   '})()';
                 iab.executeScript({ code: code }, useCallback && function (results) {
-                    if (results && results.length === 1 && results[0] === 'abc') {
+                    if (results && results.length === 1 && results[0] === longString) {
                         alert('Results verified'); // eslint-disable-line no-undef
                     } else {
                         console.log(results);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org