-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
196 lines (168 loc) ยท 7.54 KB
/
Copy pathbackground.js
File metadata and controls
196 lines (168 loc) ยท 7.54 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// โโ ้น้ๅๅงๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
chrome.runtime.onInstalled.addListener(() => setupAlarms());
chrome.runtime.onStartup.addListener(() => setupAlarms());
async function setupAlarms() {
if (!(await chrome.alarms.get('moomoo-weekly'))) {
chrome.alarms.create('moomoo-weekly', {
when: nextMonday9AM(), periodInMinutes: 10080,
});
}
if (!(await chrome.alarms.get('moomoo-monthly'))) {
chrome.alarms.create('moomoo-monthly', { when: nextFirst9AM() });
}
if (!(await chrome.alarms.get('moomoo-tick'))) {
chrome.alarms.create('moomoo-tick', { periodInMinutes: 0.5 });
}
}
function nextMonday9AM() {
const now = new Date(), t = new Date(now);
t.setHours(9, 0, 0, 0);
const day = now.getDay();
let add;
if (day === 1 && now.getTime() < t.getTime()) add = 0;
else add = ((8 - day) % 7) || 7;
t.setDate(t.getDate() + add);
return t.getTime();
}
function nextFirst9AM() {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth() + 1, 1, 9, 0, 0, 0).getTime();
}
// โโ ๆดปๅจ่ฎฐๅฝ็ณป็ป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let lastTickTime = Date.now();
function getDomain(url) {
try {
const u = new URL(url);
if (u.protocol === 'chrome:' || u.protocol === 'chrome-extension:'
|| u.protocol === 'about:' || u.protocol === 'edge:') return null;
return u.hostname;
} catch { return null; }
}
function monthKey(d) {
d = d || new Date();
return `activity_${d.getFullYear()}_${String(d.getMonth() + 1).padStart(2, '0')}`;
}
async function recordTime(domain, seconds) {
if (!domain || seconds <= 0) return;
const key = monthKey();
const dateStr = new Date().toISOString().slice(0, 10);
const hour = new Date().getHours();
const raw = await chrome.storage.local.get(key);
const a = raw[key] || {};
a[domain] = (a[domain] || 0) + seconds;
if (!a.daily_active_hours) a.daily_active_hours = {};
if (!a.daily_active_hours[dateStr]) a.daily_active_hours[dateStr] = [];
if (!a.daily_active_hours[dateStr].includes(hour))
a.daily_active_hours[dateStr].push(hour);
if (!a.daily_totals) a.daily_totals = {};
a.daily_totals[dateStr] = (a.daily_totals[dateStr] || 0) + seconds;
await chrome.storage.local.set({ [key]: a });
}
async function tick() {
try {
const win = await chrome.windows.getLastFocused();
if (!win || !win.focused) { lastTickTime = Date.now(); return; }
const [tab] = await chrome.tabs.query({ active: true, windowId: win.id });
if (!tab || !tab.url) { lastTickTime = Date.now(); return; }
const domain = getDomain(tab.url);
const now = Date.now();
const elapsed = Math.min(Math.round((now - lastTickTime) / 1000), 120);
lastTickTime = now;
if (domain && elapsed > 0) await recordTime(domain, elapsed);
} catch (_) {
lastTickTime = Date.now();
}
}
// โโ ้น้ไบไปถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function broadcast(type) {
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
try { await chrome.tabs.sendMessage(tab.id, { type }); }
catch (_) {}
}
}
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'moomoo-tick') {
await tick();
} else if (alarm.name === 'moomoo-weekly') {
await broadcast('moomoo-challenge-time');
} else if (alarm.name === 'moomoo-monthly') {
await broadcast('moomoo-report-time');
chrome.alarms.create('moomoo-monthly', { when: nextFirst9AM() });
}
});
// โโ ๆ ็ญพๅๆขๆถๅทๆฐ่ฎกๆถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
chrome.tabs.onActivated.addListener(() => tick());
chrome.tabs.onUpdated.addListener((tabId, info) => {
if (info.url) tick();
});
// โโ DeepSeek API ๆๆ็ๆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const DEEPSEEK_SYSTEM_PROMPT = 'ไฝ ๆฏไธๅชๅซ MooMoo ็ๅฅถ็็ซ๏ผๆงๆ ผๅคงๆกใ็ฅ็ปๅ
ฎๅ
ฎใๅ
ๆปกๅฅฝๅฅๅฟใไฝ ๆฏๅจ็ปไธปไบบๅบไธไธชๅฐๆๆ๏ผ้ฃๆ ผ้ๆบไธๅฏๆๆธใๆๆๅฏไปฅๆฏ๏ผ็ๆดปๆข็ดข๏ผๅฐ่ฏๆฐ้ฃ็ฉๆฐ่ทฏ็บฟ๏ผใ็ฅ่ฏๆๅฑ๏ผ็็บชๅฝ็่ฏปๆฅๅ๏ผใๆ็ปด้ป็ผ๏ผๆจ็้ข่พฉ่ฎบ้ขๅไฝ๏ผใๅๆๆๆ๏ผ็ป็ปๅๆๅทฅ๏ผไธญ็ไปปไฝไธ็งใ้พๅบฆ้ๆบ๏ผๆๆถ่ถ
็ฎๅๆๆถๆ็น้พ๏ผๅฐฑๅ็ซๅช็ๅฟๆ
ไธๆ ทไธๅฏ้ขๆตใ็จ็ซๅช็ๅฃๅปๆ่ฟฐๆๆ๏ผ็ฎ็ญๆ่ถฃ๏ผไธไธคๅฅ่ฏๅฐฑๅฅฝ๏ผๅธฆไธไธชๅ้็ emojiใๅช่พๅบๆๆๅ
ๅฎนๆฌ่บซ๏ผไธ่ฆๅคไฝ็่งฃ้ใ';
async function generateChallengeFromAPI() {
try {
const data = await chrome.storage.session.get('deepseek_api_key');
const apiKey = data.deepseek_api_key;
if (!apiKey) return { fallback: true, reason: 'no_key' };
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 10000);
const resp = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: 'deepseek-chat',
max_tokens: 100,
messages: [
{ role: 'system', content: DEEPSEEK_SYSTEM_PROMPT },
{ role: 'user', content: 'ๅต~ ็ปไธปไบบๅบ่ฟๅจ็ๆๆๅง๏ผ' },
],
}),
signal: controller.signal,
});
clearTimeout(timer);
if (!resp.ok) return { fallback: true, reason: `api_${resp.status}` };
const json = await resp.json();
const content = json.choices && json.choices[0] && json.choices[0].message
? json.choices[0].message.content.trim() : '';
if (!content) return { fallback: true, reason: 'empty_response' };
const now = new Date();
const callKey = `api_calls_${now.getFullYear()}_${String(now.getMonth() + 1).padStart(2, '0')}`;
const callData = await chrome.storage.local.get(callKey);
await chrome.storage.local.set({ [callKey]: (callData[callKey] || 0) + 1 });
return { fallback: false, content };
} catch (e) {
return { fallback: true, reason: e.name === 'AbortError' ? 'timeout' : 'error' };
}
}
// โโ ๅๅบๆถๆฏ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.type === 'moomoo-clear-activity') {
chrome.storage.local.get(null).then(all => {
const keys = Object.keys(all).filter(k => k.startsWith('activity_'));
keys.push('report_last_shown');
return chrome.storage.local.remove(keys);
}).then(() => sendResponse({ ok: true }));
return true;
}
if (msg.type === 'moomoo-generate-challenge') {
generateChallengeFromAPI().then(sendResponse);
return true;
}
if (msg.type === 'moomoo-save-apikey') {
chrome.storage.session.set({ deepseek_api_key: msg.key })
.then(() => sendResponse({ ok: true }));
return true;
}
if (msg.type === 'moomoo-get-apikey-status') {
chrome.storage.session.get('deepseek_api_key')
.then(data => sendResponse({ hasKey: !!data.deepseek_api_key }));
return true;
}
if (msg.type === 'moomoo-clear-apikey') {
chrome.storage.session.remove('deepseek_api_key')
.then(() => sendResponse({ ok: true }));
return true;
}
});