-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
139 lines (126 loc) · 3.82 KB
/
Copy pathbackground.js
File metadata and controls
139 lines (126 loc) · 3.82 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const RULE_ID = 1;
const RULE_TYPES = [
"main_frame",
"sub_frame",
"xmlhttprequest",
"script",
"image",
"stylesheet",
"font",
"media",
"ping",
"object",
"other"
];
const defaults = {
enabled: true,
lang: "auto",
activeGroupId: "default",
groups: [
{
id: "default",
name: "debug",
headers: [{ name: "", value: "" }]
}
]
};
const HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
const LANGS = ["auto", "en", "zh"];
chrome.runtime.onInstalled.addListener(async () => {
await saveState(await getState());
await applyActiveGroup();
});
chrome.runtime.onStartup.addListener(applyActiveGroup);
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
(async () => {
if (message?.type === "saveState") {
await saveState(message.state);
await applyActiveGroup();
}
sendResponse(await getState());
})();
return true;
});
async function getState() {
const state = await chrome.storage.local.get(defaults);
const groups = normalizeGroups(state.groups);
const activeGroupId = groups.some(group => group.id === state.activeGroupId) ? state.activeGroupId : groups[0].id;
return {
enabled: state.enabled !== false,
lang: normalizeLang(state.lang),
activeGroupId,
groups
};
}
async function saveState(next) {
await chrome.storage.local.set({
enabled: next.enabled !== false,
lang: normalizeLang(next.lang),
activeGroupId: next.activeGroupId,
groups: normalizeGroups(next.groups)
});
}
function normalizeGroups(groups) {
const cleaned = (Array.isArray(groups) ? groups : [])
.map(group => ({
id: group.id || crypto.randomUUID(),
name: String(group.name || "Group").trim().slice(0, 24) || "Group",
headers: (Array.isArray(group.headers) ? group.headers : [])
.map(header => ({
name: String(header.name ?? ""),
value: String(header.value ?? "")
}))
}))
.filter(group => group.name);
return cleaned.length ? cleaned : defaults.groups;
}
async function applyActiveGroup() {
const state = await getState();
const group = activeGroup(state);
const headers = group.headers.filter(header => HEADER_NAME_RE.test(header.name));
const removeRuleIds = [RULE_ID];
const addRules = state.enabled && headers.length
? [
{
id: RULE_ID,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: headers.map(header => ({
header: header.name,
operation: "set",
value: header.value
}))
},
condition: {
urlFilter: "*",
resourceTypes: RULE_TYPES
}
}
]
: [];
try {
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds, addRules });
await chrome.action.setBadgeText({ text: state.enabled ? firstBadgeChar(group.name) : "OFF" });
await chrome.action.setBadgeBackgroundColor({ color: state.enabled && headers.length ? "#2563eb" : "#64748b" });
} catch {
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds });
await chrome.action.setBadgeText({ text: "!" });
await chrome.action.setBadgeBackgroundColor({ color: "#dc2626" });
}
await chrome.action.setTitle({ title: `HeaderHack: ${state.enabled ? group.name : offText(state.lang)}` });
}
function firstBadgeChar(name) {
const char = Array.from(String(name || "?").trim())[0] || "?";
return char;
}
function activeGroup(state) {
return state.groups.find(group => group.id === state.activeGroupId) || state.groups[0];
}
function normalizeLang(lang) {
return LANGS.includes(lang) ? lang : "auto";
}
function offText(lang) {
const locale = lang === "zh" || (lang === "auto" && chrome.i18n.getUILanguage().startsWith("zh")) ? "zh" : "en";
return locale === "zh" ? "已关闭" : "Off";
}