@@ -6,12 +6,19 @@ var path = require("path"),
66var userPackageJson = require ( "../../package.json" ) ;
77var ignoreScreen = false ;
88if ( userPackageJson && userPackageJson [ 'sensorsData' ] && userPackageJson [ 'sensorsData' ] [ 'ignoreScreen' ] ) {
9- ignoreScreen = true ;
9+ ignoreScreen = true ;
1010}
11+
12+
13+
14+
1115var reactNavigationPath = dir + '/react-navigation' ,
1216 reactNavigationPath3X = dir + '/@react-navigation/native/src' ,
1317 reactNavigationPath4X = dir + '/@react-navigation/native/lib/module' ,
14- reactNavigationPath5X = dir + '/@react-navigation/core/src/BaseNavigationContainer.tsx' ;
18+ reactNavigationPath7XCommon = dir + '/@react-navigation/core/lib/commonjs/BaseNavigationContainer.js' ,
19+ reactNavigationPath7XSrc = dir + '/@react-navigation/core/src/BaseNavigationContainer.tsx' ;
20+
21+
1522// 自定义变量
1623
1724var reactNavigationReduxCreatePath = [ dir + '/react-navigation-redux-helpers/src/reduxify-navigator.js' ,
@@ -52,57 +59,56 @@ var RNGestureButtonsFilePaths = [dir + '/react-native-gesture-handler/GestureBut
5259dir + '/react-native-gesture-handler/src/components/GestureButtons.tsx' ] ;
5360// click 需 hook 的自执行代码
5461var sensorsdataClickHookCode = "(function(thatThis){ \n"
55- + " try {\n"
56- + " var ReactNative = require('react-native');\n"
57- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
58- + " thatThis.props.onPress && dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(thatThis))\n"
59- + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
60- + "})(this); /* SENSORSDATA HOOK */ " ;
61- var sensorsdataClickHookPressabilityCode = " var tag = event.currentTarget && event.currentTarget._nativeTag?event.currentTarget._nativeTag:event.currentTarget;+\n"
62- + "(function(thatThis){\n"
63- + " if(thatThis){\n"
64- + " try {\n"
65- + " var ReactNative = require('react-native');\n"
66- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
67- + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(thatThis);\n"
68- + " }catch (error){\n"
69- + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}}}\n"
70- + ")(tag); /* SENSORSDATA HOOK */ " ;
62+ + " try {\n"
63+ + " var ReactNative = require('react-native');\n"
64+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
65+ + " thatThis.props.onPress && dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(thatThis))\n"
66+ + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
67+ + "})(this); /* SENSORSDATA HOOK */ " ;
68+ var sensorsdataClickHookPressabilityCode = "(function(thatThis){\n"
69+ + " try {\n"
70+ + " var ReactNative = require('react-native');\n"
71+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
72+ + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(event.nativeEvent.target);\n"
73+ + " } catch (error) { \n"
74+ + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);\n"
75+ + " }\n"
76+ + "})(this); /* SENSORSDATA HOOK */" ;
7177var sensorsdataSliderHookCode = "(function(thatThis){\n"
72- + " try {\n"
73- + " var ReactNative = require('react-native');\n"
74- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
75- + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(event.nativeEvent.target);\n"
76- + " } catch (error) { \n"
77- + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);\n"
78- + " }\n"
79- + "})(this); /* SENSORSDATA HOOK */" ;
78+ + " try {\n"
79+ + " var ReactNative = require('react-native');\n"
80+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
81+ + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(event.nativeEvent.target);\n"
82+ + " } catch (error) { \n"
83+ + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);\n"
84+ + " }\n"
85+ + "})(this); /* SENSORSDATA HOOK */" ;
8086var sensorsdataSegmentedControlHookCode = "if(this.props.onChange != null || this.props.onValueChange != null){\n"
81- + "(function(thatThis){\n"
82- + " try {\n"
83- + " var ReactNative = require('react-native');\n"
84- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
85- + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(event.nativeEvent.target);\n"
86- + " } catch (error) { \n"
87- + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
88- + "})(this); /* SENSORSDATA HOOK */}" ;
87+ + "(function(thatThis){\n"
88+ + " try {\n"
89+ + " var ReactNative = require('react-native');\n"
90+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
91+ + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(event.nativeEvent.target);\n"
92+ + " } catch (error) { \n"
93+ + " throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
94+ + "})(this); /* SENSORSDATA HOOK */}" ;
8995var sensorsdataSwitchHookCode = "if(this.props.onChange != null || this.props.onValueChange != null){\n"
90- + " (function(thatThis){ \n"
91- + " try {\n"
92- + " var ReactNative = require('react-native');\n"
93- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
94- + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(thatThis));\n"
95- + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
96- + " })(this); /* SENSORSDATA HOOK */}" ;
96+ + " (function(thatThis){ \n"
97+ + " try {\n"
98+ + " var ReactNative = require('react-native');\n"
99+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
100+ + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(thatThis));\n"
101+ + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
102+ + " })(this); /* SENSORSDATA HOOK */}" ;
97103var sensorsdataSwitchHookCode66 = "if(nativeSwitchRef.current && onValueChange){\n"
98- + " (function(thatThis){ \n"
99- + " try {\n"
100- + " var ReactNative = require('react-native');\n"
101- + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
102- + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(nativeSwitchRef.current));\n"
103- + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
104- + " })(this); /* SENSORSDATA HOOK */}" ;
105- var sensorsdataImportReactNativeHookCode = "import ReactNative from 'react-native';\n" ;
104+ + " (function(thatThis){ \n"
105+ + " try {\n"
106+ + " var ReactNative = require('react-native');\n"
107+ + " var dataModule = ReactNative.NativeModules.RNSensorsDataModule;\n"
108+ + " dataModule && dataModule.trackViewClick && dataModule.trackViewClick(ReactNative.findNodeHandle(nativeSwitchRef.current));\n"
109+ + " } catch (error) { throw new Error('SensorsData RN Hook Code 调用异常: ' + error);}\n"
110+ + " })(this); /* SENSORSDATA HOOK */}" ;
111+ var sensorsdataImportReactNativeHookCode = "import ReactNative from 'react-native';\n" ;
106112var sensorsdataNavigation5HookCode = `
107113
108114 function getCurrentRouteName(){
@@ -375,7 +381,14 @@ sensorsdataHookPressabilityClickRN = function () {
375381
376382
377383// hook navigation 5.x
378- sensorsdataHookNavigation5 = function ( ) {
384+ sensorsdataHookNavigation5 = function ( arg ) {
385+ var reactNavigationPath5X ;
386+ if ( arg === 'src' ) {
387+ reactNavigationPath5X = reactNavigationPath7XSrc ;
388+ } else if ( arg === 'commonjs' ) {
389+ reactNavigationPath5X = reactNavigationPath7XCommon ;
390+ }
391+
379392 if ( fs . existsSync ( reactNavigationPath5X ) ) {
380393 // 读取文件内容
381394 var fileContent = fs . readFileSync ( reactNavigationPath5X , 'utf8' ) ;
@@ -393,7 +406,7 @@ sensorsdataHookNavigation5 = function () {
393406 }
394407
395408 // 插入 hook 代码
396- var hookedContent = `${ fileContent . substring ( 0 , hookIndex
409+ var hookedContent = `${ fileContent . substring ( 0 , hookIndex
397410 ) } \n${ sensorsdataNavigation5HookCode } \n${ fileContent . substring ( hookIndex ) } `;
398411 // BaseNavigationContainer.tsx
399412 fs . renameSync (
@@ -692,7 +705,8 @@ sensorsdataHookGestureButtonsRN = function (reset = false) {
692705 var hookIndex = fileContent . indexOf ( scriptStr ) ;
693706 // 判断文件是否异常,不存在 this.props.onPress(active); 导致无法 hook 点击事件
694707 if ( hookIndex == - 1 ) {
695- throw "Can't not find this.props.onPress(active); " ;
708+ console . log ( "Can't not find this.props.onPress(active); " ) ;
709+ return false ;
696710 }
697711 // 插入 hook 代码
698712 var hookedContent = `${ fileContent . substring (
@@ -896,7 +910,7 @@ addTryCatch = function (functionBody) {
896910 ) ;
897911} ;
898912// 工具函数 - 计算位置
899- function lastArgumentName ( content , index ) {
913+ function lastArgumentName ( content , index ) {
900914 -- index ;
901915 var lastComma = content . lastIndexOf ( ',' , index ) ;
902916 var lastParentheses = content . lastIndexOf ( '(' , index ) ;
@@ -1243,7 +1257,8 @@ resetAllSensorsdataHookRN = function () {
12431257 sensorsdataHookGestureButtonsRN ( true )
12441258 // 3 期
12451259 sensorsdataResetRN ( RNClickPressabilityFilePath ) ;
1246- sensorsdataResetRN ( reactNavigationPath5X ) ;
1260+ sensorsdataResetRN ( reactNavigationPath7XCommon ) ;
1261+ sensorsdataResetRN ( reactNavigationPath7XSrc ) ;
12471262 // react-navigation-redux-helper
12481263 sensorsdataHookNavigationReduxCreate ( true ) ;
12491264 sensorsdataResetRN ( reactNavigationReduxMiddlePath ) ;
@@ -1280,7 +1295,8 @@ allSensorsdataHookRN = function () {
12801295 sensorsdataHookPressabilityClickRN ( RNClickPressabilityFilePath ) ;
12811296 }
12821297 sensorsdataHookNavigationRN ( ) ;
1283- sensorsdataHookNavigation5 ( ) ;
1298+ sensorsdataHookNavigation5 ( 'src' ) ;
1299+ sensorsdataHookNavigation5 ( 'commonjs' ) ;
12841300 // react-navigation-redux-helper
12851301 sensorsdataHookNavigationReduxCreate ( ) ;
12861302 sensorsdataHookNavigationReduxMiddle ( reactNavigationReduxMiddlePath ) ;
0 commit comments