From b6e34580bb05ae0032fbc49e43620bffb9047459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=8E=E9=A5=BC=E6=9E=9C=E5=AD=90=E5=8D=B7=E9=B2=A8?= =?UTF-8?q?=E9=B1=BC=E8=BE=A3=E6=A4=92?= Date: Wed, 14 Jan 2026 17:05:56 +0800 Subject: [PATCH] =?UTF-8?q?```=20fix(cursor-hook):=20=E4=BF=AE=E5=A4=8DCur?= =?UTF-8?q?sor=20ID=E4=BF=AE=E6=94=B9=E5=99=A8=E7=9A=84=E6=AD=A3=E5=88=99?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改PowerShell脚本中的正则表达式,从泛匹配改为精确匹配b6函数名, 避免跨函数误替换导致main.js语法破坏 - 添加匹配长度检查,当匹配结果超过5000字符时跳过替换以防止文件损坏 - 更新警告信息,明确标注未找到目标函数时的行为 - 调整JavaScript Hook文件为纯CommonJS写法,避免ESM语法导致Cursor启动失败 ``` --- scripts/hook/cursor_hook.js | 21 +++++---------------- scripts/run/cursor_win_id_modifier.ps1 | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/scripts/hook/cursor_hook.js b/scripts/hook/cursor_hook.js index 9f3798b..30191c3 100644 --- a/scripts/hook/cursor_hook.js +++ b/scripts/hook/cursor_hook.js @@ -84,23 +84,12 @@ var __cursor_hook_config__ = { }; // 加载或生成 ID 配置 - // 注意:使用 createRequire 来支持 ES Module 环境 + // 注意:该 Hook 由脚本注入的 Loader 通过 CommonJS(require) 方式加载, + // 为避免出现 import.meta 等仅 ESM 支持的语法导致 Cursor 启动期解析失败,这里保持纯 CommonJS 写法。 const loadOrGenerateIds = () => { - // 在 ES Module 环境中,需要使用 createRequire 来加载 CommonJS 模块 - let fs, path, os; - try { - // 尝试使用 Node.js 内置模块 - const { createRequire } = require('module'); - const require2 = createRequire(import.meta?.url || __filename); - fs = require2('fs'); - path = require2('path'); - os = require2('os'); - } catch (e) { - // 回退到直接 require - fs = require('fs'); - path = require('path'); - os = require('os'); - } + const fs = require('fs'); + const path = require('path'); + const os = require('os'); const configPath = path.join(os.homedir(), __cursor_hook_config__.configFileName); diff --git a/scripts/run/cursor_win_id_modifier.ps1 b/scripts/run/cursor_win_id_modifier.ps1 index b003d84..7e0251d 100644 --- a/scripts/run/cursor_win_id_modifier.ps1 +++ b/scripts/run/cursor_win_id_modifier.ps1 @@ -518,16 +518,22 @@ function Modify-CursorJSFiles { # ========== 方法B: b6 定点重写(机器码源函数,仅 main.js) ========== # 说明:b6(t) 是 machineId 的核心生成函数,t=true 返回原始值,t=false 返回哈希 if ((Split-Path $file -Leaf) -eq "main.js") { - # 使用特征锚点定位(createHash("sha256") + return t?e:i),避免依赖函数名 - $b6Pattern = '(?s)async function (\w+)\((\w+)\)\{.*?createHash\("sha256"\).*?return \w+\?\w+:\w+\}' - $b6Replacement = "async function `$1(`$2){return `$2?'$machineGuid':'$machineId';}" + # ⚠️ 安全修复:旧版“泛匹配 async function + .*?”会在部分版本中跨越大段代码误替换,直接破坏 main.js 语法。 + # 这里改为仅锚定 b6(...) 的函数定义;若版本不存在 b6,则跳过方案B,避免误伤。 + $b6Pattern = '(?s)async function b6\((\w+)\)\{.*?createHash\(["'']sha256["'']\).*?return \1\?\w+:\w+\}' + $b6Replacement = "async function b6(`$1){return `$1?'$machineGuid':'$machineId';}" $b6Regex = [regex]::new($b6Pattern) - if ($b6Regex.IsMatch($content)) { - $content = $b6Regex.Replace($content, $b6Replacement, 1) - Write-Host " $GREEN✓$NC [方案B] 已重写 b6(t) 返回值" - $replacedB6 = $true + $b6Match = $b6Regex.Match($content) + if ($b6Match.Success) { + if ($b6Match.Length -gt 5000) { + Write-Host " $YELLOW⚠️ $NC [方案B] b6 匹配长度异常($($b6Match.Length)),为避免破坏文件已跳过" + } else { + $content = $b6Regex.Replace($content, $b6Replacement, 1) + Write-Host " $GREEN✓$NC [方案B] 已重写 b6(t) 返回值" + $replacedB6 = $true + } } else { - Write-Host " $YELLOW⚠️ $NC [方案B] 未定位到 b6(t) 目标函数" + Write-Host " $YELLOW⚠️ $NC [方案B] 未找到 b6(t) 目标函数,已跳过" } }