Claude Skills Guide

Chrome Extension Writing Assistant: A Developer’s Guide

Writing assistants integrated as Chrome extensions have become essential tools for developers, technical writers, and content creators. These extensions bring AI-powered writing capabilities directly into your browser, working across web forms, code comments, documentation sites, and communication platforms. This guide explores how these extensions work, practical implementation approaches, and how developers can customize or build their own writing assistants.

How Chrome Extension Writing Assistants Work

A Chrome extension writing assistant intercepts text input in web pages and provides intelligent suggestions, transformations, or completions. Unlike standalone writing tools, these extensions operate within your browser context, understanding the specific page you’re working on and offering contextual assistance.

The architecture typically consists of three core components. First, a content script that runs in the context of web pages, detecting text inputs and capturing user selections. Second, a background service worker that handles API communication with AI providers. Third, a popup or side panel interface that provides user controls and displays suggestions.

When you select text or type in a supported field, the content script captures that content and sends it to the background script. The background script then processes the request through an AI API and returns suggestions that the content script displays as an overlay or inline formatting.

Core Implementation Patterns

Building a writing assistant extension requires understanding several key patterns that make these tools functional and responsive.

Content Script Input Detection

The foundation of any writing assistant is detecting where users are输入文字. Modern web pages use various input mechanisms, from simple <input> elements to complex contenteditable areas. Your content script needs to handle multiple input types:

// content-script.js - Input detection and capture
class InputDetector {
  constructor() {
    this.observers = [];
    this.setupObservers();
  }

  setupObservers() {
    // Monitor existing and new input elements
    const inputs = document.querySelectorAll('input, textarea, [contenteditable]');
    inputs.forEach(input => this.attachListener(input));

    // Use MutationObserver for dynamically added elements
    const observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        mutation.addedNodes.forEach((node) => {
          if (node.nodeType === Node.ELEMENT_NODE) {
            const inputs = node.querySelectorAll('input, textarea, [contenteditable]');
            inputs.forEach(input => this.attachListener(input));
          }
        });
      });
    });

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

  attachListener(element) {
    if (element.dataset.writingAssistantAttached) return;
    element.dataset.writingAssistantAttached = 'true';

    element.addEventListener('input', (event) => {
      this.handleInput(event.target);
    });

    element.addEventListener('mouseup', (event) => {
      if (window.getSelection().toString().length > 0) {
        this.handleSelection(window.getSelection());
      }
    });
  }

  handleInput(element) {
    const context = element.value || element.textContent;
    const position = element.selectionStart;
    // Send to background script for processing
    chrome.runtime.sendMessage({
      type: 'TEXT_INPUT',
      context: context,
      position: position,
      url: window.location.href
    });
  }

  handleSelection(selection) {
    chrome.runtime.sendMessage({
      type: 'TEXT_SELECTION',
      text: selection.toString(),
      url: window.location.href
    });
  }
}

Background Script API Integration

The background script acts as a bridge between your content script and external AI services. It handles API authentication, request formatting, and response processing:

// background.js - API communication handler
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === 'TEXT_INPUT' || message.type === 'TEXT_SELECTION') {
    handleWritingRequest(message).then(sendResponse);
    return true; // Keep message channel open for async response
  }
});

async function handleWritingRequest(message) {
  const apiKey = await getApiKey();
  const endpoint = 'https://api.anthropic.com/v1/messages';

  const systemPrompt = `You are a writing assistant helping developers and technical writers.
Provide concise, helpful suggestions. Format responses as JSON when appropriate.`;

  const userMessage = message.type === 'TEXT_SELECTION'
    ? `Improve this text: "${message.text}"`
    : `Continue or improve this text: "${message.context}"`;

  try {
    const response = await fetch(endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'x-api-key': apiKey,
        'anthropic-version': '2023-06-01'
      },
      body: JSON.stringify({
        model: 'claude-3-haiku-20240307',
        max_tokens: 1024,
        system: systemPrompt,
        messages: [{ role: 'user', content: userMessage }]
      })
    });

    const data = await response.json();
    return { success: true, suggestion: data.content[0].text };
  } catch (error) {
    return { success: false, error: error.message };
  }
}

async function getApiKey() {
  const result = await chrome.storage.local.get(['apiKey']);
  return result.apiKey;
}

