108 lines
2.8 KiB
JavaScript
108 lines
2.8 KiB
JavaScript
|
|
// Track if monitoring is active
|
||
|
|
let isTracking = false;
|
||
|
|
let trackedTabs = new Map();
|
||
|
|
|
||
|
|
// Listen for messages from popup or content script
|
||
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||
|
|
if (message.action === 'startTracking') {
|
||
|
|
startTracking();
|
||
|
|
sendResponse({ status: 'started' });
|
||
|
|
} else if (message.action === 'stopTracking') {
|
||
|
|
stopTracking();
|
||
|
|
sendResponse({ status: 'stopped' });
|
||
|
|
} else if (message.action === 'getStatus') {
|
||
|
|
sendResponse({ isTracking, tabCount: trackedTabs.size });
|
||
|
|
} else if (message.action === 'getAllTabs') {
|
||
|
|
getAllTabs().then(tabs => {
|
||
|
|
sendResponse({ tabs });
|
||
|
|
});
|
||
|
|
return true; // Keep channel open for async response
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Start tracking tabs
|
||
|
|
async function startTracking() {
|
||
|
|
isTracking = true;
|
||
|
|
console.log('Started tracking tabs');
|
||
|
|
|
||
|
|
// Get all current tabs
|
||
|
|
const tabs = await chrome.tabs.query({});
|
||
|
|
tabs.forEach(tab => {
|
||
|
|
if (tab.url && !tab.url.startsWith('chrome://') && !tab.url.startsWith('edge://')) {
|
||
|
|
addTab(tab);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Send initial tabs to web app
|
||
|
|
sendTabsToWebApp();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Stop tracking
|
||
|
|
function stopTracking() {
|
||
|
|
isTracking = false;
|
||
|
|
trackedTabs.clear();
|
||
|
|
console.log('Stopped tracking tabs');
|
||
|
|
|
||
|
|
// Notify web app to clear data
|
||
|
|
sendToWebApp({ action: 'clear' });
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add or update a tab
|
||
|
|
function addTab(tab) {
|
||
|
|
trackedTabs.set(tab.id, {
|
||
|
|
id: tab.id,
|
||
|
|
url: tab.url,
|
||
|
|
title: tab.title || tab.url,
|
||
|
|
favIconUrl: tab.favIconUrl,
|
||
|
|
timestamp: new Date().toISOString()
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get all tabs
|
||
|
|
async function getAllTabs() {
|
||
|
|
return Array.from(trackedTabs.values());
|
||
|
|
}
|
||
|
|
|
||
|
|
// Send tabs to web app
|
||
|
|
async function sendTabsToWebApp() {
|
||
|
|
const tabs = await getAllTabs();
|
||
|
|
sendToWebApp({
|
||
|
|
action: 'updateTabs',
|
||
|
|
tabs: tabs
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Send message to web app via content script
|
||
|
|
function sendToWebApp(data) {
|
||
|
|
chrome.tabs.query({ url: ['https://tab.caesargaming.org/*', 'http://localhost:8080/*'] }, (tabs) => {
|
||
|
|
tabs.forEach(tab => {
|
||
|
|
chrome.tabs.sendMessage(tab.id, data).catch(() => {
|
||
|
|
// Ignore errors if content script not loaded
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Listen for new tabs
|
||
|
|
chrome.tabs.onCreated.addListener((tab) => {
|
||
|
|
if (isTracking && tab.url && !tab.url.startsWith('chrome://') && !tab.url.startsWith('edge://')) {
|
||
|
|
addTab(tab);
|
||
|
|
sendTabsToWebApp();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Listen for tab updates
|
||
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||
|
|
if (isTracking && changeInfo.status === 'complete' && tab.url && !tab.url.startsWith('chrome://') && !tab.url.startsWith('edge://')) {
|
||
|
|
addTab(tab);
|
||
|
|
sendTabsToWebApp();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Listen for closed tabs
|
||
|
|
chrome.tabs.onRemoved.addListener((tabId) => {
|
||
|
|
if (isTracking && trackedTabs.has(tabId)) {
|
||
|
|
trackedTabs.delete(tabId);
|
||
|
|
sendTabsToWebApp();
|
||
|
|
}
|
||
|
|
});
|