mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 14:28:33 +02:00
* Update `proxy-blocker` extension to be compatible with firefox 56+ * Change `DIRECT 1234` to `DIRECT` see bug https://bugzilla.mozilla.org/show_bug.cgi?id=1355198
56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
// Location of the proxy script, relative to manifest.json
|
|
const proxyScriptURL = "proxy/proxy-script.js";
|
|
|
|
// Default settings. If there is nothing in storage, use these values.
|
|
const defaultSettings = {
|
|
blockedHosts: ["example.com", "example.org"]
|
|
}
|
|
|
|
// Register the proxy script
|
|
browser.proxy.register(proxyScriptURL);
|
|
|
|
// Log any errors from the proxy script
|
|
browser.proxy.onProxyError.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);
|