Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ protected void createRNCWebViewBridge(RNCWebView webView) {
this.bridgeListener = new WebViewCompat.WebMessageListener() {
@Override
public void onPostMessage(@NonNull WebView view, @NonNull WebMessageCompat message, @NonNull Uri sourceOrigin, boolean isMainFrame, @NonNull JavaScriptReplyProxy replyProxy) {
RNCWebView.this.onMessage(message.getData(), sourceOrigin.toString(), isMainFrame);
RNCWebView.this.onMessage(message.getData(), sourceOrigin.toString(), isMainFrame, view.getUrl());
}
};
WebViewCompat.addWebMessageListener(
Expand Down Expand Up @@ -345,10 +345,14 @@ public void setInjectedJavaScriptObject(String obj) {
}

public void onMessage(String message, String sourceUrl) {
onMessage(message, sourceUrl, null);
onMessage(message, sourceUrl, null, null);
}

public void onMessage(String message, String sourceUrl, @Nullable Boolean isMainFrame) {
onMessage(message, sourceUrl, isMainFrame, null);
}

public void onMessage(String message, String sourceUrl, @Nullable Boolean isMainFrame, @Nullable String topFrameUrl) {
ThemedReactContext reactContext = getThemedReactContext();
RNCWebView mWebView = this;

Expand All @@ -365,6 +369,9 @@ public void run() {
if (isMainFrame != null) {
data.putBoolean("isMainFrame", isMainFrame);
}
if (topFrameUrl != null) {
data.putString("topFrameUrl", topFrameUrl);
}

if (mMessagingJSModule != null) {
dispatchDirectMessage(data);
Expand All @@ -379,6 +386,9 @@ public void run() {
if (isMainFrame != null) {
eventData.putBoolean("isMainFrame", isMainFrame);
}
if (topFrameUrl != null) {
eventData.putString("topFrameUrl", topFrameUrl);
}

if (mMessagingJSModule != null) {
dispatchDirectMessage(eventData);
Expand Down Expand Up @@ -476,7 +486,7 @@ protected class RNCWebViewBridge {
public void postMessage(String message) {
if (mWebView.getMessagingEnabled()) {
// Post to main thread because `mWebView.getUrl()` requires to be executed on main.
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl()));
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl(), null, mWebView.getUrl()));
} else {
FLog.w(TAG, "ReactNativeWebView.postMessage method was called but messaging is disabled. Pass an onMessage handler to the WebView.");
}
Expand All @@ -495,4 +505,4 @@ public boolean isWaitingForCommandLoadUrl() {
return waitingForCommandLoadUrl;
}
}
}
}
5 changes: 4 additions & 1 deletion apple/RNCWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ - (instancetype)initWithFrame:(CGRect)frame
_view.onMessage = [self](NSDictionary* dictionary) {
if (_eventEmitter) {
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
id topFrameUrlValue = [dictionary valueForKey:@"topFrameUrl"];
NSString *topFrameUrl = [topFrameUrlValue isKindOfClass:[NSString class]] ? topFrameUrlValue : @"";
facebook::react::RNCWebViewEventEmitter::OnMessage data = {
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
Expand All @@ -135,7 +137,8 @@ - (instancetype)initWithFrame:(CGRect)frame
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
.data = std::string([[dictionary valueForKey:@"data"] UTF8String]),
.isMainFrame = static_cast<bool>([[dictionary valueForKey:@"isMainFrame"] boolValue])
.isMainFrame = static_cast<bool>([[dictionary valueForKey:@"isMainFrame"] boolValue]),
.topFrameUrl = std::string([topFrameUrl UTF8String], [topFrameUrl lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
};
webViewEventEmitter->onMessage(data);
}
Expand Down
1 change: 1 addition & 0 deletions apple/RNCWebViewImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ - (void)userContentController:(WKUserContentController *)userContentController
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{@"data": message.body}];
[event addEntriesFromDictionary: @{@"url": message.frameInfo.request.URL.absoluteString}];
[event addEntriesFromDictionary: @{@"topFrameUrl": _webView.URL.absoluteString ?: @""}];
[event addEntriesFromDictionary: @{@"isMainFrame": @(isMainFrame)}];
_onMessage(event);
}
Expand Down
1 change: 1 addition & 0 deletions src/RNCWebViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type WebViewMessageEvent = Readonly<{
lockIdentifier: Double;
data: string;
isMainFrame?: boolean;
topFrameUrl?: string;
}>;
export type WebViewOpenWindowEvent = Readonly<{
targetUrl: string;
Expand Down
1 change: 1 addition & 0 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export type DecelerationRateConstant = 'normal' | 'fast';
export interface WebViewMessage extends WebViewNativeEvent {
data: string;
isMainFrame?: boolean;
topFrameUrl?: string;
}

export interface WebViewError extends WebViewNativeEvent {
Expand Down
Loading