Skip to content

Commit

Permalink
Implement 'injectJavaScript' method
Browse files Browse the repository at this point in the history
Summary:
@public

This diff introduces a method called `injectJavaScript(script)` on the React Native `<WKWebView/>` component. When called with a string, it evaluates that string as JavaScript within the web view.

Reviewed By: shergin

Differential Revision: D6367445

fbshipit-source-id: f68afeff42535dc991747f96a63f3c956faf13d3
  • Loading branch information
RSNara authored and facebook-github-bot committed Aug 16, 2018
1 parent 90e85a4 commit 0022354
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions React/Views/RCTWKWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
@property (nonatomic, assign) CGFloat decelerationRate;

- (void)postMessage:(NSString *)message;
- (void)injectJavaScript:(NSString *)script;

@end
5 changes: 5 additions & 0 deletions React/Views/RCTWKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,9 @@ - (void) webView:(WKWebView *)webView
}
}

- (void)injectJavaScript:(NSString *)script
{
[self evaluateJS: script thenCall: nil];
}

@end
12 changes: 12 additions & 0 deletions React/Views/RCTWKWebViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ - (UIView *)view
view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
}

RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTWKWebView *> *viewRegistry) {
RCTWKWebView *view = viewRegistry[reactTag];
if (![view isKindOfClass:[RCTWKWebView class]]) {
RCTLogError(@"Invalid view returned from registry, expecting RCTWebView, got: %@", view);
} else {
[view injectJavaScript:script];
}
}];
}

@end

0 comments on commit 0022354

Please sign in to comment.