Skip to content

Commit

Permalink
feat: auto web js sdk bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Oct 12, 2024
1 parent dde83f1 commit f4a17fd
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Example/Example/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (void)SDK3rdStart {
#endif

#if defined(SDKHybridMODULE)
GrowingHybridModule.sharedInstance.autoJavaScriptBridgeEnabled = YES;
GrowingHybridModule.sharedInstance.autoJsSdkInject = YES;
#endif

#pragma clang diagnostic push
Expand Down
2 changes: 1 addition & 1 deletion Modules/Hybrid/GrowingHybridModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ + (instancetype)sharedInstance {
- (instancetype)init {
if (self = [super init]) {
_autoBridgeEnabled = YES;
_autoJavaScriptBridgeEnabled = NO;
_autoJsSdkInject = NO;
_enableBridgeWebViews = [NSHashTable weakObjectsHashTable];
_disableBridgeWebViews = [NSHashTable weakObjectsHashTable];
}
Expand Down
81 changes: 47 additions & 34 deletions Modules/Hybrid/GrowingWKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,45 +94,58 @@ + (void)addUserScripts:(WKUserContentController *)contentController {
*stop = YES;
}
}];

if (!isContainUserScripts) {
NSString *accountId = GrowingConfigurationManager.sharedInstance.trackConfiguration.accountId;
NSString *dataSourceId = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataSourceId;
NSString *bundleId = [GrowingDeviceInfo currentDeviceInfo].bundleID;
NSString *urlScheme = [GrowingDeviceInfo currentDeviceInfo].urlScheme;
GrowingWebViewJavascriptBridgeConfiguration *config =
[GrowingWebViewJavascriptBridgeConfiguration configurationWithAccountId:accountId
dataSourceId:dataSourceId
appId:urlScheme
appPackage:bundleId
nativeSdkVersion:GrowingTrackerVersionName
nativeSdkVersionCode:GrowingTrackerVersionCode];

WKUserScript *userScript =
[[WKUserScript alloc] initWithSource:[GrowingWKWebViewJavascriptBridge_JS
createJavascriptBridgeJsWithNativeConfiguration:config]
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
[contentController addUserScript:userScript];

if (GrowingHybridModule.sharedInstance.autoJavaScriptBridgeEnabled) {
NSBundle *bundle = [self resourcesBundle];
NSString *filePath = [bundle pathForResource:@"gdp-full" ofType:@"js"];
NSString *webJSContent = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];
webJSContent =
[webJSContent stringByAppendingFormat:@"gdp('init', '%@', '%@');", accountId, dataSourceId];
WKUserScript *userScript2 = [[WKUserScript alloc] initWithSource:webJSContent
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:NO];
[contentController addUserScript:userScript2];
}

if (isContainUserScripts) {
return;
}

NSString *accountId = GrowingConfigurationManager.sharedInstance.trackConfiguration.accountId;
NSString *dataSourceId = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataSourceId;
NSString *bundleId = [GrowingDeviceInfo currentDeviceInfo].bundleID;
NSString *urlScheme = [GrowingDeviceInfo currentDeviceInfo].urlScheme;
GrowingWebViewJavascriptBridgeConfiguration *config =
[GrowingWebViewJavascriptBridgeConfiguration configurationWithAccountId:accountId
dataSourceId:dataSourceId
appId:urlScheme
appPackage:bundleId
nativeSdkVersion:GrowingTrackerVersionName
nativeSdkVersionCode:GrowingTrackerVersionCode];

[contentController addUserScript:[self bridgeJsUserScriptWithConfig:config]];
if (GrowingHybridModule.sharedInstance.autoJsSdkInject) {
[contentController addUserScript:[self javaScriptSdkInjectJsUserScriptWithConfig:config]];
}
} @catch (NSException *exception) {
}
}

