Skip to content

Commit fa84f40

Browse files
authored
send top frame URL with iframe events (#37)
1 parent 33f960b commit fa84f40

5 files changed

Lines changed: 21 additions & 5 deletions

File tree

android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ protected void createRNCWebViewBridge(RNCWebView webView) {
263263
this.bridgeListener = new WebViewCompat.WebMessageListener() {
264264
@Override
265265
public void onPostMessage(@NonNull WebView view, @NonNull WebMessageCompat message, @NonNull Uri sourceOrigin, boolean isMainFrame, @NonNull JavaScriptReplyProxy replyProxy) {
266-
RNCWebView.this.onMessage(message.getData(), sourceOrigin.toString(), isMainFrame);
266+
RNCWebView.this.onMessage(message.getData(), sourceOrigin.toString(), isMainFrame, view.getUrl());
267267
}
268268
};
269269
WebViewCompat.addWebMessageListener(
@@ -345,10 +345,14 @@ public void setInjectedJavaScriptObject(String obj) {
345345
}
346346

347347
public void onMessage(String message, String sourceUrl) {
348-
onMessage(message, sourceUrl, null);
348+
onMessage(message, sourceUrl, null, null);
349349
}
350350

351351
public void onMessage(String message, String sourceUrl, @Nullable Boolean isMainFrame) {
352+
onMessage(message, sourceUrl, isMainFrame, null);
353+
}
354+
355+
public void onMessage(String message, String sourceUrl, @Nullable Boolean isMainFrame, @Nullable String topFrameUrl) {
352356
ThemedReactContext reactContext = getThemedReactContext();
353357
RNCWebView mWebView = this;
354358

@@ -365,6 +369,9 @@ public void run() {
365369
if (isMainFrame != null) {
366370
data.putBoolean("isMainFrame", isMainFrame);
367371
}
372+
if (topFrameUrl != null) {
373+
data.putString("topFrameUrl", topFrameUrl);
374+
}
368375

369376
if (mMessagingJSModule != null) {
370377
dispatchDirectMessage(data);
@@ -379,6 +386,9 @@ public void run() {
379386
if (isMainFrame != null) {
380387
eventData.putBoolean("isMainFrame", isMainFrame);
381388
}
389+
if (topFrameUrl != null) {
390+
eventData.putString("topFrameUrl", topFrameUrl);
391+
}
382392

383393
if (mMessagingJSModule != null) {
384394
dispatchDirectMessage(eventData);
@@ -476,7 +486,7 @@ protected class RNCWebViewBridge {
476486
public void postMessage(String message) {
477487
if (mWebView.getMessagingEnabled()) {
478488
// Post to main thread because `mWebView.getUrl()` requires to be executed on main.
479-
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl()));
489+
mWebView.post(() -> mWebView.onMessage(message, mWebView.getUrl(), null, mWebView.getUrl()));
480490
} else {
481491
FLog.w(TAG, "ReactNativeWebView.postMessage method was called but messaging is disabled. Pass an onMessage handler to the WebView.");
482492
}
@@ -495,4 +505,4 @@ public boolean isWaitingForCommandLoadUrl() {
495505
return waitingForCommandLoadUrl;
496506
}
497507
}
498-
}
508+
}

apple/RNCWebView.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ - (instancetype)initWithFrame:(CGRect)frame
127127
_view.onMessage = [self](NSDictionary* dictionary) {
128128
if (_eventEmitter) {
129129
auto webViewEventEmitter = std::static_pointer_cast<RNCWebViewEventEmitter const>(_eventEmitter);
130+
id topFrameUrlValue = [dictionary valueForKey:@"topFrameUrl"];
131+
NSString *topFrameUrl = [topFrameUrlValue isKindOfClass:[NSString class]] ? topFrameUrlValue : @"";
130132
facebook::react::RNCWebViewEventEmitter::OnMessage data = {
131133
.url = std::string([[dictionary valueForKey:@"url"] UTF8String]),
132134
.lockIdentifier = [[dictionary valueForKey:@"lockIdentifier"] doubleValue],
@@ -135,7 +137,8 @@ - (instancetype)initWithFrame:(CGRect)frame
135137
.canGoForward = static_cast<bool>([[dictionary valueForKey:@"canGoForward"] boolValue]),
136138
.loading = static_cast<bool>([[dictionary valueForKey:@"loading"] boolValue]),
137139
.data = std::string([[dictionary valueForKey:@"data"] UTF8String]),
138-
.isMainFrame = static_cast<bool>([[dictionary valueForKey:@"isMainFrame"] boolValue])
140+
.isMainFrame = static_cast<bool>([[dictionary valueForKey:@"isMainFrame"] boolValue]),
141+
.topFrameUrl = std::string([topFrameUrl UTF8String], [topFrameUrl lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
139142
};
140143
webViewEventEmitter->onMessage(data);
141144
}

apple/RNCWebViewImpl.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ - (void)userContentController:(WKUserContentController *)userContentController
788788
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
789789
[event addEntriesFromDictionary: @{@"data": message.body}];
790790
[event addEntriesFromDictionary: @{@"url": message.frameInfo.request.URL.absoluteString}];
791+
[event addEntriesFromDictionary: @{@"topFrameUrl": _webView.URL.absoluteString ?: @""}];
791792
[event addEntriesFromDictionary: @{@"isMainFrame": @(isMainFrame)}];
792793
_onMessage(event);
793794
}

src/RNCWebViewNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type WebViewMessageEvent = Readonly<{
3030
lockIdentifier: Double;
3131
data: string;
3232
isMainFrame?: boolean;
33+
topFrameUrl?: string;
3334
}>;
3435
export type WebViewOpenWindowEvent = Readonly<{
3536
targetUrl: string;

src/WebViewTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export type DecelerationRateConstant = 'normal' | 'fast';
111111
export interface WebViewMessage extends WebViewNativeEvent {
112112
data: string;
113113
isMainFrame?: boolean;
114+
topFrameUrl?: string;
114115
}
115116

116117
export interface WebViewError extends WebViewNativeEvent {

0 commit comments

Comments
 (0)