-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
91 lines (78 loc) · 2.61 KB
/
popup.js
File metadata and controls
91 lines (78 loc) · 2.61 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
/* global chrome */
// keys in storage:
// - domains: { [domain]: { headers:[{name,value}], ruleId:number } }
// - nextId: integer (next dynamic rule ID)
const $ = id => document.getElementById(id);
const tableBody = $("rules").querySelector("tbody");
initUI();
$("add").onclick = addOrUpdate;
async function initUI() {
const { domains = {} } = await chrome.storage.sync.get("domains");
tableBody.innerHTML = "";
for (const [dom, info] of Object.entries(domains)) {
insertRow(dom, info.headers);
}
}
function insertRow(domain, headers) {
const tr = document.createElement("tr");
const hdrTxt = headers.map(h => `${h.name}: ${h.value}`).join("\n");
tr.innerHTML = `
<td>${domain}</td>
<td class="headerCell">${hdrTxt}</td>
<td class="del" title="Delete">✖</td>`;
tr.querySelector(".del").onclick = () => removeDomain(domain);
tableBody.appendChild(tr);
}
async function addOrUpdate() {
const dom = $("domain").value.trim().toLowerCase();
const name = $("headerName").value.trim();
const value = $("headerValue").value.trim();
if (!dom || !name) return;
const store = await chrome.storage.sync.get(["domains", "nextId"]);
const domains = store.domains || {};
let nextId = store.nextId || 1;
const hdrs = domains[dom]?.headers || [];
// replace header value if name exists, else push
const existing = hdrs.find(h => h.name.toLowerCase() === name.toLowerCase());
if (existing) existing.value = value; else hdrs.push({ name, value });
// build rule
const ruleId = domains[dom]?.ruleId || nextId++;
const rule = {
id: ruleId,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: hdrs.map(h => ({
header: h.name,
operation: "set",
value: h.value
}))
},
condition: {
requestDomains: [dom],
resourceTypes: ["xmlhttprequest", "main_frame", "sub_frame", "script"]
}
};
// push to DNR
await chrome.declarativeNetRequest.updateDynamicRules({
addRules: [rule],
removeRuleIds: [ruleId] // remove previous copy (if any)
});
// persist
domains[dom] = { headers: hdrs, ruleId };
await chrome.storage.sync.set({ domains, nextId });
// refresh view & clear header boxes
initUI();
$("headerName").value = $("headerValue").value = "";
}
async function removeDomain(dom) {
const store = await chrome.storage.sync.get("domains");
const domains = store.domains || {};
const ruleId = domains[dom]?.ruleId;
if (ruleId) {
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [ruleId] });
}
delete domains[dom];
await chrome.storage.sync.set({ domains });
initUI();
}