diff --git a/RNSensorsAnalyticsModule.podspec b/RNSensorsAnalyticsModule.podspec index 17a79d8..9d8592b 100644 --- a/RNSensorsAnalyticsModule.podspec +++ b/RNSensorsAnalyticsModule.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "RNSensorsAnalyticsModule" - s.version = "2.1.0" + s.version = "2.1.1" s.summary = "The official React Native SDK of Sensors Analytics." s.description = <<-DESC 神策分析 RN 组件 diff --git a/SensorsDataRNHook.js b/SensorsDataRNHook.js index 2bdde18..86a96af 100644 --- a/SensorsDataRNHook.js +++ b/SensorsDataRNHook.js @@ -43,7 +43,8 @@ var RNSwitchFiles = [dir + '/react-native/Libraries/Components/Switch/Switch.js' var RNSegmentedControlFilePath = [dir + '/react-native/Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js', dir + '/@react-native-community/segmented-control/js/SegmentedControl.ios.js']; // RN 控制 GestureButtons 的文件 -var RNGestureButtonsFilePath = dir + '/react-native-gesture-handler/GestureButtons.js'; +var RNGestureButtonsFilePaths = [dir + '/react-native-gesture-handler/GestureButtons.js', +dir + '/react-native-gesture-handler/src/components/GestureButtons.tsx']; // click 需 hook 的自执行代码 var sensorsdataClickHookCode = "(function(thatThis){ \n" +" try {\n" @@ -618,40 +619,58 @@ sensorsdataHookSegmentedControlRN = function (reset = false) { }); }; // hook GestureButtons -sensorsdataHookGestureButtonsRN = function () { - if (fs.existsSync(RNGestureButtonsFilePath)) { - // 读取文件内容 - var fileContent = fs.readFileSync(RNGestureButtonsFilePath, 'utf8'); - // 已经 hook 过了,不需要再次 hook - if (fileContent.indexOf('SENSORSDATA HOOK') > -1) { - return; - } - // 获取 hook 的代码插入的位置 - var scriptStr = 'this.props.onPress(active);'; - var hookIndex = fileContent.indexOf(scriptStr); - // 判断文件是否异常,不存在 touchableHandlePress 方法,导致无法 hook 点击事件 - if (hookIndex == -1) { - throw "Can't not find onValueChange function"; +sensorsdataHookGestureButtonsRN = function (reset = false) { + RNGestureButtonsFilePaths.forEach(function (onefile) { + if (fs.existsSync(onefile)) { + // 读取文件内容 + var fileContent = fs.readFileSync(onefile, 'utf8'); + if (reset) { + // 未被 hook 过代码,不需要处理 + if (fileContent.indexOf('SENSORSDATA HOOK') == -1) { + return; + } + // 检查备份文件是否存在 + var backFilePath = `${onefile}_sensorsdata_backup`; + if (!fs.existsSync(backFilePath)) { + throw `File: ${backFilePath} not found, Please rm -rf node_modules and npm install again`; + } + // 将备份文件重命名恢复 + 自动覆盖被 hook 过的同名文件 + fs.renameSync(backFilePath, onefile); + console.log(`found and reset GestureButtons: ${onefile}`); + } else { + // 已经 hook 过了,不需要再次 hook + if (fileContent.indexOf('SENSORSDATA HOOK') > -1) { + return; + } + // 获取 hook 的代码插入的位置 + var scriptStr = 'this.props.onPress(active);'; + var hookIndex = fileContent.indexOf(scriptStr); + // 判断文件是否异常,不存在 this.props.onPress(active); 导致无法 hook 点击事件 + if (hookIndex == -1) { + throw "Can't not find this.props.onPress(active); "; + } + // 插入 hook 代码 + var hookedContent = `${fileContent.substring( + 0, + hookIndex + scriptStr.length, + )}\n${sensorsdataClickHookCode}\n${fileContent.substring( + hookIndex + scriptStr.length, + )}`; + // 备份目标源文件 + fs.renameSync( + onefile, + `${onefile}_sensorsdata_backup`, + ); + // 重写修改后的文件 + fs.writeFileSync(onefile, hookedContent, 'utf8'); + console.log( + `found and modify GestureButtons: ${onefile}`, + ); + } } - // 插入 hook 代码 - var hookedContent = `${fileContent.substring( - 0, - hookIndex + scriptStr.length - )}\n${sensorsdataClickHookCode}\n${fileContent.substring( - hookIndex + scriptStr.length - )}`; - // 备份 Touchable.js 源文件 - fs.renameSync( - RNGestureButtonsFilePath, - `${RNGestureButtonsFilePath}_sensorsdata_backup` - ); - // 重写 Touchable.js 文件 - fs.writeFileSync(RNGestureButtonsFilePath, hookedContent, 'utf8'); - console.log( - `found and modify GestureButtons.js: ${RNGestureButtonsFilePath}` - ); - } + }); }; + // hook clickable sensorsdataHookClickableRN = function (reset = false) { RNClickableFiles.forEach(function (onefile) { @@ -1138,7 +1157,7 @@ resetAllSensorsdataHookRN = function () { sensorsdataHookSliderRN(true); sensorsdataHookSwitchRN(true); sensorsdataHookSegmentedControlRN(true); - sensorsdataResetRN(RNGestureButtonsFilePath); + sensorsdataHookGestureButtonsRN(true) // 3 期 sensorsdataResetRN(RNClickPressabilityFilePath); sensorsdataResetRN(reactNavigationPath5X); @@ -1155,7 +1174,7 @@ allSensorsdataHookRN = function () { sensorsdataHookSliderRN(); sensorsdataHookSwitchRN(); sensorsdataHookSegmentedControlRN(); - sensorsdataHookGestureButtonsRN(); + sensorsdataHookGestureButtonsRN(false) // 3 期 sensorsdataHookPressabilityClickRN(RNClickPressabilityFilePath); sensorsdataHookNavigation5(); diff --git a/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java b/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java index f0722b7..9751463 100644 --- a/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java +++ b/android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java @@ -28,7 +28,7 @@ import java.util.List; public class RNSensorsAnalyticsPackage implements ReactPackage { - public static final String VERSION = "2.1.0"; + public static final String VERSION = "2.1.1"; @Override public List createNativeModules(ReactApplicationContext reactContext) { List modules = new ArrayList<>(); diff --git a/ios/RNSensorsAnalyticsModule.m b/ios/RNSensorsAnalyticsModule.m index 4f04f1d..78f485e 100644 --- a/ios/RNSensorsAnalyticsModule.m +++ b/ios/RNSensorsAnalyticsModule.m @@ -32,7 +32,7 @@ #import "SAReactNativeManager.h" #import "SAReactNativeEventProperty.h" -NSString *const kSAReactNativePluginVersion = @"react_native:2.1.0"; +NSString *const kSAReactNativePluginVersion = @"react_native:2.1.1"; @implementation RNSensorsAnalyticsModule diff --git a/package.json b/package.json index ce48e22..33f0ee8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sensorsdata-analytics-react-native", - "version": "2.1.0", + "version": "2.1.1", "private": false, "description": "神策分析 RN 组件", "main": "index.js",