feat: 部署初版测试
This commit is contained in:
@@ -5,33 +5,87 @@
|
||||
|
||||
console.log('InsightReply Background Script Loaded');
|
||||
|
||||
const API_BASE = 'http://localhost:8080/api/v1';
|
||||
const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080/api/v1';
|
||||
|
||||
chrome.runtime.onMessage.addListener((message: { type: string; payload?: any }, _sender: chrome.runtime.MessageSender, sendResponse: (response?: any) => void) => {
|
||||
if (message.type === 'SHOW_INSIGHT') {
|
||||
console.log('Received tweet data in background:', message.payload);
|
||||
}
|
||||
|
||||
if (message.type === 'FETCH_CUSTOM_STRATEGIES') {
|
||||
chrome.storage.local.get(['jwt_token'], (res) => {
|
||||
const token = res.jwt_token;
|
||||
if (!token) {
|
||||
sendResponse({ success: false, data: [] });
|
||||
return;
|
||||
}
|
||||
|
||||
fetch(`${API_BASE}/users/me/strategies`, {
|
||||
headers: { 'Authorization': `Bearer ${token}` }
|
||||
})
|
||||
.then(resp => resp.json())
|
||||
.then(data => {
|
||||
if (data.code === 200 && Array.isArray(data.data)) {
|
||||
sendResponse({ success: true, data: data.data });
|
||||
} else {
|
||||
sendResponse({ success: false, data: [] });
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Fetch strategies error:', err);
|
||||
sendResponse({ success: false, data: [] });
|
||||
});
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
if (message.type === 'GENERATE_REPLY') {
|
||||
const { tweetContent, strategy, identity } = message.payload;
|
||||
|
||||
fetch(`${API_BASE}/ai/generate`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
tweet_content: tweetContent,
|
||||
strategy: strategy,
|
||||
identity: identity || 'Independent Developer / Founder'
|
||||
chrome.storage.local.get(['jwt_token'], (res) => {
|
||||
const token = res.jwt_token;
|
||||
if (!token) {
|
||||
sendResponse({ success: false, error: 'unauthorized', message: 'Please log in via the Extension Options page.' });
|
||||
return;
|
||||
}
|
||||
|
||||
fetch(`${API_BASE}/ai/generate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
tweet_content: tweetContent,
|
||||
strategy: strategy,
|
||||
identity: identity || 'Independent Developer / Founder'
|
||||
})
|
||||
})
|
||||
})
|
||||
.then(resp => resp.json())
|
||||
.then(data => {
|
||||
sendResponse({ success: true, data: data.data });
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('API Error:', err);
|
||||
sendResponse({ success: false, error: err.message });
|
||||
});
|
||||
.then(resp => resp.json())
|
||||
.then(data => {
|
||||
const resultData = data.data;
|
||||
sendResponse({ success: true, data: resultData });
|
||||
|
||||
// Save to History Tab
|
||||
if (resultData && resultData.replies) {
|
||||
chrome.storage.local.get(['history'], (res) => {
|
||||
const history = Array.isArray(res.history) ? res.history : [];
|
||||
const newEntry = {
|
||||
timestamp: Date.now(),
|
||||
tweetContent,
|
||||
replies: resultData.replies
|
||||
};
|
||||
const updatedHistory = [newEntry, ...history].slice(0, 50); // Keep last 50
|
||||
chrome.storage.local.set({ history: updatedHistory });
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('API Error:', err);
|
||||
sendResponse({ success: false, error: err.message });
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return true; // Keep channel open for async response
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user