forked from FoxRefire/wvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
131 lines (119 loc) · 3.79 KB
/
background.js
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
(async () => {
window.psshs=[];
window.requests=[];
window.bodys=[];
window.targetIds=[];
window.pageURL="";
window.clearkey="";
window.mpdFiles = "";
window.BaseUrl = "";
chrome.storage.local.get("isBlock", (value) => {
window.isBlock = value.isBlock;
})
function convertHeaders(obj){
return JSON.stringify(Object.fromEntries(obj.map(header => [header.name, header.value])))
}
window.blockRules = await fetch("blockRules.conf").then((r)=>r.text());
window.blockRules = window.blockRules.replace(/\n^\s*$|\s*\/\/.*|\s*$/gm, "").split("\n");
function testBlock(url) {
return window.isBlock && window.blockRules.some(e => url.includes(e));
}
//Get URL and headers from POST requests
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.method === "POST") {
window.requests.push({
url:details.url,
headers:convertHeaders(details.requestHeaders),
body:window.bodys.find((b) => b.id == details.requestId).body
});
if(testBlock(details.url)){
return {cancel:true}
}
}
},
{urls: ["<all_urls>"]},
["requestHeaders", "blocking"]
);
//Get requestBody from POST requests
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.method === "POST") {
window.bodys.push({
body:details.requestBody.raw ? btoa(String.fromCharCode(...new Uint8Array(details.requestBody.raw[0]['bytes']))) : "",
id:details.requestId
});
}
},
{urls: ["<all_urls>"]},
["requestBody"]
);
//Get requestBody from POST requests
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.url.endsWith('.mpd')) {
window.mpdFiles = details.url;
}
},
{ urls: ["<all_urls>"] }, ["requestBody"]
);
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
const videoExtensions = ['.mp4', '.m4s', '.webm', '.ogg', '.mkv', '.avi', '.mov', '.flv'];
const url = details.url.toLowerCase();
if (videoExtensions.some(ext => url.endsWith(ext))) {
window.BaseUrl = details.url;
}
},
{ urls: ["<all_urls>"] },
["requestBody"]
);
//Receive PSSH from content.js
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch(request.type){
case "RESET":
location.reload()
break;
case "PSSH":
window.psshs.push(request.text)
window.pageURL=sender.tab.url
window.targetIds=[sender.tab.id, sender.frameId]
break;
case "CLEARKEY":
window.clearkey=request.text
break;
}
}
);
} )()
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
url: "popup.html",
type: "popup",
width: 820,
height: 600
});
});
function createMenu(){
chrome.storage.local.set({'isBlock': false}, null);
chrome.contextMenus.create({
id: "toggleBlocking",
title: "Enable License Blocking"
});
}
chrome.runtime.onInstalled.addListener(createMenu)
chrome.runtime.onStartup.addListener(createMenu)
chrome.contextMenus.onClicked.addListener(item => {
if(item.menuItemId == "toggleBlocking"){
chrome.storage.local.get("isBlock", (value) => {
if(value.isBlock){
chrome.storage.local.set({'isBlock': false}, null);
chrome.contextMenus.update("toggleBlocking",{title: "Enable License Blocking"})
} else {
chrome.storage.local.set({'isBlock': true}, null);
chrome.contextMenus.update("toggleBlocking",{title: "Disable License Blocking"})
}
})
}
})