mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-18 15:28:37 +02:00
proxy-blocker updates (#422)
* 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
This commit is contained in:
@@ -1,55 +1,48 @@
|
||||
// Location of the proxy script, relative to manifest.json
|
||||
const proxyScriptURL = "proxy/proxy-script.js";
|
||||
// Initialize the list of blocked hosts
|
||||
let blockedHosts = ["example.com", "example.org"];
|
||||
|
||||
// Default settings. If there is nothing in storage, use these values.
|
||||
const defaultSettings = {
|
||||
blockedHosts: ["example.com", "example.org"]
|
||||
}
|
||||
// Set the default list on installation.
|
||||
browser.runtime.onInstalled.addListener(details => {
|
||||
browser.storage.local.set({
|
||||
blockedHosts: blockedHosts
|
||||
});
|
||||
});
|
||||
|
||||
// Register the proxy script
|
||||
browser.proxy.register(proxyScriptURL);
|
||||
// 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.onProxyError.addListener(error => {
|
||||
browser.proxy.onError.addListener(error => {
|
||||
console.error(`Proxy error: ${error.message}`);
|
||||
});
|
||||
|
||||
// Initialize the proxy
|
||||
function handleInit() {
|
||||
// update the proxy whenever stored settings change
|
||||
browser.storage.onChanged.addListener((newSettings) => {
|
||||
browser.runtime.sendMessage(newSettings.blockedHosts.newValue, {toProxyScript: true});
|
||||
});
|
||||
|
||||
// get the current settings, then...
|
||||
browser.storage.local.get()
|
||||
.then((storedSettings) => {
|
||||
// if there are stored settings, update the proxy with them...
|
||||
if (storedSettings.blockedHosts) {
|
||||
browser.runtime.sendMessage(storedSettings.blockedHosts, {toProxyScript: true});
|
||||
// ...otherwise, initialize storage with the default values
|
||||
} else {
|
||||
browser.storage.local.set(defaultSettings);
|
||||
}
|
||||
|
||||
})
|
||||
.catch(()=> {
|
||||
console.log("Error retrieving stored settings");
|
||||
});
|
||||
}
|
||||
|
||||
function handleMessage(message, sender) {
|
||||
// only handle messages from the proxy script
|
||||
if (sender.url != browser.extension.getURL(proxyScriptURL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message === "init") {
|
||||
handleInit(message);
|
||||
} else {
|
||||
// after the init message the only other messages are status messages
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
|
||||
browser.runtime.onMessage.addListener(handleMessage);
|
||||
|
||||
Reference in New Issue
Block a user