-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
125 lines (108 loc) · 4.09 KB
/
background.js
File metadata and controls
125 lines (108 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Background script to monitor network requests
let extractedSession = null;
let isSessionFound = false;
let extractionTimeout = null; // 用于防抖的定时器
// 移除防重复复制逻辑,允许每次刷新都重新复制
// Listen for web requests to the Trae API
// Function to extract session from cookies and auto-copy to clipboard
async function extractSessionFromCookies() {
try {
console.log('Attempting to read X-Cloudide-Session cookie from trae.ai domain');
// Get the X-Cloudide-Session cookie from trae.ai domain
const cookie = await chrome.cookies.get({
url: 'https://www.trae.ai',
name: 'X-Cloudide-Session'
});
if (cookie && cookie.value) {
extractedSession = cookie.value;
isSessionFound = true;
console.log('X-Cloudide-Session found via cookies API:', extractedSession);
// 自动复制到剪贴板
const sessionWithPrefix = `X-Cloudide-Session=${extractedSession}`;
await copyToClipboard(sessionWithPrefix);
// 通知content script显示toast
notifyPageSessionCopied();
console.log('Session auto-copied to clipboard:', extractedSession);
// Store the session
chrome.storage.local.set({
traeSession: extractedSession,
sessionFound: true
});
// Update badge
chrome.action.setBadgeText({text: '✓'});
chrome.action.setBadgeBackgroundColor({color: '#4CAF50'});
return true;
} else {
console.log('X-Cloudide-Session cookie not found');
return false;
}
} catch (error) {
console.error('Error reading cookies:', error);
return false;
}
}
// Function to copy text to clipboard
async function copyToClipboard(text) {
try {
// 在background script中,需要通过content script来复制到剪贴板
const tabs = await chrome.tabs.query({active: true, currentWindow: true});
if (tabs.length > 0) {
await chrome.tabs.sendMessage(tabs[0].id, {
action: 'copyToClipboard',
text: text
});
console.log('Text copied to clipboard successfully');
}
} catch (error) {
console.error('Failed to copy to clipboard:', error);
}
}
// Function to notify content script to show toast
function notifyPageSessionCopied() {
// 获取当前活动的trae.ai标签页
chrome.tabs.query({active: true, url: '*://*.trae.ai/*'}, (tabs) => {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'showSessionCopiedToast'
}).catch(error => {
console.log('Could not send message to content script:', error);
});
}
});
}
// Listen for ide_user_pay_status API requests to trigger cookie extraction
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
console.log('Request URL:', details.url);
// Check if this is the ide_user_pay_status API
if (details.url.includes('/ide_user_pay_status')) {
console.log('ide_user_pay_status API detected! Will extract session from the last request...');
// 清除之前的定时器,确保只处理最后一个请求
if (extractionTimeout) {
clearTimeout(extractionTimeout);
console.log('Previous extraction cancelled, waiting for the last request...');
}
// 设置新的定时器,延迟1000ms执行,确保是最后一个请求
extractionTimeout = setTimeout(() => {
console.log('Processing the last ide_user_pay_status request, extracting session from cookies...');
extractSessionFromCookies();
extractionTimeout = null;
}, 1000);
}
},
{urls: ["*://*.trae.ai/*"]},
["requestHeaders"]
);
// Handle extension installation
chrome.runtime.onInstalled.addListener(() => {
// No default badge - only show when token is found
});
// Reset session status when navigating away from trae.ai
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && !tab.url.includes('trae.ai')) {
chrome.storage.local.set({
'sessionFound': false
});
chrome.action.setBadgeText({ text: '' });
}
});