Closed MMing0310 closed 1 year ago
您好,react-native-webview是基于WKWebview实现的,那我使用原生runtime机制,拦截交换,在WKWebview初始化的时候,直接注入js代码,通过css改变h5的样式,应该是可以的吧?但是注入后,ECharts就失效不显示了,哪怕我什么都不注入,依然无法显示,这是为什么? (instancetype)gl_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration )configuration { BOOL isOpenWhiteBlackModel = [[NSUserDefaults standardUserDefaults] boolForKey:@"kIsShowBlackWhiteModel"]; if (isOpenWhiteBlackModel) { // js脚本 // NSString jScript = @"var filter = '-webkit-filter:grayscale(100%);-moz-filter:grayscale(100%); -ms-filter:grayscale(100%); -o-filter:grayscale(100%) filter:grayscale(100%);';document.getElementsByTagName('html')[0].style.filter = 'grayscale(100%)';"; NSString jScript = @""; // 注入 WKUserScript wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; // 配置对象 WKWebViewConfiguration wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; configuration = wkWebConfig; WKWebView *webView = [self gl_initWithFrame:frame configuration:configuration]; return webView; }
return [self gl_initWithFrame:frame configuration:configuration]; }
方案已提供,可以试一下。
可能是和react-native-webview
的 JS
注入有冲突。
iOS端使用WKWebview注入js脚本(webview 置灰操作,实现APP灰白模式)后,Echarts无法正常显示,以下是WKWebview注入js脚本的代码:
import "WKWebView+blackWhiteModel.h"
import <objc/runtime.h>
@implementation WKWebView (blackWhiteModel)
(void)load { Method customMethod = class_getInstanceMethod([self class], @selector(gl_initWithFrame:configuration:)); Method originMethod = class_getInstanceMethod([self class], @selector(initWithFrame:configuration:)); method_exchangeImplementations(customMethod, originMethod);//方法交换 }
(instancetype)gl_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration )configuration { BOOL isOpenWhiteBlackModel = [[NSUserDefaults standardUserDefaults] boolForKey:@"kIsShowBlackWhiteModel"]; if (isOpenWhiteBlackModel) { // js脚本 NSString jScript = @"var filter = '-webkit-filter:grayscale(100%);-moz-filter:grayscale(100%); -ms-filter:grayscale(100%); -o-filter:grayscale(100%) filter:grayscale(100%);';document.getElementsByTagName('html')[0].style.filter = 'grayscale(100%)';"; // 注入 WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
}
return [self gl_initWithFrame:frame configuration:configuration]; } @end
目前该库是否支持WKWebview注入js脚本,更改Echarts主题样式等问题?