update alram

This commit is contained in:
Admin 2025-09-25 08:48:07 +07:00
parent 07e3a9fcfa
commit ac7edfbc8b
3 changed files with 71 additions and 39 deletions

File diff suppressed because one or more lines are too long

View File

@ -80,49 +80,81 @@ function broadcastToPorts(message: any) {
}); });
} }
function initDailyReloadAlarm() { // function initDailyReloadAlarm() {
// Tên alarm // // Tên alarm
const alarmName = "dailyReload"; // const alarmName = "dailyReload";
// // Xóa alarm cũ nếu có
// chrome.alarms.clear(alarmName, () => {
// // Lấy thời gian hiện tại
// const now = new Date();
// // Thiết lập 2 giờ sáng ngày hôm nay hoặc ngày mai nếu đã quá giờ
// const firstTrigger = new Date();
// firstTrigger.setHours(2, 0, 0, 0); // 2:00:00
// if (firstTrigger.getTime() <= now.getTime()) {
// // Nếu đã qua 2h sáng hôm nay, đặt vào ngày mai
// firstTrigger.setDate(firstTrigger.getDate() + 1);
// }
// const delayInMinutes = (firstTrigger.getTime() - now.getTime()) / 1000 / 60;
// // Tạo alarm
// chrome.alarms.create(alarmName, {
// delayInMinutes,
// periodInMinutes: 24 * 60, // lặp lại mỗi 24h
// });
// console.log(`⏰ Daily reload alarm set for ${firstTrigger}`);
// });
// // Lắng nghe alarm
// chrome.alarms.onAlarm.addListener((alarm) => {
// if (alarm.name === "dailyReload") {
// console.log("🔄 Reloading page (2 AM alarm)");
// // Reload tất cả tab đang mở (hoặc tab cụ thể nếu muốn)
// chrome.tabs.query({}, (tabs) => {
// tabs.forEach((tab) => {
// if (tab.id) chrome.tabs.reload(tab.id);
// });
// });
// }
// });
// }
function initReloadAlarmEvery4Hours() {
const alarmName = "reloadEvery4Hours";
// Xóa alarm cũ nếu có // Xóa alarm cũ nếu có
chrome.alarms.clear(alarmName, () => { chrome.alarms.clear(alarmName, () => {
// Lấy thời gian hiện tại // Tạo alarm chạy sau 1 phút và lặp lại mỗi 4 tiếng
const now = new Date();
// Thiết lập 2 giờ sáng ngày hôm nay hoặc ngày mai nếu đã quá giờ
const firstTrigger = new Date();
firstTrigger.setHours(2, 0, 0, 0); // 2:00:00
if (firstTrigger.getTime() <= now.getTime()) {
// Nếu đã qua 2h sáng hôm nay, đặt vào ngày mai
firstTrigger.setDate(firstTrigger.getDate() + 1);
}
const delayInMinutes = (firstTrigger.getTime() - now.getTime()) / 1000 / 60;
// Tạo alarm
chrome.alarms.create(alarmName, { chrome.alarms.create(alarmName, {
delayInMinutes, delayInMinutes: 1, // chạy lần đầu sau 1 phút
periodInMinutes: 24 * 60, // lặp lại mỗi 24h periodInMinutes: 4 * 60, // 4 giờ = 240 phút
}); });
console.log(`⏰ Daily reload alarm set for ${firstTrigger}`); console.log(`⏰ Reload alarm "${alarmName}" set to repeat every 4 hours`);
}); });
}
// Lắng nghe alarm // Listener nên đặt bên ngoài để không bị mất khi service worker restart
chrome.alarms.onAlarm.addListener((alarm) => { chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "dailyReload") { if (alarm.name === "reloadEvery4Hours") {
console.log("🔄 Reloading page (2 AM alarm)"); console.log(
// Reload tất cả tab đang mở (hoặc tab cụ thể nếu muốn) "🔄 Reloading all tabs (every 4 hours)",
new Date().toLocaleString()
);
// Reload tất cả các tab đang mở
chrome.tabs.query({}, (tabs) => { chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => { tabs.forEach((tab) => {
if (tab.id) chrome.tabs.reload(tab.id); if (tab.id) chrome.tabs.reload(tab.id);
}); });
}); });
} }
}); });
}
initSocket(); initSocket();
initDailyReloadAlarm(); initReloadAlarmEvery4Hours();

View File

@ -45,10 +45,10 @@ contentService.startSyncConversations();
// AUTO SYNC MESAGE PREFIX (INTERNAL) // AUTO SYNC MESAGE PREFIX (INTERNAL)
contentService.autoSyncConversationPrefixMessages(); contentService.autoSyncConversationPrefixMessages();
// setTimeout(() => { // setTimeout(async () => {
// const a = new TeamsChatService(); // const a = new TeamsChatService();
// const message = a.extractAllMessages(); // const conversations = await a.handleGetConversations();
// console.log({ message }); // console.log({ conversations });
// }, 10000); // }, 10000);