wvg/background.js

111 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

(async () => {
2024-04-30 18:23:08 +00:00
window.psshs=[];
window.requests=[];
window.bodys=[];
2024-06-11 23:23:29 +00:00
window.targetIds=[];
2024-04-30 18:23:08 +00:00
window.pageURL="";
2024-05-07 19:43:06 +00:00
window.clearkey="";
chrome.storage.local.get("isBlock", (value) => {
2024-06-21 09:50:42 +00:00
window.isBlock = value.isBlock;
})
2024-04-08 22:58:09 +00:00
function convertHeaders(obj){
2024-04-18 20:48:32 +00:00
return JSON.stringify(Object.fromEntries(obj.map(header => [header.name, header.value])))
2024-04-08 22:58:09 +00:00
}
2024-04-26 01:53:24 +00:00
window.blockRules = await fetch("blockRules.conf").then((r)=>r.text());
2024-06-25 01:43:09 +00:00
window.blockRules = window.blockRules.replace(/\n^\s*$|\s*\/\/.*|\s*$/gm, "").split("\n");
function testBlock(url) {
2024-06-25 01:43:09 +00:00
return window.isBlock && window.blockRules.some(e => url.includes(e));
}
2024-04-26 01:53:24 +00:00
//Get URL and headers from POST requests
2024-04-08 22:58:09 +00:00
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"]
2024-04-08 22:58:09 +00:00
);
2024-09-27 05:36:08 +00:00
//Get requestBody from POST requests
2024-04-26 01:53:24 +00:00
chrome.webRequest.onBeforeRequest.addListener(
2024-06-25 02:11:08 +00:00
function(details) {
if (details.method === "POST") {
window.bodys.push({
2024-09-27 05:36:08 +00:00
body:details.requestBody.raw ? btoa(String.fromCharCode(...new Uint8Array(details.requestBody.raw[0]['bytes']))) : "",
id:details.requestId
2024-06-25 02:11:08 +00:00
});
}
},
{urls: ["<all_urls>"]},
["requestBody"]
2024-04-26 01:53:24 +00:00
);
2024-04-08 22:58:09 +00:00
//Receive PSSH from content.js
chrome.runtime.onMessage.addListener(
2024-06-25 02:11:08 +00:00
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;
}
2024-04-08 22:58:09 +00:00
}
);
} )()
2024-04-08 22:58:09 +00:00
chrome.browserAction.onClicked.addListener(tab => {
if(chrome.windows){
chrome.windows.create({
url: "popup.html",
type: "popup",
width: 820,
height: 600
});
} else {
chrome.tabs.create({url: 'popup.html'})
}
2024-04-08 22:58:09 +00:00
});
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) => {
2024-06-21 09:50:42 +00:00
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"})
}
})
}
})