This code logs the URL for every resource requested which matches the <all_urls> pattern:
function logURL(requestDetails) {
console.log(`Loading: ${requestDetails.url}`);
}
browser.webRequest.onBeforeRequest.addListener(
logURL,
{urls: ["<all_urls>"]}
);
This code cancels requests for images that are made to URLs under "https://developer.mozilla.org/" (to see the effect, visit any page on MDN that contains images, such as webRequest):
let pattern = "https://developer.mozilla.org/*";
function cancel(requestDetails) {
console.log(`Canceling: ${requestDetails.url}`);
return { cancel: true };
}
browser.webRequest.onBeforeRequest.addListener(
cancel,
{urls: [pattern], types: ["image"]},
["blocking"]
);
This code replaces, by redirection, all network requests for images that are made to URLs under "https://developer.mozilla.org/" (to see the effect, visit any page on MDN that contains images, such as webRequest):
let pattern = "https://developer.mozilla.org/*";
function redirect(requestDetails) {
console.log(`Redirecting: ${requestDetails.url}`);
return {
redirectUrl: "https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif"
};
}
browser.webRequest.onBeforeRequest.addListener(
redirect,
{urls:[pattern], types:["image"]},
["blocking"]
);
This code is exactly like the previous example, except that the listener handles the request asynchronously. It returns a Promise
that sets a timer, and resolves with the redirect URL when the timer expires:
let pattern = "https://developer.mozilla.org/*";
let redirectUrl = "https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif";
function redirectAsync(requestDetails) {
console.log(`Redirecting async: ${requestDetails.url}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ redirectUrl });
}, 2000);
});
}
browser.webRequest.onBeforeRequest.addListener(
redirectAsync,
{urls: [pattern], types: ["image"]},
["blocking"]
);
Another example, that redirects all images to a data URL:
let pattern = "https://developer.mozilla.org/*";
let image = `
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<rect style="stroke-width: 10; stroke: #666;" width="100%" height="100%" fill="#d4d0c8" />
<text transform="translate(0, 9)" x="50%" y="50%" width="100%" fill="#666" height="100%" style="text-anchor: middle; font: bold 10pt 'Segoe UI', Arial, Helvetica, Sans-serif;">Blocked</text>
</svg>
`;
function listener(details) {
const redirectUrl = `data:image/svg+xml,${encodeURIComponent(image)}`;
return { redirectUrl };
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: [pattern], types: ["image"]},
["blocking"]
);
Here's another version:
function randomColor() {
return `#${Math.floor(Math.random()*16777215).toString(16)}`;
}
const pattern = "https://developer.mozilla.org/*";
let image = `
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<rect width="100%" height="100%" fill="${randomColor()}"/>
</svg>
`;
function listener(details) {
const redirectUrl = `data:image/svg+xml,${encodeURIComponent(image)}`;
return { redirectUrl };
}
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: [pattern], types: ["image"]},
["blocking"]
);