File: technical.html
Purpose: Technical information about architecture, API and internal structure
Purpose: Technical information about architecture, API and internal structure
๐ Project Structure
buxartranslate/
โโโ manifest.json # Extension configuration
โโโ background.js # Background script (main logic)
โโโ popup.html # Popup interface
โโโ popup.js # Popup logic
โโโ icons/ # Extension icons
โ โโโ icon-16.png
โ โโโ icon-32.png
โ โโโ icon-48.png
โ โโโ icon-64.png
โ โโโ icon-128.png
โโโ _locales/ # Localization
โโโ en/
โ โโโ messages.json
โโโ ru/
โ โโโ messages.json
โโโ de/
โ โโโ messages.json
โโโ es/
โ โโโ messages.json
โโโ it/
โ โโโ messages.json
โโโ fr/
โโโ messages.json
๐ Key Files
manifest.json
Type: WebExtension for Thunderbird
API Version: 78.0+
Permissions: storage, menus, notifications, activeTab
background.js
Purpose: Main extension logic
Functions: Context menu, translation API, window management
popup.js
Purpose: Settings interface logic
Functions: Settings management, language search, localization
๐ API and Integrations
Google Translate API
Language detection: https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=ld&q=...
Text translation: https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=...
// Translation function via Google API
async function translateWithGoogle(text, sourceLang, targetLang) {
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${sourceLang}&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Google API error: ${response.status}`);
}
const data = await response.json();
let translatedText = "";
if (data && data[0]) {
data[0].forEach(item => {
if (item[0]) translatedText += item[0];
});
}
return preserveLineBreaks(translatedText || text, text);
}
Thunderbird WebExtension API
browser.menus
Context menu creation and management
browser.menus.create({
id: "translate",
title: menuTitle,
contexts: ["selection"]
});
browser.storage
Settings and data storage
// Save settings
await browser.storage.local.set(settings);
// Load settings
const settings = await browser.storage.local.get(defaultSettings);
browser.notifications
System notifications display
browser.notifications.create({
type: "basic",
iconUrl: "icon.png",
title: title,
message: message
});
browser.windows
Translation windows management
const translationWindow = await browser.windows.create({
url: url,
type: 'popup',
width: 550,
height: 600
});
๐ Messaging System
Communication between extension components:
// Message handler in background.js
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getTranslation") {
browser.storage.local.get("lastTranslation").then(result => {
sendResponse(result);
});
return true;
}
if (request.action === "getSettings") {
// ... settings request handling
}
if (request.action === "saveSettings") {
// ... settings save handling
}
});
๐พ Data Storage
// Data structure in localStorage
{
// User settings
"translateTo": "en",
"autoDetectLanguage": true,
"windowDisplayTime": 30,
"userLang": "auto",
// Recently used languages
"recentLanguages": ["en", "ru", "es", "fr", "de"],
// Translation history
"lastTranslation": {
"original": "ะัะธะฒะตั ะผะธั",
"translated": "Hello world",
"sourceLang": "ru",
"targetLang": "en",
"timestamp": 1700000000000,
"translator": "google"
},
// Opened translation window ID
"translationWindowId": 12345
}
๐ง Extension Architecture
Component Structure
- Background Script - main logic, context menu, API calls
- Popup Interface - user interface for settings
- Translation Window - separate window for translation display
- Localization System - multilingual interface support
Data Flow
- User selects text โ context menu
- Background script gets text โ detects language
- Request to Google Translate API โ gets translation
- Saves translation โ shows notification and window
- Updates popup interface with translation history
๐ก๏ธ Security
HTTPS Connections
All Google API requests are made via secure HTTPS protocol
Local Storage
User settings and data are stored locally in browser
Minimum Permissions
Extension requests only necessary permissions for operation
Error Handling
Comprehensive error handling and display system for users