mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 14:28:33 +02:00
* proxy-blocker updates These changes replace the use of the deprecated `proxy.register` (PAC file) with `proxy.onRequest` to handle the proxying of requests. * Changes as per feedback * Fixed typo * Addressed feedback relating to issue with variable initialization after enable/disable * Typo fix * The changes as suggested by @wbamberg
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
// Initialize the list of blocked hosts
|
|
let blockedHosts = ["example.com", "example.org"];
|
|
|
|
// Set the default list on installation.
|
|
browser.runtime.onInstalled.addListener(details => {
|
|
browser.storage.local.set({
|
|
blockedHosts: blockedHosts
|
|
});
|
|
});
|
|
|
|
// Get the stored list
|
|
browser.storage.local.get(data => {
|
|
if (data.blockedHosts) {
|
|
blockedHosts = data.blockedHosts;
|
|
}
|
|
});
|
|
|
|
// Listen for changes in the blocked list
|
|
browser.storage.onChanged.addListener(changeData => {
|
|
blockedHosts = changeData.blockedHosts.newValue;
|
|
});
|
|
|
|
// Managed the proxy
|
|
|
|
// Listen for a request to open a webpage
|
|
browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]});
|
|
|
|
// On the request to open a webpage
|
|
function handleProxyRequest(requestInfo) {
|
|
// Read the web address of the page to be visited
|
|
const url = new URL(requestInfo.url);
|
|
// Determine whether the domain in the web address is on the blocked hosts list
|
|
if (blockedHosts.indexOf(url.hostname) != -1) {
|
|
// Write details of the proxied host to the console and return the proxy address
|
|
console.log(`Proxying: ${url.hostname}`);
|
|
return {type: "http", host: "127.0.0.1", port: 65535};
|
|
}
|
|
// Return instructions to open the requested webpage
|
|
return {type: "direct"};
|
|
}
|
|
|
|
// Log any errors from the proxy script
|
|
browser.proxy.onError.addListener(error => {
|
|
console.error(`Proxy error: ${error.message}`);
|
|
});
|
|
|
|
|
|
|