Suggestion Display Overlay

Once you receive suggestions from the AI, displaying them effectively is crucial for good user experience. An overlay system that appears near the selected text or input provides the most intuitive interaction:

// content-script.js - Suggestion overlay
function showSuggestionOverlay(element, suggestion) {
  // Remove existing overlay
  const existing = document.getElementById('writing-assistant-overlay');
  if (existing) existing.remove();

  const overlay = document.createElement('div');
  overlay.id = 'writing-assistant-overlay';
  overlay.className = 'writing-assistant-overlay';
  overlay.innerHTML = `
    <div class="suggestion-content">${suggestion}</div>
    <div class="suggestion-actions">
      <button class="accept-btn">Accept</button>
      <button class="dismiss-btn">Dismiss</button>
    </div>
  `;

  // Position near the input element
  const rect = element.getBoundingClientRect();
  overlay.style.position = 'fixed';
  overlay.style.top = `${rect.bottom + window.scrollY + 10}px`;
  overlay.style.left = `${rect.left + window.scrollX}px`;

  // Add event listeners
  overlay.querySelector('.accept-btn').addEventListener('click', () => {
    insertSuggestion(element, suggestion);
    overlay.remove();
  });

  overlay.querySelector('.dismiss-btn').addEventListener('click', () => {
    overlay.remove();
  });

  document.body.appendChild(overlay);
}

function insertSuggestion(element, suggestion) {
  if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
    const start = element.selectionStart;
    const end = element.selectionEnd;
    const text = element.value;
    element.value = text.substring(0, start) + suggestion + text.substring(end);
    element.selectionStart = element.selectionEnd = start + suggestion.length;
  } else {
    // For contenteditable elements
    document.execCommand('insertText', false, suggestion);
  }
}

Key Features for Developer Productivity

Writing assistants for developers often include specialized features beyond basic text improvement.

Code Comment Enhancement

Developers frequently need to write meaningful comments and documentation. A writing assistant can help generate or improve code comments while respecting programming language syntax:

// Specialized prompt for code comments
const commentPrompt = `You are helping a developer write code comments.
Given this code snippet, write clear, concise comments explaining what the code does.
Use appropriate comment syntax for the detected language.

Code: ${selectedCode}`;

Documentation Formatting

When writing technical documentation, extensions can suggest formatting improvements, heading structures, and help maintain consistent style:

// Documentation improvement prompt
const docPrompt = `Improve this documentation text for clarity and readability.
Maintain markdown formatting. Suggest improvements for:
- Heading hierarchy
- Sentence clarity
- Technical accuracy
- Consistent terminology

Text: ${selectedText}`;

Multi-Language Support

For developers working across languages, writing assistants can provide translations, explain foreign language error messages, or help craft messages in multiple languages:

// Translation and explanation helper
const translatePrompt = `Translate this text and explain any technical terms.
If this is an error message, explain what it means and potential solutions.

Text: ${selectedText}
Target language: ${targetLanguage}`;

Configuration and Customization

Effective writing assistants provide users with control over behavior. Store user preferences using Chrome’s storage API:

// Managing user preferences
chrome.runtime.onInstalled.addListener(() => {
  chrome.storage.local.set({
    apiKey: '',
    autoSuggest: false,
    suggestionDelay: 500,
    enabledDomains: ['github.com', 'stackoverflow.com'],
    excludedDomains: []
  });
});

// Retrieve preferences in content script
async function getPreferences() {
  return await chrome.storage.local.get([
    'autoSuggest',
    'suggestionDelay',
    'enabledDomains'
  ]);
}

Performance Considerations

When building production-ready writing assistants, several performance factors require attention. Debouncing input requests prevents excessive API calls while the user is still typing. Caching recent responses reduces redundant API calls for similar content. Lazy loading the extension’s JavaScript ensures it does not impact page load times for unsupported sites.

Implementing these considerations creates a responsive extension that provides value without overwhelming users or incurring unnecessary API costs.


Chrome extension writing assistants represent a practical intersection of AI capabilities and browser functionality. By understanding the core patterns—input detection, API integration, and suggestion display—developers can build customized tools that enhance productivity across the web. Whether improving code comments, drafting documentation, or composing communications, these extensions bring intelligent assistance directly into your workflow.

Built by theluckystrike — More at zovo.one