AI SEO Writing Chrome Extension: A Developer’s Guide
Building an AI-powered SEO writing Chrome extension requires understanding both browser extension architecture and SEO optimization techniques. This guide walks through practical implementation strategies with concrete code examples.
Core Extension Architecture
A functional AI SEO writing extension consists of several key components working together. The manifest file defines the extension’s capabilities, while content scripts handle page interaction. Background scripts manage API communication, and the popup UI provides user controls.
Manifest Configuration
Your manifest.json must declare the necessary permissions:
{
"manifest_version": 3,
"name": "AI SEO Writer",
"version": "1.0",
"permissions": ["activeTab", "storage", "scripting"],
"host_permissions": ["https://api.openai.com/*"],
"action": {
"default_popup": "popup.html",
"default_icon": "icon.png"
}
}
The host_permissions field is critical for API calls to AI services. Without proper declaration, network requests will fail.
Content Script Integration
Content scripts run in the context of web pages and can analyze existing content. Here’s a pattern for extracting page text:
// content.js - Extract page content for SEO analysis
function extractPageContent() {
const selectors = ['article', 'main', '.content', '.post-body'];
for (const selector of selectors) {
const element = document.querySelector(selector);
if (element) {
return {
text: element.innerText,
wordCount: element.innerText.split(/\s+/).length,
headings: element.querySelectorAll('h1, h2, h3').length
};
}
}
return null;
}
// Listen for messages from popup or background
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'analyzeContent') {
const content = extractPageContent();
sendResponse(content);
}
});
This extraction method targets common content areas and provides basic metrics for SEO analysis.
Background Script API Integration
The background script handles communication with AI APIs securely. Never expose API keys in content scripts or popup files:
// background.js - Secure API communication
const API_CONFIG = {
endpoint: 'https://api.openai.com/v1/chat/completions',
model: 'gpt-4',
maxTokens: 1000
};
async function generateSEOContent(prompt) {
const apiKey = await chrome.storage.local.get('apiKey');
if (!apiKey.apiKey) {
throw new Error('API key not configured');
}
const response = await fetch(API_CONFIG.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey.apiKey}`
},
body: JSON.stringify({
model: API_CONFIG.model,
messages: [
{
role: 'system',
content: 'You are an SEO writing assistant. Optimize content for search engines while maintaining readability.'
},
{
role: 'user',
content: prompt
}
],
max_tokens: API_CONFIG.maxTokens
})
});
return response.json();
}
// Handle messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'generateContent') {
generateSEOContent(request.prompt)
.then(data => sendResponse({ success: true, data }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true;
}
});
This approach keeps API keys in chrome.storage, which is more secure than hardcoding them.
Popup UI Implementation
The popup provides the user interface. Use a clean, functional design:
<!-- popup.html -->
<!DOCTYPE html>
<html>
<head>
<style>
body { width: 320px; padding: 16px; font-family: system-ui; }
textarea { width: 100%; height: 80px; margin-bottom: 8px; }
button { background: #0066cc; color: white; border: none; padding: 8px 16px; cursor: pointer; }
button:disabled { background: #ccc; }
#output { margin-top: 12px; padding: 8px; background: #f5f5f5; white-space: pre-wrap; }
.error { color: red; }
</style>
</head>
<body>
<h3>AI SEO Writer</h3>
<textarea id="prompt" placeholder="Enter your SEO writing request..."></textarea>
<button id="generate">Generate</button>
<div id="output"></div>
<script src="popup.js"></script>
</body>
</html>
// popup.js
document.getElementById('generate').addEventListener('click', async () => {
const prompt = document.getElementById('prompt').value;
const output = document.getElementById('output');
output.textContent = 'Generating...';
const response = await chrome.runtime.sendMessage({
action: 'generateContent',
prompt
});
if (response.success) {
output.textContent = response.data.choices[0].message.content;
} else {
output.textContent = 'Error: ' + response.error;
output.classList.add('error');
}
});
SEO Optimization Features
Beyond content generation, effective SEO extensions should provide these capabilities:
Keyword Density Analysis: Calculate how often target keywords appear relative to total word count. Aim for 1-3% density for primary keywords.
Meta Tag Suggestions: Extract and analyze existing meta descriptions and title tags, suggesting improvements based on character count and keyword placement.
Readability Scoring: Implement Flesch-Kincaid or similar metrics to ensure content remains accessible while being SEO-optimized.
Internal Linking Suggestions: Scan page content for opportunities to add relevant internal links based on your site’s existing content.
Configuration and Storage
Store user preferences securely:
// Store API key and preferences
async function saveSettings(settings) {
await chrome.storage.local.set(settings);
}
async function loadSettings() {
return await chrome.storage.local.get(['apiKey', 'defaultModel', 'maxTokens']);
}
Users should configure their API keys through a dedicated settings page rather than hardcoding them in the extension.
Security Considerations
When building AI-powered extensions, follow these security practices:
Never embed API keys in your extension code. Users should provide their own keys stored in chrome.storage.local.
Implement rate limiting to prevent abuse and manage API costs.
Validate all content passed to AI APIs to prevent injection attacks.
Use content security policy headers in your extension to restrict script execution.
Deployment and Testing
Before publishing to the Chrome Web Store:
- Test across different websites with varying DOM structures
- Verify API key storage and retrieval works correctly
- Ensure the extension handles network failures gracefully
- Check that content scripts inject only where needed
- Validate all user inputs are sanitized
Chrome’s developer dashboard provides testing capabilities through developer accounts. Load your unpacked extension for local testing before submission.
Built by theluckystrike — More at zovo.one