Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
110 lines (100 loc) · 5.59 KB
/
Copy pathbackground.js
File metadata and controls
110 lines (100 loc) · 5.59 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Service worker: opens the side panel from the toolbar icon, clears settings of removed features, checks for a
// new version (toolbar badge + one notification per version), and makes
// every request the extension sends itself (Jira, NetBox, the catalog) look like a plain API client
// instead of a browser: drop the Origin/Referer/Sec-* headers Chrome adds and replace the browser User-Agent.
importScripts("common.js"); // the update check and badge are shared with the panel and Settings
const PLAIN_CLIENT_RULE_ID = 1;
const BROWSER_HEADERS = ["origin", "referer", "sec-fetch-site", "sec-fetch-mode", "sec-fetch-dest", "sec-ch-ua", "sec-ch-ua-mobile", "sec-ch-ua-platform"];
async function installPlainClientRule() {
const rule = (headers) => ({
id: PLAIN_CLIENT_RULE_ID,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
...headers.map((header) => ({ header, operation: "remove" })),
{ header: "user-agent", operation: "set", value: `lambda-rackside/${chrome.runtime.getManifest().version}` }
]
},
condition: {
initiatorDomains: [chrome.runtime.id],
resourceTypes: ["xmlhttprequest", "other"]
}
});
try {
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [PLAIN_CLIENT_RULE_ID], addRules: [rule(BROWSER_HEADERS)] });
} catch (e) {
// Some Chrome versions refuse to touch Sec-* headers; Origin and User-Agent are the ones that matter.
console.warn("plain client rule", e);
await chrome.declarativeNetRequest
.updateDynamicRules({ removeRuleIds: [PLAIN_CLIENT_RULE_ID], addRules: [rule(["origin", "referer"])] })
.catch((e2) => console.warn("plain client rule", e2));
}
}
installPlainClientRule();
// Clicking the toolbar icon opens the side panel.
function enablePanel() {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch(console.error);
}
chrome.runtime.onInstalled.addListener((details) => {
enablePanel();
newVersionCheck().catch((e) => console.warn("update check", e)); // after an update this clears the badge
if (details.reason === "install") chrome.runtime.openOptionsPage();
// Slack direct-message lookup was removed in 1.7.2: forget its saved token, address and member list.
// 1Password CLI logins were removed in 1.8.0: forget those settings too.
chrome.storage.local.remove(["slackToken", "slackUrl", "slackDir", "opVault", "opAccount", "opTag", "opFallbackItem", "bmcLogin", "bmcHealthAuto"]);
// The AI moved to your browser chat in 1.36.0 (no API key): forget the old keys, models and provider.
chrome.storage.local.remove(OLD_AI_SETTINGS);
// 1.37.0 picks the chat you're signed in to (Auto). 1.36.0 saved "claude" as its default: move that to Auto.
if (details.reason === "update" && details.previousVersion === "1.36.0") {
chrome.storage.local.get("aiChat").then(({ aiChat }) => aiChat === "claude" && chrome.storage.local.set({ aiChat: "auto" }));
}
// The parts catalog moved to a new sheet in 1.16.0: move anyone still on an old one.
chrome.storage.local.get("catalogUrl").then(({ catalogUrl }) => {
if (/14oA5dZCMhyGyqI0yXAniO8rNkANbIh8mtG8NI3dz1n4|1iBI8eWuq0Ks3L6YQb2r8F8kLK4QSoyBt/.test(catalogUrl || "")) chrome.storage.local.remove("catalogUrl");
});
});
chrome.runtime.onStartup.addListener(() => {
enablePanel();
newVersionCheck();
});
// ---------- Lambda OS link ----------
// Lambda OS (the new-tab dashboard, a separate extension) sends "hello" and "rackside.open" here. Its ID is
// remembered from the sender so the panel can link back. sidePanel.open must run before any await, or Chrome
// drops the click that allows it.
chrome.runtime.onMessageExternal.addListener((msg, sender, reply) => {
if (msg?.app !== "lambda-os" || !sender.id) return;
chrome.storage.local.set({ peerLambdaOS: sender.id });
if (msg.type === "hello") { reply({ ok: true, version: chrome.runtime.getManifest().version }); return; }
if (msg.type === "rackside.open") {
if (typeof msg.windowId !== "number") { reply({ ok: true, panel: false }); return; }
chrome.sidePanel.open({ windowId: msg.windowId })
.then(() => reply({ ok: true, panel: true }), (e) => reply({ ok: true, panel: false, error: String(e?.message || e) }));
return true;
}
});
// ---------- New version alert ----------
// Every 2 hours while Chrome runs (and at startup / after an update): a "NEW" badge on the toolbar icon while a
// newer version is waiting, and one notification per new version. Clicking it opens Settings at Updates.
const UPDATE_ALARM = "updateCheck";
async function newVersionCheck() {
const u = await checkForUpdate();
await showUpdateBadge(u);
if (!u.available) return;
const { updateNotified } = await chrome.storage.local.get("updateNotified");
if (updateNotified === u.latest) return;
await chrome.storage.local.set({ updateNotified: u.latest });
chrome.notifications.create("update", {
type: "basic", iconUrl: "icons/icon128.png", priority: 1,
title: `Lambda Rackside ${u.latest} is available`,
message: `You have ${u.current}. Click to update in one click.`
});
}
// Created once: re-creating it each time the worker wakes would keep pushing the next check back.
chrome.alarms.get(UPDATE_ALARM).then((a) => a || chrome.alarms.create(UPDATE_ALARM, { delayInMinutes: 1, periodInMinutes: 120 }));
chrome.alarms.onAlarm.addListener((a) => a.name === UPDATE_ALARM && newVersionCheck().catch((e) => console.warn("update check", e)));
chrome.notifications.onClicked.addListener((id) => {
if (id !== "update") return;
chrome.notifications.clear(id);
chrome.tabs.create({ url: chrome.runtime.getURL("options.html#updates") });
});