// ==UserScript==
// @name Wikipedia Claude Proofreader
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Proofread Wikipedia articles using Claude API
// @author You
// @match https://*.wikipedia.org/wiki/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
class WikipediaClaudeProofreader {
constructor() {
this.apiKey = localStorage.getItem('claude_api_key');
this.init();
}
init() {
this.createUI();
this.attachEventListeners();
}
createUI() {
// Create floating panel
const panel = document.createElement('div');
panel.id = 'claude-proofreader-panel';
panel.innerHTML = `
<div id="claude-panel-header">
<h3>Claude Proofreader</h3>
<button id="claude-toggle-btn">−</button>
</div>
<div id="claude-panel-content">
<div id="claude-controls">
<button id="claude-set-key-btn" ${this.apiKey ? 'style="display:none"' : ''}>Set API Key</button>
<button id="claude-proofread-btn" ${!this.apiKey ? 'style="display:none"' : ''}>Proofread Article</button>
<button id="claude-change-key-btn" ${!this.apiKey ? 'style="display:none"' : ''}>Change Key</button>
</div>
<div id="claude-results">
<div id="claude-status">Ready to proofread</div>
<div id="claude-output"></div>
</div>
</div>
`;
// Add CSS styles
const style = document.createElement('style');
style.textContent = `
#claude-proofreader-panel {
position: fixed;
top: 20px;
right: 20px;
width: 400px;
max-height: 600px;
background: #fff;
border: 2px solid #0645ad;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 10000;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-size: 14px;
}
#claude-panel-header {
background: #0645ad;
color: white;
padding: 10px 15px;
display: flex;
justify-content: space-between;
align-items: center;
border-radius: 6px 6px 0 0;
cursor: move;
}
#claude-panel-header h3 {
margin: 0;
font-size: 16px;
}
#claude-toggle-btn {
background: none;
border: none;
color: white;
font-size: 18px;
cursor: pointer;
padding: 0;
width: 20px;
height: 20px;
}
#claude-panel-content {
padding: 15px;
max-height: 520px;
overflow-y: auto;
}
#claude-controls {
margin-bottom: 15px;
}
#claude-controls button {
background: #0645ad;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
margin-right: 8px;
margin-bottom: 8px;
font-size: 13px;
}
#claude-controls button:hover {
background: #0a5bb3;
}
#claude-controls button:disabled {
background: #ccc;
cursor: not-allowed;
}
#claude-status {
font-weight: bold;
margin-bottom: 10px;
padding: 8px;
background: #f8f9fa;
border-radius: 4px;
}
#claude-output {
line-height: 1.6;
max-height: 300px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 12px;
border-radius: 4px;
background: #fafafa;
}
#claude-output h1, #claude-output h2, #claude-output h3 {
color: #0645ad;
margin-top: 16px;
margin-bottom: 8px;
}
#claude-output h1 { font-size: 1.4em; }
#claude-output h2 { font-size: 1.2em; }
#claude-output h3 { font-size: 1.1em; }
#claude-output ul, #claude-output ol {
padding-left: 20px;
}
#claude-output p {
margin-bottom: 12px;
}
#claude-output strong {
color: #d33;
}
.claude-collapsed #claude-panel-content {
display: none;
}
.claude-loading {
color: #0645ad;
font-style: italic;
}
.claude-error {
color: #d33;
background: #fef2f2;
border: 1px solid #fecaca;
padding: 8px;
border-radius: 4px;
}
`;
document.head.appendChild(style);
document.body.appendChild(panel);
// Make panel draggable
this.makeDraggable(panel);
}
makeDraggable(element) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
const header = element.querySelector('#claude-panel-header');
header.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
element.style.top = (element.offsetTop - pos2) + "px";
element.style.left = (element.offsetLeft - pos1) + "px";
element.style.right = 'auto';
}
function closeDragElement() {
document.onmouseup = null;
document.onmousemove = null;
}
}
attachEventListeners() {
document.getElementById('claude-toggle-btn').addEventListener('click', () => {
const panel = document.getElementById('claude-proofreader-panel');
const isCollapsed = panel.classList.contains('claude-collapsed');
const toggleBtn = document.getElementById('claude-toggle-btn');
if (isCollapsed) {
panel.classList.remove('claude-collapsed');
toggleBtn.textContent = '−';
} else {
panel.classList.add('claude-collapsed');
toggleBtn.textContent = '+';
}
});
document.getElementById('claude-set-key-btn').addEventListener('click', () => {
this.setApiKey();
});
document.getElementById('claude-change-key-btn').addEventListener('click', () => {
this.setApiKey();
});
document.getElementById('claude-proofread-btn').addEventListener('click', () => {
this.proofreadArticle();
});
}
setApiKey() {
const key = prompt('Enter your Claude API Key:');
if (key && key.trim()) {
this.apiKey = key.trim();
localStorage.setItem('claude_api_key', this.apiKey);
// Update UI
document.getElementById('claude-set-key-btn').style.display = 'none';
document.getElementById('claude-proofread-btn').style.display = 'inline-block';
document.getElementById('claude-change-key-btn').style.display = 'inline-block';
this.updateStatus('API key set successfully!');
}
}
updateStatus(message, isError = false) {
const statusEl = document.getElementById('claude-status');
statusEl.textContent = message;
statusEl.className = isError ? 'claude-error' : '';
}
updateOutput(content, isMarkdown = false) {
const outputEl = document.getElementById('claude-output');
if (isMarkdown) {
// Simple markdown to HTML conversion
content = this.markdownToHtml(content);
outputEl.innerHTML = content;
} else {
outputEl.textContent = content;
}
}
markdownToHtml(markdown) {
return markdown
// Headers
.replace(/^### (.*$)/gim, '<h3>$1</h3>')
.replace(/^## (.*$)/gim, '<h2>$1</h2>')
.replace(/^# (.*$)/gim, '<h1>$1</h1>')
// Bold
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
// Italic
.replace(/\*(.*?)\*/g, '<em>$1</em>')
// Lists
.replace(/^\* (.*$)/gim, '<li>$1</li>')
.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>')
.replace(/^\d+\. (.*$)/gim, '<li>$1</li>')
// Line breaks
.replace(/\n\n/g, '</p><p>')
.replace(/\n/g, '<br>')
// Wrap in paragraphs
.replace(/^(?!<[hul])/gm, '<p>')
.replace(/(?<!>)$/gm, '</p>')
// Clean up
.replace(/<p><\/p>/g, '')
.replace(/<p>(<[hul])/g, '$1')
.replace(/(<\/[hul]>)<\/p>/g, '$1');
}
async proofreadArticle() {
if (!this.apiKey) {
this.updateStatus('Please set your API key first!', true);
return;
}
try {
this.updateStatus('Fetching article content...', false);
const proofreadBtn = document.getElementById('claude-proofread-btn');
proofreadBtn.disabled = true;
// Get current article title
const articleTitle = this.getArticleTitle();
if (!articleTitle) {
throw new Error('Could not extract article title from current page');
}
// Fetch wikicode
const wikicode = await this.fetchWikicode(articleTitle);
if (!wikicode) {
throw new Error('Could not fetch article wikicode');
}
// Check length and warn user
if (wikicode.length > 100000) {
if (!confirm(`This article is quite long (${wikicode.length} characters). Processing may take a while and use significant API credits. Continue?`)) {
this.updateStatus('Operation cancelled by user.');
proofreadBtn.disabled = false;
return;
}
}
this.updateStatus('Processing with Claude... Please wait...');
// Call Claude API
const result = await this.callClaudeAPI(wikicode);
this.updateStatus('Proofreading complete!');
this.updateOutput(result, true);
} catch (error) {
console.error('Proofreading error:', error);
this.updateStatus(`Error: ${error.message}`, true);
this.updateOutput('');
} finally {
document.getElementById('claude-proofread-btn').disabled = false;
}
}
getArticleTitle() {
// Extract title from URL
const url = window.___location.href;
const match = url.match(/\/wiki\/(.+)$/);
return match ? decodeURIComponent(match[1]) : null;
}
async fetchWikicode(articleTitle) {
// Get language from current URL
const language = window.___location.hostname.split('.')[0] || 'en';
const apiUrl = `https://${language}.wikipedia.org/w/api.php?` +
`action=query&titles=${encodeURIComponent(articleTitle)}&` +
`prop=revisions&rvprop=content&format=json&formatversion=2&origin=*`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`Wikipedia API request failed: ${response.status}`);
}
const data = await response.json();
if (!data.query || !data.query.pages || data.query.pages.length === 0) {
throw new Error('No pages found in API response');
}
const page = data.query.pages[0];
if (page.missing) {
throw new Error('Wikipedia page not found');
}
if (!page.revisions || page.revisions.length === 0) {
throw new Error('No revisions found');
}
const content = page.revisions[0].content;
if (!content || content.length < 50) {
throw new Error('Retrieved content is too short');
}
return content;
} catch (error) {
console.error('Error fetching wikicode:', error);
throw error;
}
}
async callClaudeAPI(wikicode) {
const requestBody = {
model: "claude-sonnet-4-20250514",
max_tokens: 4000,
messages: [{
role: "user",
content: `It is currently June 2025. Please proofread this Wikipedia article and identify any typos, grammatical errors and factual inconsistencies. Ignore formatting issues and date inconsistencies. Focus on the actual article content. Format your response as a detailed analysis with specific issues found:\n\n${wikicode}`
}]
};
try {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
'anthropic-version': '2023-06-01',
'anthropic-dangerous-direct-browser-access': 'true'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`API request failed (${response.status}): ${errorText}`);
}
const data = await response.json();
if (!data.content || !data.content[0] || !data.content[0].text) {
throw new Error('Invalid API response format');
}
return data.content[0].text;
} catch (error) {
console.error('Claude API error:', error);
throw error;
}
}
}
// Initialize the proofreader when page loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
new WikipediaClaudeProofreader();
});
} else {
new WikipediaClaudeProofreader();
}
})();