-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
64 lines (58 loc) · 2.12 KB
/
Copy pathbackground.js
File metadata and controls
64 lines (58 loc) · 2.12 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
/**
* Co-Dialectic service worker (v0.1.0 scaffold).
*
* Responsibilities (current scaffold):
* - Log lifecycle events for transparency.
* - Route status queries from the popup to the active tab's content script.
*
* Responsibilities (planned for v0.2+):
* - Coordinate Prompt API session lifecycle (chrome.ai.languageModel).
* - Maintain per-site preferences from chrome.storage.local.
*
* No telemetry. No remote calls. No analytics.
*/
const EXTENSION_NAME = "Co-Dialectic";
const EXTENSION_VERSION = "0.1.0";
chrome.runtime.onInstalled.addListener((details) => {
// eslint-disable-next-line no-console
console.info(
`[${EXTENSION_NAME} v${EXTENSION_VERSION}] installed (reason=${details.reason})`
);
});
chrome.runtime.onStartup.addListener(() => {
// eslint-disable-next-line no-console
console.info(`[${EXTENSION_NAME} v${EXTENSION_VERSION}] service worker started`);
});
/**
* Forward popup -> content-script status requests.
*
* The popup sends { type: "codi:get-active-tab-status" }. We resolve the
* active tab in the active window, dispatch a "codi:status" probe to its
* content script, and relay the response.
*/
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!message || message.type !== "codi:get-active-tab-status") {
return false;
}
chrome.tabs
.query({ active: true, currentWindow: true })
.then(([tab]) => {
if (!tab || tab.id === undefined) {
sendResponse({ active: false, reason: "no-active-tab" });
return;
}
chrome.tabs
.sendMessage(tab.id, { type: "codi:status" })
.then((response) => sendResponse(response || { active: false }))
.catch(() => {
// Most common cause: page is not in our match list, so the
// content script never loaded. That's a normal "inactive" case.
sendResponse({ active: false, reason: "no-content-script" });
});
})
.catch((err) => {
sendResponse({ active: false, reason: "tabs-query-failed", error: String(err) });
});
// Returning true keeps the message channel open for the async response.
return true;
});