+ (WKUserScript *)bridgeJsUserScriptWithConfig:(GrowingWebViewJavascriptBridgeConfiguration *)config {
WKUserScript *userScript =
[[WKUserScript alloc] initWithSource:[GrowingWKWebViewJavascriptBridge_JS
createJavascriptBridgeJsWithNativeConfiguration:config]
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
return userScript;
}

+ (WKUserScript *)javaScriptSdkInjectJsUserScriptWithConfig:(GrowingWebViewJavascriptBridgeConfiguration *)config {
// NSBundle *bundle = [self resourcesBundle];
// NSString *filePath = [bundle pathForResource:@"gdp-full" ofType:@"js"];
// NSString *webJSContent = [NSString stringWithContentsOfFile:filePath
// encoding:NSUTF8StringEncoding
// error:nil];
// webJSContent =
// [webJSContent stringByAppendingFormat:@"gdp('init', '%@', '%@');", config.accountId, config.dataSourceId];
// WKUserScript *userScript = [[WKUserScript alloc] initWithSource:webJSContent
// injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
// forMainFrameOnly:NO];
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:[GrowingWKWebViewJavascriptBridge_JS
createJavascriptSdkInjectJsWithNativeConfiguration:config]
injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
forMainFrameOnly:NO];
return userScript;
}

+ (NSBundle *)resourcesBundle {
#if SWIFT_PACKAGE
return GrowingBundleWrapper_SWIFTPM_MODULE_BUNDLE();
Expand Down
3 changes: 3 additions & 0 deletions Modules/Hybrid/GrowingWKWebViewJavascriptBridge_JS.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
+ (NSString *)createJavascriptBridgeJsWithNativeConfiguration:
(GrowingWebViewJavascriptBridgeConfiguration *)configuration;

+ (NSString *)createJavascriptSdkInjectJsWithNativeConfiguration:
(GrowingWebViewJavascriptBridgeConfiguration *)configuration;

@end
37 changes: 37 additions & 0 deletions Modules/Hybrid/GrowingWKWebViewJavascriptBridge_JS.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,41 @@ + (NSString *)createJavascriptBridgeJsWithNativeConfiguration:
withString:[configuration toJsonString]];
}

static NSString *kWKWebViewJavascriptSdkInject_js(void) {
#define __WKWebViewJavascriptSdkInject_js_func__(x) #x

// BEGIN preprocessorJSCode
static NSString *kGrowingPreprocessorJSCode = @__WKWebViewJavascriptSdkInject_js_func__(
!(function (e, n, t, s, c) {
var o;
e[s] =
e[s] ||
function () {
(e[s].q = e[s].q || []).push(arguments);
};
(c = c || 'vds'),
(e._gio_local_vds = c),
(e[c] = null !== (o = e[c]) && void 0 !== o ? o : {}),
(e[c].namespace = s);
var d = n.createElement('script');
var i = n.getElementsByTagName('script')[0];
(d.async = !0), (d.src = t), i.parentNode.insertBefore(d, i);
})(window, document, 'https://assets.giocdn.com/sdk/webjs/gdp-full.js', 'gdp');

gdp('init', '$accountId_replacement', '$dataSourceId_replacement');
);
#undef __WKWebViewJavascriptSdkInject_js_func__
return kGrowingPreprocessorJSCode;
};

+ (NSString *)createJavascriptSdkInjectJsWithNativeConfiguration:
(GrowingWebViewJavascriptBridgeConfiguration *)configuration {
NSString *bridge = kWKWebViewJavascriptSdkInject_js();
bridge = [bridge stringByReplacingOccurrencesOfString:@"$accountId_replacement"
withString:configuration.accountId];
bridge = [bridge stringByReplacingOccurrencesOfString:@"$dataSourceId_replacement"
withString:configuration.dataSourceId];
return bridge;
}

@end
2 changes: 1 addition & 1 deletion Modules/Hybrid/Public/GrowingHybridModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ NS_SWIFT_NAME(Hybrid)
@property (nonatomic, assign) BOOL autoBridgeEnabled;

// 是否对所有webView自动注入Web JS SDK,默认为NO
@property (nonatomic, assign) BOOL autoJavaScriptBridgeEnabled;
@property (nonatomic, assign) BOOL autoJsSdkInject;

// 在autoBridgeEnabled为NO时,对单个webView启用Hybrid注入,请在主线程调用
- (void)enableBridgeForWebView:(WKWebView *)webView NS_SWIFT_NAME(enableBridge(_:));
Expand Down

0 comments on commit f4a17fd

Please sign in to comment.