161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { io, Socket } from "socket.io-client";
|
|
|
|
const EVENTS = {
|
|
GET_CONVERSATIONS: "messages.get-conversations",
|
|
GET_CONVERSATION: "messages.get-conversation",
|
|
RECEIVE_CONVERSATIONS: "messages.receive-conversations",
|
|
RECEIVE_CONVERSATION: "messages.receive-conversation",
|
|
SEND_MESSAGE: "messages.send-messsage",
|
|
REPLY_MESSAGE: "messages.reply-messsage",
|
|
};
|
|
|
|
let socket: Socket | null = null;
|
|
let ports: chrome.runtime.Port[] = [];
|
|
|
|
function initSocket() {
|
|
if (!socket) {
|
|
socket = io(`${import.meta.env.VITE_WS_URL}`, {
|
|
transports: ["websocket"],
|
|
// path: "/socket.io", // đảm bảo path đúng
|
|
});
|
|
|
|
// Kết nối port từ content script
|
|
chrome.runtime.onConnect.addListener((port) => {
|
|
if (port.name === "message") {
|
|
ports.push(port);
|
|
port.postMessage({ type: "status", msg: "Connected to background" });
|
|
|
|
port.onDisconnect.addListener(() => {
|
|
ports = ports.filter((p) => p !== port);
|
|
});
|
|
}
|
|
|
|
port.onMessage.addListener((msg: IMsg<any>) => {
|
|
console.log(`[${msg.event}] Received from content:`, msg);
|
|
|
|
if (msg.type !== "socket-response") return;
|
|
|
|
socket?.emit(msg.event, msg?.data);
|
|
});
|
|
});
|
|
|
|
// Socket.IO events
|
|
socket.on("connect", () => {
|
|
console.log("✅ Socket.IO connected");
|
|
broadcastToPorts({
|
|
type: "socket",
|
|
event: "connect",
|
|
msg: "Socket.IO connected",
|
|
});
|
|
});
|
|
|
|
// Listent events
|
|
const eventsToListen = [
|
|
EVENTS.GET_CONVERSATIONS,
|
|
EVENTS.GET_CONVERSATION,
|
|
EVENTS.SEND_MESSAGE,
|
|
EVENTS.REPLY_MESSAGE,
|
|
];
|
|
|
|
eventsToListen.forEach((event) => {
|
|
socket?.on(event, (data: any) => {
|
|
broadcastToPorts({
|
|
type: "socket",
|
|
event,
|
|
data,
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
function broadcastToPorts(message: any) {
|
|
ports.forEach((port) => {
|
|
try {
|
|
port.postMessage(message);
|
|
} catch (err) {
|
|
console.warn("❌ Failed to send message:", err);
|
|
}
|
|
});
|
|
}
|
|
|
|
// function initDailyReloadAlarm() {
|
|
// // Tên alarm
|
|
// 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ó
|
|
chrome.alarms.clear(alarmName, () => {
|
|
// Tạo alarm chạy sau 1 phút và lặp lại mỗi 4 tiếng
|
|
chrome.alarms.create(alarmName, {
|
|
delayInMinutes: 1, // chạy lần đầu sau 1 phút
|
|
periodInMinutes: 4 * 60, // 4 giờ = 240 phút
|
|
});
|
|
|
|
console.log(`⏰ Reload alarm "${alarmName}" set to repeat every 4 hours`);
|
|
});
|
|
}
|
|
|
|
// Listener nên đặt bên ngoài để không bị mất khi service worker restart
|
|
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
if (alarm.name === "reloadEvery4Hours") {
|
|
console.log(
|
|
"🔄 Reloading all tabs (every 4 hours)",
|
|
new Date().toLocaleString()
|
|
);
|
|
|
|
// Reload tất cả các tab đang mở
|
|
chrome.tabs.query({}, (tabs) => {
|
|
tabs.forEach((tab) => {
|
|
if (tab.id) chrome.tabs.reload(tab.id);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
initSocket();
|
|
|
|
initReloadAlarmEvery4Hours();
|