@@ -43,7 +43,8 @@ var RNSwitchFiles = [dir + '/react-native/Libraries/Components/Switch/Switch.js'
4343var RNSegmentedControlFilePath = [ dir + '/react-native/Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js' ,
4444dir + '/@react-native-community/segmented-control/js/SegmentedControl.ios.js' ] ;
4545// RN 控制 GestureButtons 的文件
46- var RNGestureButtonsFilePath = dir + '/react-native-gesture-handler/GestureButtons.js' ;
46+ var RNGestureButtonsFilePaths = [ dir + '/react-native-gesture-handler/GestureButtons.js' ,
47+ dir + '/react-native-gesture-handler/src/components/GestureButtons.tsx' ] ;
4748// click 需 hook 的自执行代码
4849var sensorsdataClickHookCode = "(function(thatThis){ \n"
4950 + " try {\n"
@@ -618,40 +619,58 @@ sensorsdataHookSegmentedControlRN = function (reset = false) {
618619 } ) ;
619620} ;
620621// hook GestureButtons
621- sensorsdataHookGestureButtonsRN = function ( ) {
622- if ( fs . existsSync ( RNGestureButtonsFilePath ) ) {
623- // 读取文件内容
624- var fileContent = fs . readFileSync ( RNGestureButtonsFilePath , 'utf8' ) ;
625- // 已经 hook 过了,不需要再次 hook
626- if ( fileContent . indexOf ( 'SENSORSDATA HOOK' ) > - 1 ) {
627- return ;
628- }
629- // 获取 hook 的代码插入的位置
630- var scriptStr = 'this.props.onPress(active);' ;
631- var hookIndex = fileContent . indexOf ( scriptStr ) ;
632- // 判断文件是否异常,不存在 touchableHandlePress 方法,导致无法 hook 点击事件
633- if ( hookIndex == - 1 ) {
634- throw "Can't not find onValueChange function" ;
622+ sensorsdataHookGestureButtonsRN = function ( reset = false ) {
623+ RNGestureButtonsFilePaths . forEach ( function ( onefile ) {
624+ if ( fs . existsSync ( onefile ) ) {
625+ // 读取文件内容
626+ var fileContent = fs . readFileSync ( onefile , 'utf8' ) ;
627+ if ( reset ) {
628+ // 未被 hook 过代码,不需要处理
629+ if ( fileContent . indexOf ( 'SENSORSDATA HOOK' ) == - 1 ) {
630+ return ;
631+ }
632+ // 检查备份文件是否存在
633+ var backFilePath = `${ onefile } _sensorsdata_backup` ;
634+ if ( ! fs . existsSync ( backFilePath ) ) {
635+ throw `File: ${ backFilePath } not found, Please rm -rf node_modules and npm install again` ;
636+ }
637+ // 将备份文件重命名恢复 + 自动覆盖被 hook 过的同名文件
638+ fs . renameSync ( backFilePath , onefile ) ;
639+ console . log ( `found and reset GestureButtons: ${ onefile } ` ) ;
640+ } else {
641+ // 已经 hook 过了,不需要再次 hook
642+ if ( fileContent . indexOf ( 'SENSORSDATA HOOK' ) > - 1 ) {
643+ return ;
644+ }
645+ // 获取 hook 的代码插入的位置
646+ var scriptStr = 'this.props.onPress(active);' ;
647+ var hookIndex = fileContent . indexOf ( scriptStr ) ;
648+ // 判断文件是否异常,不存在 this.props.onPress(active); 导致无法 hook 点击事件
649+ if ( hookIndex == - 1 ) {
650+ throw "Can't not find this.props.onPress(active); " ;
651+ }
652+ // 插入 hook 代码
653+ var hookedContent = `${ fileContent . substring (
654+ 0 ,
655+ hookIndex + scriptStr . length ,
656+ ) } \n${ sensorsdataClickHookCode } \n${ fileContent . substring (
657+ hookIndex + scriptStr . length ,
658+ ) } `;
659+ // 备份目标源文件
660+ fs . renameSync (
661+ onefile ,
662+ `${ onefile } _sensorsdata_backup` ,
663+ ) ;
664+ // 重写修改后的文件
665+ fs . writeFileSync ( onefile , hookedContent , 'utf8' ) ;
666+ console . log (
667+ `found and modify GestureButtons: ${ onefile } ` ,
668+ ) ;
669+ }
635670 }
636- // 插入 hook 代码
637- var hookedContent = `${ fileContent . substring (
638- 0 ,
639- hookIndex + scriptStr . length
640- ) } \n${ sensorsdataClickHookCode } \n${ fileContent . substring (
641- hookIndex + scriptStr . length
642- ) } `;
643- // 备份 Touchable.js 源文件
644- fs . renameSync (
645- RNGestureButtonsFilePath ,
646- `${ RNGestureButtonsFilePath } _sensorsdata_backup`
647- ) ;
648- // 重写 Touchable.js 文件
649- fs . writeFileSync ( RNGestureButtonsFilePath , hookedContent , 'utf8' ) ;
650- console . log (
651- `found and modify GestureButtons.js: ${ RNGestureButtonsFilePath } `
652- ) ;
653- }
671+ } ) ;
654672} ;
673+
655674// hook clickable
656675sensorsdataHookClickableRN = function ( reset = false ) {
657676 RNClickableFiles . forEach ( function ( onefile ) {
@@ -1138,7 +1157,7 @@ resetAllSensorsdataHookRN = function () {
11381157 sensorsdataHookSliderRN ( true ) ;
11391158 sensorsdataHookSwitchRN ( true ) ;
11401159 sensorsdataHookSegmentedControlRN ( true ) ;
1141- sensorsdataResetRN ( RNGestureButtonsFilePath ) ;
1160+ sensorsdataHookGestureButtonsRN ( true )
11421161 // 3 期
11431162 sensorsdataResetRN ( RNClickPressabilityFilePath ) ;
11441163 sensorsdataResetRN ( reactNavigationPath5X ) ;
@@ -1155,7 +1174,7 @@ allSensorsdataHookRN = function () {
11551174 sensorsdataHookSliderRN ( ) ;
11561175 sensorsdataHookSwitchRN ( ) ;
11571176 sensorsdataHookSegmentedControlRN ( ) ;
1158- sensorsdataHookGestureButtonsRN ( ) ;
1177+ sensorsdataHookGestureButtonsRN ( false )
11591178 // 3 期
11601179 sensorsdataHookPressabilityClickRN ( RNClickPressabilityFilePath ) ;
11611180 sensorsdataHookNavigation5 ( ) ;
0 commit comments