This repository was archived by the owner on Jun 1, 2025. It is now read-only.
forked from FoxRefire/wvg
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
185 lines (161 loc) · 6 KB
/
background.js
File metadata and controls
185 lines (161 loc) · 6 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(async () => {
// Global state
window.psshs = [];
window.requests = [];
window.bodys = [];
window.targetIds = [];
window.pageURL = "";
window.clearkey = "";
window.isBlock = false;
window.licenseUrlsByTab = {};
window.blockRules = [];
// Load and parse blockRules.conf
try {
const blockText = await fetch(chrome.runtime.getURL("blockRules.conf")).then((r) => r.text());
window.blockRules = blockText
.replace(/\n^\s*$|\s*\/\/.*|\s*$/gm, "")
.split("\n")
.filter(Boolean);
} catch {
window.blockRules = [];
}
function testBlock(url) {
return window.blockRules.some((rule) => url.includes(rule));
}
function convertHeaders(headers) {
return JSON.stringify(Object.fromEntries(headers.map((h) => [h.name, h.value])));
}
function registerBlockListener() {
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
if (details.method === "POST" && window.isBlock && testBlock(details.url)) {
console.log(`Blocking request to: ${details.url}`);
return { cancel: true };
}
},
{ urls: ["<all_urls>"] },
["blocking"]
);
}
// Register the blocking listener immediately
registerBlockListener();
// Load block setting and create menu (after listener is already registered)
chrome.storage.local.get("isBlock", (value) => {
window.isBlock = value?.isBlock || false;
createMenu(); // Initialize menu with proper title
});
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
if (details.method === "POST") {
const raw = details.requestBody?.raw?.[0]?.bytes;
const body = raw ? btoa(String.fromCharCode(...new Uint8Array(raw))) : "";
window.bodys.push({ id: details.requestId, body });
}
},
{ urls: ["<all_urls>"] },
["requestBody"]
);
chrome.webRequest.onBeforeSendHeaders.addListener(
(details) => {
if (details.method === "POST") {
const bodyObj = window.bodys.find((b) => b.id === details.requestId);
const body = bodyObj?.body || "";
window.requests.push({
url: details.url,
headers: convertHeaders(details.requestHeaders),
body,
});
// Fallback for pageURL
if (!window.pageURL) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const fallbackURL = tabs?.[0]?.url;
if (fallbackURL) {
window.pageURL = fallbackURL;
console.log("Fallback pageURL set to:", fallbackURL);
if (!window.licenseUrlsByTab[window.pageURL]) {
window.licenseUrlsByTab[window.pageURL] = [];
}
if (!window.licenseUrlsByTab[window.pageURL].includes(details.url)) {
window.licenseUrlsByTab[window.pageURL].push(details.url);
chrome.storage.local.set({ licenseUrlsByTab: window.licenseUrlsByTab });
}
} else {
console.warn("Could not determine fallback pageURL");
}
});
} else {
if (!window.licenseUrlsByTab[window.pageURL]) {
window.licenseUrlsByTab[window.pageURL] = [];
}
if (!window.licenseUrlsByTab[window.pageURL].includes(details.url)) {
window.licenseUrlsByTab[window.pageURL].push(details.url);
chrome.storage.local.set({ licenseUrlsByTab: window.licenseUrlsByTab });
}
}
if (window.isBlock && testBlock(details.url)) return { cancel: true };
}
},
{ urls: ["<all_urls>"] },
["requestHeaders", "blocking"]
);
// Context menu toggle
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "toggleBlocking") {
window.isBlock = !window.isBlock;
chrome.storage.local.set({ isBlock: window.isBlock });
chrome.contextMenus.update("toggleBlocking", {
title: window.isBlock ? "Disable License Blocking" : "Enable License Blocking",
});
console.log("License blocking toggled:", window.isBlock);
}
});
function createMenu() {
chrome.contextMenus.create({
id: "toggleBlocking",
title: window.isBlock ? "Disable License Blocking" : "Enable License Blocking",
contexts: ["all"],
});
}
// Message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.type) {
case "RESET":
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
const pageURL = tab?.url || "unknown";
window.pageURL = pageURL;
window.licenseUrlsByTab[pageURL] = window.licenseUrlsByTab[pageURL] || [];
chrome.storage.local.set({
pageURL,
licenseUrlsByTab: window.licenseUrlsByTab,
});
});
break;
case "PSSH":
window.psshs.push(request.text);
window.pageURL = sender.tab?.url || "";
window.targetIds = [sender.tab?.id, sender.frameId];
chrome.storage.local.set({ psshs: window.psshs });
break;
case "CLEARKEY":
window.clearkey = request.text;
chrome.storage.local.set({ clearkey: request.text });
break;
case "GET_LICENSE_REQUEST":
const licenseRequest = window.requests.find((r) => r.url === request.url);
sendResponse(licenseRequest);
break;
}
});
// Open popup
chrome.browserAction.onClicked.addListener(() => {
chrome.windows
? chrome.windows.create({
url: "index.html",
type: "popup",
width: 820,
height: 600,
})
: chrome.tabs.create({ url: "index.html" });
});
})();