如何禁用反应本机 webview 的默认上下文菜单,并调用反应道具而不是本机道具?
How to disable the default context menu of react native webview, and call a react prop instead of native one?
我需要禁用在 Web 视图中 selecting 文本时弹出的默认上下文菜单,而是从 javascript 端处理此事件。
经过一些初步研究,我在 RNCWebview.m
中添加了以下代码,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
NSLog(@"From native side");
return NO;
}
然后从我渲染组件的地方传递了一个 onSelection
道具。我在我的节点模块中给出了 console.logs 并将这个对象作为 webview
的道具
{"cacheEnabled": true, "javaScriptEnabled": true, "onSelection": [Function onSelection], "originWhitelist": ["http://*", "https://*"], "source": {"uri": "https://www.google.com"}, "style": {"height": 500}, "useSharedProcessPool": true}
但我不确定如何处理此函数并从 objective-c 代码中调用它。但是当我 select webview 中的一些文本并且 onSelection
道具没有被调用时,上下文菜单仍然出现。
我确定我在这里遗漏了一些 link。请帮助
对于stop menuContext,可以在onselect事件中添加一个eventListener,以便在你html的WebView组件的代码中使用preventDefault,但是onselect只能在input and textarea tags中使用。
我终于找到了解决办法。我修改了代码 RNCWebView.m
和 RNCWebViewManager.m
以阻止 onSelection
事件并通过实施以下代码。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return NO;
}
这禁用了 Web 视图中的默认上下文菜单。然后使用 webview 的 injectJavascript
道具我添加了一个事件侦听器来观察 selectionChange
事件并使用自定义函数响应它。
我需要禁用在 Web 视图中 selecting 文本时弹出的默认上下文菜单,而是从 javascript 端处理此事件。
经过一些初步研究,我在 RNCWebview.m
中添加了以下代码,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
NSLog(@"From native side");
return NO;
}
然后从我渲染组件的地方传递了一个 onSelection
道具。我在我的节点模块中给出了 console.logs 并将这个对象作为 webview
{"cacheEnabled": true, "javaScriptEnabled": true, "onSelection": [Function onSelection], "originWhitelist": ["http://*", "https://*"], "source": {"uri": "https://www.google.com"}, "style": {"height": 500}, "useSharedProcessPool": true}
但我不确定如何处理此函数并从 objective-c 代码中调用它。但是当我 select webview 中的一些文本并且 onSelection
道具没有被调用时,上下文菜单仍然出现。
我确定我在这里遗漏了一些 link。请帮助
对于stop menuContext,可以在onselect事件中添加一个eventListener,以便在你html的WebView组件的代码中使用preventDefault,但是onselect只能在input and textarea tags中使用。
我终于找到了解决办法。我修改了代码 RNCWebView.m
和 RNCWebViewManager.m
以阻止 onSelection
事件并通过实施以下代码。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return NO;
}
这禁用了 Web 视图中的默认上下文菜单。然后使用 webview 的 injectJavascript
道具我添加了一个事件侦听器来观察 selectionChange
事件并使用自定义函数响应它。