feat: add product strategy, agent skills (3 SKILL.md), and Chrome browser extension
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* TradeMate Background Service Worker
|
||||
* Handles context menus and clipboard operations.
|
||||
*/
|
||||
|
||||
import { translate } from '../api/client.js';
|
||||
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
chrome.contextMenus.create({
|
||||
id: 'translate-selection',
|
||||
title: 'TradeMate 翻译选中文本',
|
||||
contexts: ['selection'],
|
||||
});
|
||||
});
|
||||
|
||||
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||
if (info.menuItemId === 'translate-selection') {
|
||||
const selectedText = info.selectionText.trim();
|
||||
if (!selectedText) return;
|
||||
|
||||
try {
|
||||
const result = await translate(selectedText, 'zh');
|
||||
const translated = result.translated_text || '翻译失败';
|
||||
|
||||
// Try to send to content script for display
|
||||
if (tab?.id) {
|
||||
chrome.tabs.sendMessage(tab.id, {
|
||||
action: 'showTranslation',
|
||||
original: selectedText,
|
||||
translated,
|
||||
}).catch(() => {
|
||||
// Fallback: show in notification
|
||||
notifyResult(translated);
|
||||
});
|
||||
} else {
|
||||
notifyResult(translated);
|
||||
}
|
||||
} catch (err) {
|
||||
notifyResult(`错误: ${err.message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function notifyResult(text) {
|
||||
chrome.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: 'icons/icon48.png',
|
||||
title: 'TradeMate 翻译',
|
||||
message: text.slice(0, 200),
|
||||
});
|
||||
}
|
||||
|
||||
// Listen for popup API calls that need background processing
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.action === 'translateContext') {
|
||||
translate(request.text, request.targetLang)
|
||||
.then(result => sendResponse({ ok: true, data: result }))
|
||||
.catch(err => sendResponse({ ok: false, error: err.message }));
|
||||
return true; // Keep channel open for async response
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user