Skip to content

Commit

Permalink
Wait for Window to Load Before Executing Javascript Callback.
Browse files Browse the repository at this point in the history
Patched from v4 commit: braintree@4bff178
Resolves: braintree#26
  • Loading branch information
dpa99c committed Aug 6, 2021
1 parent 0a0f282 commit ba88771
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,20 @@ public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, U

if (result == BrowserSwitchResult.CANCELED) {
runJavaScriptInWebView(""
+ "if (typeof window.popupBridge.onCancel === 'function') {"
+ " window.popupBridge.onCancel();"
+ "function notifyCanceled() {"
+ " if (typeof window.popupBridge.onCancel === 'function') {"
+ " window.popupBridge.onCancel();"
+ " } else {"
+ " window.popupBridge.onComplete(null, null);"
+ " }"
+ "}"
+ ""
+ "if (document.readyState === 'complete') {"
+ " notifyCanceled();"
+ "} else {"
+ " window.popupBridge.onComplete(null, null);"
+ " window.addEventListener('load', function () {"
+ " notifyCanceled();"
+ " });"
+ "}");
return;
} else if (result == BrowserSwitchResult.OK) {
Expand Down Expand Up @@ -147,7 +157,20 @@ public void onBrowserSwitchResult(int requestCode, BrowserSwitchResult result, U
error = "new Error('" + result.getErrorMessage() + "')";
}

runJavaScriptInWebView(String.format("window.popupBridge.onComplete(%s, %s);", error, payload));
String successJavascript = String.format(""
+ "function notifyComplete() {"
+ " window.popupBridge.onComplete(%s, %s);"
+ "}"
+ ""
+ "if (document.readyState === 'complete') {"
+ " notifyComplete();"
+ "} else {"
+ " window.addEventListener('load', function () {"
+ " notifyComplete();"
+ " });"
+ "}", error, payload);

runJavaScriptInWebView(successJavascript);
}

@Override
Expand Down

0 comments on commit ba88771

Please sign in to comment.