Hello,
I have an add-on for Firefox that should cancel specific network request types for a specific website for some duration before allowing requests to flow. It works in development mode but when I signed it with web-ext and add it as permanent add-on; it doesn’t work unless I open the inspection page. What could be the issue?
This is my manifest.json:
{
"manifest_version": 2,
"name": "Cancel",
"version": "1.17",
"description": "Manipulate",
"browser_specific_settings": {
"gecko": {
"id": "h@example.com"
}
},
"browser_action": {
"browser_style": true,
"default_icon": {
"16": "imgs/stop-16.png",
"48": "imgs/stop-48.png",
"128": "imgs/stop-128.png"
}
},
"icons": {
"16": "imgs/stop-16.png",
"48": "imgs/stop-48.png",
"128": "imgs/stop-128.png"
},
"permissions": [
"tabs",
"<all_urls>",
"webNavigation",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["other-old.js"],
"persistent": true
}
}
This is my other-old.js:
browser.runtime.onStartup.addListener(() => {
/*Program start block*/
console.log('It is working!!\n');
/* Code to be executed when a new tab is created*/
function handleCreatedTab(tab) {
console.log("\ninside tab onCreated!");
/*Variables Section*/
let delayAmount = 30;
let requestTime = 0;
/*Webrequest filtering section*/
let webpattern = ["*://*.example.com/*"];
const oFilter = {urls: webpattern,types: ["xmlhttprequest"]};
let xinfo = "blocking";
//const audioType = "mime=audio";
const videoType = "mime=video";
const firstPktRange = "&range=0-";
function onBeforeSendHeadersHandler(requestDetails) {
//check for the type of the requested media and specified range
let lowerCaseUrl = requestDetails.url.toLowerCase();
let finalRequest = requestDetails;
let currentTime = Date.now();
if ((lowerCaseUrl.includes(firstPktRange)) && (lowerCaseUrl.includes(videoType))){
if((currentTime - requestTime) < delayAmount){
finalRequest = { cancel: true };
}
}
return finalRequest;
}
browser.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeadersHandler, oFilter, [xinfo, "requestHeaders"]);
/*Code to be executed when an existing tab is updated*/
const uFilter = { //only execute code when the tab url has changed
properties: ["url"]
}
function handleUpdatedTab(tabId, changeInfo, tabInfo) {
console.log("\n\ninside tab Updated! ",`Updated tab: ${tabId}`);
let videoFlag = changeInfo.url.toLowerCase().includes(“example.com/first”);
let shortsFlag = changeInfo.url.toLowerCase().includes(“example.com/second”);
let correctUrl = videoFlag || shortsFlag;
if(correctUrl){
delayAmount = getRandomDelay();
requestTime = Date.now();
//console.log("the updated delay = ", delayAmount);
}
}
browser.tabs.onUpdated.addListener(handleUpdatedTab, uFilter);
}
browser.tabs.onCreated.addListener(handleCreatedTab);
});
Best,