Skip to content

Commit

Permalink
Use evaluateJavascript for getting javascript result for Android reso…
Browse files Browse the repository at this point in the history
…lves apache#303

Replaces the prompt interface for executeScript

fix(InAppBrowser): Add missing import
  • Loading branch information
ianhowe76 authored and deanylev committed Nov 18, 2024
1 parent 10996f8 commit 5d27087
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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;

Expand Down Expand Up @@ -281,11 +282,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;
Expand Down Expand Up @@ -423,6 +428,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
*
Expand Down

0 comments on commit 5d27087

Please sign in to comment.