EngineeringOctober 3, 2026 · 6 min read

How @mention routing works: the technical story

When you type @claude inside ChatGPT and press Enter, a lot happens in under 300 milliseconds. Here is the exact sequence — every DOM selector, every Chrome API call, every polling loop — because I think you should be able to audit what a browser extension does to your data.

The first thing I decided when building Crossagent was that I would not proxy anything. No server in the middle that intercepts your HTTPS connection and re-sends your message. No API key that routes your words through my backend. The extension would talk to the AI's own DOM — the same way you do when you type.

This decision shaped everything about the architecture.

Step 1: Intercepting the @mention

Every content script on every supported AI tab attaches a keydown listener at the window capture phase — before the host application's own listeners fire. When you press Enter, this runs first:

window.addEventListener('keydown', (e) => {
  if (e.key !== 'Enter' || e.shiftKey) return;
  const text = getInputText(); // reads the active contenteditable
  const mention = parseMention(text); // looks for @claude, @all, etc.
  if (!mention) return;
  e.stopImmediatePropagation(); // prevents ChatGPT from seeing the Enter
  e.preventDefault();
  handleMention(mention);
}, true); // capture phase

The true at the end is the important part. Capture phase means our listener runs before React's synthetic event system. ChatGPT never sees the Enter key.

Step 2: Finding the target tab

handleMention sends a message to the background service worker — the persistent script that can access Chrome APIs:

// content.js → background.js
chrome.runtime.sendMessage({
  type: 'RELAY_NATIVE',
  target: 'claude',
  message: userMessage,
  conversationUrl: null // background will look it up
});

// background.js
chrome.runtime.onMessage.addListener(async (msg) => {
  if (msg.type !== 'RELAY_NATIVE') return;

  const tabs = await chrome.tabs.query({ url: 'https://claude.ai/*' });
  const tab = tabs[0];

  if (!tab) {
    // open claude.ai in background, wait for load
    const newTab = await chrome.tabs.create({ url: 'https://claude.ai/new', active: false });
    await waitForTab(newTab.id);
  }

  // restore last conversation URL if we have one
  const saved = await chrome.storage.local.get('claude_conversation_url');
  if (saved.claude_conversation_url) {
    await chrome.tabs.update(tab.id, { url: saved.claude_conversation_url });
    await waitForTab(tab.id);
  }

  chrome.tabs.sendMessage(tab.id, { type: 'INJECT', message: msg.message });
});

Step 3: Injecting the message

This was the hardest part. Every AI platform uses a different input system — Claude uses ProseMirror, ChatGPT uses Lexical, Gemini uses a raw contenteditable. Getting text into all of them reliably required a cascade of methods:

async function injectMessage(text) {
  const input = findInput(); // aria-label, role, data-testid cascade

  input.focus();

  // Primary: execCommand (works across ProseMirror + Lexical + contenteditable)
  document.execCommand('selectAll');
  document.execCommand('insertText', false, text);

  // Fallback: direct textContent + InputEvent
  if (input.textContent !== text) {
    input.textContent = text;
    input.dispatchEvent(new InputEvent('input', { bubbles: true, data: text }));
  }

  // Move cursor to end (required for some frameworks)
  const range = document.createRange();
  range.selectNodeContents(input);
  range.collapse(false);
  window.getSelection().removeAllRanges();
  window.getSelection().addRange(range);

  // Click send
  const btn = findSendButton(); // aria-label → data-testid → proximity heuristic
  btn.click();
}

Step 4: Waiting for the response

The AI streams its response. We can't read it until it's done — otherwise we'd capture a half-sentence. The solution is a stability check: poll the response element every 200ms, and only resolve when the text hasn't changed for ~1 second:

function waitForResponse() {
  return new Promise((resolve) => {
    let lastText = '';
    let stableCount = 0;

    const observer = new MutationObserver(() => {
      const el = document.querySelector('[data-testid="assistant-message"]:last-child');
      if (!el) return;

      const text = el.textContent;
      if (text === lastText) {
        stableCount++;
        if (stableCount >= 5) { // stable for 1 second (5 × 200ms)
          observer.disconnect();
          resolve(text);
        }
      } else {
        lastText = text;
        stableCount = 0;
      }
    });

    observer.observe(document.body, { childList: true, subtree: true });
  });
}

Step 5: Rendering in the panel

The response travels back through the message chain: Claude tab → background → ChatGPT tab → panel. The panel renders it with a “native claude” label so you always know what happened:

// Response flows back
chrome.runtime.sendMessage({
  type: 'RESPONSE',
  agent: 'claude',
  text: responseText,
  routing: 'native' // vs 'api' for the relay fallback
});

// Panel receives and renders
panel.addMessage({
  role: 'assistant',
  agent: 'claude',
  text: responseText,
  routing: 'native',
  timestamp: Date.now()
});

@all is just this, seven times at once

When you type @all, the same flow runs for all seven platforms in parallel:

const agents = ['claude', 'chatgpt', 'gemini', 'perplexity', 'grok', 'mistral', 'copilot'];

const results = await Promise.allSettled(
  agents.map(agent => routeNative(agent, message))
);

// Synthesize: send all responses to Claude natively
const synthesis = results
  .filter(r => r.status === 'fulfilled')
  .map(r => `[${r.value.agent}]: ${r.value.text}`)
  .join('\n\n');

const summary = await routeNative('claude',
  `Synthesize these 7 AI responses to: "${message}"\n\n${synthesis}`
);

The whole thing — seven parallel native routing calls plus synthesis — usually completes in 15-40 seconds, bounded entirely by the slowest AI's streaming time. Crossagent's own overhead is under 300ms.

What never happens

At no point in this flow does a message leave your browser on the native path. There is no network call to my backend. There is no API key lookup. There is no token counter. The only network traffic is the normal HTTPS connection from your browser to Claude's servers — the same connection that would have happened if you'd typed it yourself.

You can verify this: open Chrome DevTools → Network → filter by XHR. Type @claude test in ChatGPT. You'll see a request to claude.ai. No request to railway.app.

That's the whole architecture. I think it's the right way to build this.

Written by

Tanmay Sangam

Builder, Crossagent  ·  Northeastern University  ·  Boston

See the source.

The extension is MIT licensed. Every selector, every observer, every routing decision is auditable.

Follow on X ↗Add to Chrome — free →

NEXT POST

Why we route natively — and what it means for your data →