Existing ASP.NET API with vanilla JS SPA, WindowWatcher, Chrome extension, and MCP server. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.4 KiB
JavaScript
56 lines
1.4 KiB
JavaScript
const API_BASE = "http://localhost:5200";
|
|
let debounceTimer = null;
|
|
let lastReportedUrl = "";
|
|
let lastReportedTitle = "";
|
|
|
|
function reportTab(tab) {
|
|
if (!tab || !tab.url || tab.url.startsWith("chrome://") || tab.url.startsWith("chrome-extension://")) {
|
|
return;
|
|
}
|
|
|
|
// Skip if same as last reported
|
|
if (tab.url === lastReportedUrl && tab.title === lastReportedTitle) {
|
|
return;
|
|
}
|
|
|
|
// Debounce: wait 2 seconds before reporting
|
|
if (debounceTimer) {
|
|
clearTimeout(debounceTimer);
|
|
}
|
|
|
|
debounceTimer = setTimeout(() => {
|
|
const payload = {
|
|
source: "ChromeExtension",
|
|
appName: "chrome.exe",
|
|
windowTitle: tab.title || "",
|
|
url: tab.url
|
|
};
|
|
|
|
fetch(`${API_BASE}/api/context`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload)
|
|
})
|
|
.then(() => {
|
|
lastReportedUrl = tab.url;
|
|
lastReportedTitle = tab.title;
|
|
})
|
|
.catch(err => console.warn("Failed to report context:", err));
|
|
}, 2000);
|
|
}
|
|
|
|
// Tab switched
|
|
chrome.tabs.onActivated.addListener((activeInfo) => {
|
|
chrome.tabs.get(activeInfo.tabId, (tab) => {
|
|
if (chrome.runtime.lastError) return;
|
|
reportTab(tab);
|
|
});
|
|
});
|
|
|
|
// Navigation within tab
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
|
if (changeInfo.status === "complete" && tab.active) {
|
|
reportTab(tab);
|
|
}
|
|
});
|