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
This commit is contained in:
Claude
2026-04-16 16:12:04 +00:00
parent 03dd61d8bd
commit 16c8275f4e
5 changed files with 68 additions and 17 deletions
+18 -9
View File
@@ -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;
}