Update
This commit is contained in:
+31
-18
@@ -1,34 +1,47 @@
|
||||
console.log('[XAI Background] ✅ Service worker started');
|
||||
|
||||
// ===== TẠO MENU =====
|
||||
// ===== TẠO MENU: XÓA CŨ → TẠO MỚI, KHÔNG GỘP =====
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
chrome.contextMenus.create({
|
||||
id: 'writeComment',
|
||||
title: '✍️ Viết comment',
|
||||
contexts: ['all'],
|
||||
documentUrlPatterns: ['https://x.com/*']
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: 'translateText',
|
||||
title: '🌐 Dịch văn bản',
|
||||
contexts: ['selection'],
|
||||
documentUrlPatterns: ['https://x.com/*', 'http://*/*', 'https://*/*']
|
||||
chrome.contextMenus.removeAll(() => {
|
||||
// Menu 1: Comment — KHÔNG cần bôi đen, chỉ trên X
|
||||
chrome.contextMenus.create({
|
||||
id: 'writeComment',
|
||||
title: '✍️ Viết comment',
|
||||
contexts: ['all'],
|
||||
documentUrlPatterns: ['https://x.com/*']
|
||||
});
|
||||
|
||||
// Menu 2: Dịch — CHỈ hiện khi đã bôi đen text, có thể dùng trên mọi trang
|
||||
chrome.contextMenus.create({
|
||||
id: 'translateText',
|
||||
title: '🌐 Dịch văn bản',
|
||||
contexts: ['selection'],
|
||||
documentUrlPatterns: ['https://x.com/*', 'http://*/*', 'https://*/*']
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ===== CONTEXT MENU CLICK =====
|
||||
// ===== XỬ LÝ CLICK MENU =====
|
||||
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||
if (!tab?.id) return;
|
||||
|
||||
if (info.menuItemId === 'writeComment') {
|
||||
// Gửi xuống content: mở form comment
|
||||
chrome.tabs.sendMessage(tab.id, { action: 'OPEN_FORM' }).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.menuItemId === 'translateText' && info.selectionText) {
|
||||
chrome.tabs.sendMessage(tab.id, { action: 'OPEN_TRANSLATE_FORM', text: info.selectionText }).catch(() => {});
|
||||
// Gửi xuống content: mở tab Dịch + fill text sẵn
|
||||
chrome.tabs.sendMessage(tab.id, {
|
||||
action: 'OPEN_TRANSLATE_FORM',
|
||||
text: info.selectionText
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
// ===== MESSAGE LISTENER =====
|
||||
// ===== NHẬN MESSAGE TỪ CONTENT =====
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.action === 'GENERATE_COMMENT') {
|
||||
handleGenerate(request.data, sender.tab.id);
|
||||
@@ -44,7 +57,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
return false;
|
||||
});
|
||||
|
||||
// ===== COMMENT API =====
|
||||
// ===== GỌI API COMMENT =====
|
||||
async function handleGenerate({ text, lang, tone, angle }, tabId) {
|
||||
const cfg = await chrome.storage.local.get(['apiUrl', 'apiKey']);
|
||||
if (!cfg.apiUrl || !cfg.apiKey) {
|
||||
@@ -78,7 +91,7 @@ async function handleGenerate({ text, lang, tone, angle }, tabId) {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== TRANSLATE API =====
|
||||
// ===== GỌI API DỊCH =====
|
||||
async function handleTranslate({ text, target_lang }, tabId) {
|
||||
const cfg = await chrome.storage.local.get(['apiUrl','apiKey','translateUrl','translateKey']);
|
||||
const url = cfg.translateUrl || cfg.apiUrl;
|
||||
@@ -87,7 +100,7 @@ async function handleTranslate({ text, target_lang }, tabId) {
|
||||
if (!url || !key) {
|
||||
await chrome.tabs.sendMessage(tabId, {
|
||||
action: 'SHOW_TRANSLATE_ERROR',
|
||||
error: '⚠️ Chưa cấu hình API.\n\n👉 Vào tab ⚙️ Config để nhập URL và Key.'
|
||||
error: '⚠️ Chưa cấu hình API.'
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user