From 16c8275f4e1671bd57db667d5e4d3779db20cfb0 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 16 Apr 2026 16:12:04 +0000 Subject: [PATCH] Skip claude.ai/code and use word boundaries for mappings Two user-reported conflicts when using the extension alongside Claude Code on the web and with short mappings: 1. Claude Code (claude.ai/code) sends real file paths, usernames, and shell commands to its tool runtime. Substituting any of these corrupts execution, forcing users to disable the extension for that app. early-hook.js and injector.js now short-circuit on that path so fetch/XHR are never wrapped and no page-world script is injected. 2. Explicit mappings used raw literal regexes, so "not" -> "bad" would rewrite "nothing" into "badhing". substitute/reveal/scan/diff (and their page-world mirrors) now add \b on word-character edges only, leaving non-word edges like "@foo" unchanged so they keep matching. https://claude.ai/code/session_01Y2YprLMx348eWD5C9Z4zpV --- src/content/content.js | 27 ++++++++++++++++++--------- src/content/early-hook.js | 11 +++++++++++ src/content/injector.js | 9 +++++++++ src/lib/substitution-engine.js | 26 ++++++++++++++++++-------- test-suite.js | 12 ++++++++++++ 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/content/content.js b/src/content/content.js index 2626424..c14dc1c 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -51,8 +51,8 @@ for (const m of sorted) { if (!m.enabled || !m.real?.trim() || !m.substitute?.trim()) continue; - const escaped = esc(m.real); - const regex = new RegExp(escaped, m.caseSensitive ? 'g' : 'gi'); + const pattern = wordBoundary(m.real); + const regex = new RegExp(pattern, m.caseSensitive ? 'g' : 'gi'); let match; while ((match = regex.exec(result)) !== null) { replacements.push({ @@ -71,8 +71,8 @@ const sorted = [...maps].sort((a, b) => b.substitute.length - a.substitute.length); for (const m of sorted) { if (!m.enabled || !m.real?.trim() || !m.substitute?.trim()) continue; - const escaped = esc(m.substitute); - const regex = new RegExp(escaped, m.caseSensitive ? 'g' : 'gi'); + const pattern = wordBoundary(m.substitute); + const regex = new RegExp(pattern, m.caseSensitive ? 'g' : 'gi'); result = result.replace(regex, m.real); } return result; @@ -82,6 +82,15 @@ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } + // Add \b only on word-character edges so "not" doesn't match inside "nothing", + // while leaving non-word edges (e.g. "@foo") alone since they self-delimit. + function wordBoundary(str) { + const escaped = esc(str); + const left = /^\w/.test(str) ? '\\b' : ''; + const right = /\w$/.test(str) ? '\\b' : ''; + return left + escaped + right; + } + // ============================================================ // Smart Pattern Engine (inline for page world) // ============================================================ @@ -1043,8 +1052,8 @@ const pairs = getRevealPairs(); let result = text; for (const p of pairs) { - const escaped = esc(p.from); - const regex = new RegExp(escaped, p.caseSensitive ? 'g' : 'gi'); + const pattern = wordBoundary(p.from); + const regex = new RegExp(pattern, p.caseSensitive ? 'g' : 'gi'); result = result.replace(regex, p.to); } return result; @@ -1085,9 +1094,9 @@ const pairs = getRevealPairs(); let result = text; for (const p of pairs) { - const escaped = esc(p.to); // p.to is the real value - const regex = new RegExp(escaped, p.caseSensitive ? 'g' : 'gi'); - result = result.replace(regex, p.from); // p.from is the substitute + const pattern = wordBoundary(p.to); + const regex = new RegExp(pattern, p.caseSensitive ? 'g' : 'gi'); + result = result.replace(regex, p.from); } return result; } diff --git a/src/content/early-hook.js b/src/content/early-hook.js index d68dafe..b921422 100644 --- a/src/content/early-hook.js +++ b/src/content/early-hook.js @@ -9,6 +9,17 @@ * because sites like claude.ai have strict CSP that blocks inline scripts. */ (function () { + // Claude Code (web) runs at claude.ai/code and makes tool-call requests + // containing real file paths, usernames, and commands. Substituting any of + // these corrupts tool execution, so we never hook fetch/XHR on that app. + if ( + location.hostname === 'claude.ai' && + /^\/code(\/|$)/.test(location.pathname) + ) { + window.__ssSkipHost = true; + return; + } + window.__ssOriginalFetch = window.fetch; window.__ssOriginalXHROpen = XMLHttpRequest.prototype.open; window.__ssOriginalXHRSend = XMLHttpRequest.prototype.send; diff --git a/src/content/injector.js b/src/content/injector.js index e609d65..262384a 100644 --- a/src/content/injector.js +++ b/src/content/injector.js @@ -15,6 +15,15 @@ if (window.__silentSendInjected) return; window.__silentSendInjected = true; + // Skip Claude Code (web). Its tool-call traffic carries real paths/commands + // that must reach the runtime verbatim — substitution breaks execution. + if ( + location.hostname === 'claude.ai' && + /^\/code(\/|$)/.test(location.pathname) + ) { + return; + } + // Merge active profiles into flat identity object function mergeProfiles(data) { const profiles = data?.profiles || []; diff --git a/src/lib/substitution-engine.js b/src/lib/substitution-engine.js index d0e64ec..10f0b62 100644 --- a/src/lib/substitution-engine.js +++ b/src/lib/substitution-engine.js @@ -23,8 +23,8 @@ const SubstitutionEngine = { for (const mapping of sorted) { if (!mapping.enabled || !mapping.real?.trim() || !mapping.substitute?.trim()) continue; - const escaped = this._escapeRegex(mapping.real); - const regex = new RegExp(escaped, mapping.caseSensitive ? 'g' : 'gi'); + const pattern = this._wordBoundaryPattern(mapping.real); + const regex = new RegExp(pattern, mapping.caseSensitive ? 'g' : 'gi'); let match; while ((match = regex.exec(result)) !== null) { @@ -56,8 +56,8 @@ const SubstitutionEngine = { for (const mapping of sorted) { if (!mapping.enabled || !mapping.real?.trim() || !mapping.substitute?.trim()) continue; - const escaped = this._escapeRegex(mapping.substitute); - const regex = new RegExp(escaped, mapping.caseSensitive ? 'g' : 'gi'); + const pattern = this._wordBoundaryPattern(mapping.substitute); + const regex = new RegExp(pattern, mapping.caseSensitive ? 'g' : 'gi'); result = result.replace(regex, mapping.real); } @@ -73,8 +73,8 @@ const SubstitutionEngine = { for (const mapping of mappings) { if (!mapping.enabled || !mapping.real?.trim()) continue; - const escaped = this._escapeRegex(mapping.real); - const regex = new RegExp(escaped, mapping.caseSensitive ? 'g' : 'gi'); + const pattern = this._wordBoundaryPattern(mapping.real); + const regex = new RegExp(pattern, mapping.caseSensitive ? 'g' : 'gi'); if (regex.test(text)) { found.push({ @@ -105,8 +105,8 @@ const SubstitutionEngine = { for (const mapping of sorted) { if (!mapping.enabled || !mapping.real?.trim() || !mapping.substitute?.trim()) continue; - const escaped = this._escapeRegex(mapping.real); - const regex = new RegExp(escaped, mapping.caseSensitive ? 'g' : 'gi'); + const pattern = this._wordBoundaryPattern(mapping.real); + const regex = new RegExp(pattern, mapping.caseSensitive ? 'g' : 'gi'); let match; while ((match = regex.exec(original)) !== null) { @@ -145,6 +145,16 @@ const SubstitutionEngine = { _escapeRegex(str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }, + + // Wrap an escaped literal in \b only on edges that are word characters, + // so "not" → "bad" matches "not" but not "nothing", while mappings whose + // edges aren't word chars (e.g. "@foo", "foo.com ") still work. + _wordBoundaryPattern(str) { + const escaped = this._escapeRegex(str); + const left = /^\w/.test(str) ? '\\b' : ''; + const right = /\w$/.test(str) ? '\\b' : ''; + return left + escaped + right; + }, }; // Support both module and content-script contexts diff --git a/test-suite.js b/test-suite.js index 5d2efad..f9a0903 100644 --- a/test-suite.js +++ b/test-suite.js @@ -345,6 +345,18 @@ test('Disabled mapping is skipped', () => { if (result.text.includes('Alex Demo')) throw 'Disabled mapping should not substitute'; }); +test('Mapping matches whole words only', () => { + const result = SubstitutionEngine.substitute('nothing is not a thing, not even this', [ + { real: 'not', substitute: 'bad', enabled: true } + ]); + if (result.text.includes('bahing') || result.text.includes('badhing')) { + throw `Should not match inside "nothing", got: ${result.text}`; + } + if (!/\bbad\b/.test(result.text)) { + throw `Should still match standalone "not", got: ${result.text}`; + } +}); + // ============================================================ // SMART PATTERNS // ============================================================