Update
This commit is contained in:
+29
-32
@@ -1,63 +1,60 @@
|
||||
// ===== 1. TẠO CONTEXT MENU =====
|
||||
console.log('[XAI Background] ✅ Service worker started');
|
||||
|
||||
// ⚠️ SỬA ENDPOINT & KEY
|
||||
const API_URL = 'https://punch-scientific-electrical-antibodies.trycloudflare.com/content-writer/comment';
|
||||
const API_KEY = 'YOUR_API_KEY_HERE';
|
||||
|
||||
// ===== MENU =====
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
chrome.contextMenus.create({
|
||||
id: 'writeComment',
|
||||
title: '✍️ Viết comment',
|
||||
contexts: ['all'],
|
||||
documentUrlPatterns: ['https://x.com/*', 'https://twitter.com/*']
|
||||
documentUrlPatterns: ['https://x.com/*']
|
||||
});
|
||||
});
|
||||
|
||||
// ===== 2. KHI CLICK VÀO MENU =====
|
||||
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||
if (info.menuItemId === 'writeComment') {
|
||||
chrome.tabs.sendMessage(tab.id, { action: 'OPEN_FORM' }).catch(() => {
|
||||
// Nếu content script chưa sẵn sàng (hiếm) thì bỏ qua
|
||||
});
|
||||
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||
if (info.menuItemId !== 'writeComment' || !tab?.id) return;
|
||||
try {
|
||||
await chrome.tabs.sendMessage(tab.id, { action: 'OPEN_FORM' });
|
||||
} catch (err) {
|
||||
console.error('[XAI Background] ❌ Không gọi được content script:', err.message);
|
||||
}
|
||||
});
|
||||
|
||||
// ===== 3. NHẬN YÊU CẦU TỪ CONTENT SCRIPT VÀ GỌI API =====
|
||||
// ===== HANDLE GENERATE =====
|
||||
chrome.runtime.onMessage.addListener((request, sender) => {
|
||||
if (request.action === 'GENERATE_COMMENT') {
|
||||
callYourAPI(request.data, sender.tab.id);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
async function callYourAPI({
|
||||
text,
|
||||
tone,
|
||||
angle,
|
||||
language
|
||||
}, tabId) {
|
||||
try {
|
||||
// ⚠️ THAY BẰNG ENDPOINT CỦA BẠN
|
||||
const API_URL = 'https://gamerdota0042-himalayas.nord:3000';
|
||||
const commentApi='/content-writer/comment'
|
||||
const API_KEY = 'YOUR_API_KEY_HERE'; // Nên chuyển sang chrome.storage nếu publish
|
||||
async function callYourAPI({ text, lang, tone, angle }, tabId) {
|
||||
const payload = { originalPost: text, language:lang, tone, angle };
|
||||
console.log('[XAI Background] ⏳ Gọi API:', API_URL);
|
||||
console.log('[XAI Background] 📤 Payload:', JSON.stringify(payload, null, 2));
|
||||
|
||||
const res = await fetch(`${API_URL}${commentApi}`, {
|
||||
try {
|
||||
const res = await fetch(API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${API_KEY}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
originalPost: text,
|
||||
tone: tone,
|
||||
angle: angle,
|
||||
language,
|
||||
})
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
console.log('[XAI Background] 📥 Status:', res.status, '| JSON:', data);
|
||||
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
|
||||
const data = await res.json();
|
||||
// Giả định API trả về: { comment: "..." }
|
||||
const comment = data.comment || data.text || JSON.stringify(data);
|
||||
|
||||
chrome.tabs.sendMessage(tabId, { action: 'SHOW_RESULT', comment });
|
||||
await chrome.tabs.sendMessage(tabId, { action: 'SHOW_RESULT', comment });
|
||||
} catch (err) {
|
||||
chrome.tabs.sendMessage(tabId, { action: 'SHOW_ERROR', error: err.message });
|
||||
console.error('[XAI Background] ❌ Lỗi:', err.message);
|
||||
await chrome.tabs.sendMessage(tabId, { action: 'SHOW_ERROR', error: err.message });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user