-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
130 lines (114 loc) · 3.74 KB
/
background.js
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
126
127
128
129
130
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "translateSelection",
title: "翻译选中文本",
contexts: ["selection"]
});
chrome.contextMenus.create({
id: "translatePage",
title: "翻译整个页面",
contexts: ["page"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "translateSelection") {
const selectedText = info.selectionText;
// 在这里处理选中文本的翻译
// 可以打开弹出窗口并填入选中的文本
chrome.windows.create({
url: `popup.html?text=${encodeURIComponent(selectedText)}`,
type: 'popup',
width: 400,
height: 300
});
} else if (info.menuItemId === "translatePage") {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
// 触发页面翻译
document.dispatchEvent(new CustomEvent('translate-page'));
}
});
}
});
// 修改字符统计相关功能
const MONTHLY_CHAR_LIMIT = 500000;
// 获取当前月份的key
function getCurrentMonthKey() {
const now = new Date();
return `${now.getFullYear()}-${now.getMonth() + 1}`;
}
// 重置字符统计
async function resetCharacterCount(apiKey) {
const currentMonth = getCurrentMonthKey();
const storageKey = `characterCount_${apiKey}`;
await chrome.storage.local.set({
[storageKey]: {
month: currentMonth,
count: 0
}
});
}
// 获取字符统计
async function getCharacterCount(apiKey) {
const storageKey = `characterCount_${apiKey}`;
const data = await chrome.storage.local.get(storageKey);
const currentMonth = getCurrentMonthKey();
// 如果是新的月份或新的API key,重置计数
if (!data[storageKey] || data[storageKey].month !== currentMonth) {
await resetCharacterCount(apiKey);
return 0;
}
return data[storageKey].count;
}
// 更新字符统计
async function updateCharacterCount(apiKey, newChars) {
const currentCount = await getCharacterCount(apiKey);
const currentMonth = getCurrentMonthKey();
const storageKey = `characterCount_${apiKey}`;
await chrome.storage.local.set({
[storageKey]: {
month: currentMonth,
count: currentCount + newChars
}
});
}
// 检查字符限制
async function checkCharacterLimit(apiKey, newChars) {
const currentCount = await getCharacterCount(apiKey);
return {
allowed: (currentCount + newChars) <= MONTHLY_CHAR_LIMIT,
current: currentCount,
limit: MONTHLY_CHAR_LIMIT
};
}
// 修改消息监听器
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'updateCharacterCount') {
updateCharacterCount(message.apiKey, message.count)
.then(() => sendResponse({ success: true }))
.catch(error => sendResponse({ success: false, error }));
return true;
}
if (message.action === 'checkCharacterLimit') {
checkCharacterLimit(message.apiKey, message.count)
.then(result => sendResponse(result))
.catch(error => sendResponse({ allowed: false, error }));
return true;
}
if (message.action === 'getCharacterCount') {
getCharacterCount(message.apiKey)
.then(count => sendResponse({ count }))
.catch(error => sendResponse({ error }));
return true;
}
});
// 添加定时检查,每月重置
chrome.alarms.create('resetCharacterCount', {
periodInMinutes: 60 * 24 // 每天检查一次
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'resetCharacterCount') {
getCharacterCount(); // 这会在需要时自动重置
}
});