function logURL(requestDetails) {
console.log(`Loading: ${requestDetails.url}`);
}
browser.webRequest.onBeforeRequest.addListener(logURL, {
urls: ["<all_urls>"],
});webRequest.onBeforeRequest / notebook
webRequest.onBeforeRequest
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
// match pattern for the URLs to redirect
let pattern = "https://developer.mozilla.org/*";
// cancel function returns an object
// which contains a property `cancel` set to `true`
function cancel(requestDetails) {
console.log(`Canceling: ${requestDetails.url}`);
return { cancel: true };
}
// add the listener,
// passing the filter argument and "blocking"
browser.webRequest.onBeforeRequest.addListener(
cancel,
{ urls: [pattern], types: ["image"] },
["blocking"],
);// match pattern for the URLs to redirect
let pattern = "https://developer.mozilla.org/*";
// redirect function
// returns an object with a property `redirectURL`
// set to the new URL
function redirect(requestDetails) {
console.log(`Redirecting: ${requestDetails.url}`);
return {
redirectUrl:
"https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif",
};
}
// add the listener,
// passing the filter argument and "blocking"
browser.webRequest.onBeforeRequest.addListener(
redirect,
{ urls: [pattern], types: ["image"] },
["blocking"],
);// match pattern for the URLs to redirect
let pattern = "https://developer.mozilla.org/*";
// URL we will redirect to
let redirectUrl =
"https://38.media.tumblr.com/tumblr_ldbj01lZiP1qe0eclo1_500.gif";
// redirect function returns a Promise
// which is resolved with the redirect URL when a timer expires
function redirectAsync(requestDetails) {
console.log(`Redirecting async: ${requestDetails.url}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ redirectUrl });
}, 2000);
});
}
// add the listener,
// passing the filter argument and "blocking"
browser.webRequest.onBeforeRequest.addListener(
redirectAsync,
{ urls: [pattern], types: ["image"] },
["blocking"],
);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: #666666;" width="100%" height="100%" fill="#d4d0c8" />
<text transform="translate(0, 9)" x="50%" y="50%" width="100%" fill="#666666" height="100%" style="text-anchor: middle; font: bold 10pt 'Segoe UI', "Helvetica", "Arial";">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"],
);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"],
);