diff --git a/proxy-blocker/README.md b/proxy-blocker/README.md new file mode 100644 index 0000000..926db7f --- /dev/null +++ b/proxy-blocker/README.md @@ -0,0 +1,22 @@ +# proxy-filter + +## What it does + +This add-on registers a [PAC script](https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_%28PAC%29_file) using the proxy API. + +The PAC script is initialized with a list of hostnames: it blocks requests to any hosts that are in the list. + +The list is given the following default values: `["example.com", "example.org"]`, but the user can add and remove hosts using the add-on's options page. + +Note that the hostname-matching is very simple: hostnames must exactly match an entry in the list if they are to be blocked. So with the default settings, "example.org" would be blocked but "www.example.org" would be permitted. + +To try it out: +* install it +* try visiting `http://example.com`, and see it is blocked +* visit `about:addons`, open the add-on's preferences, and try changing the hostnames in the text box +* try visiting some different pages, to see the effect of your changes. + +## What it shows + +* How to implement a simple PAC script, and register it using the proxy API. +* How to exchange messages between a PAC script and a background script. diff --git a/proxy-blocker/background/proxy-handler.js b/proxy-blocker/background/proxy-handler.js new file mode 100644 index 0000000..098089c --- /dev/null +++ b/proxy-blocker/background/proxy-handler.js @@ -0,0 +1,55 @@ +// 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.registerProxyScript(proxyScriptURL); + +// Log any errors from the proxy script +browser.proxy.onProxyError.addListener(error => { + console.error(`Proxy error: ${error.message}`); +}); + +// Initialize the proxy +function handleInit(message) { + // 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); diff --git a/proxy-blocker/icons/LICENSE b/proxy-blocker/icons/LICENSE new file mode 100644 index 0000000..f39164e --- /dev/null +++ b/proxy-blocker/icons/LICENSE @@ -0,0 +1 @@ +The "lock".svg" icon is taken from the Material Core iconset and is used under the terms of its license: https://www.iconfinder.com/iconsets/material-core. diff --git a/proxy-blocker/icons/block.svg b/proxy-blocker/icons/block.svg new file mode 100644 index 0000000..da360e2 --- /dev/null +++ b/proxy-blocker/icons/block.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/proxy-blocker/manifest.json b/proxy-blocker/manifest.json new file mode 100644 index 0000000..f5c66ef --- /dev/null +++ b/proxy-blocker/manifest.json @@ -0,0 +1,31 @@ +{ + + "manifest_version": 2, + "name": "Proxy-blocker", + "description": "Uses the proxy API to block requests to specific hosts.", + "version": "1.0", + + "icons": { + "48": "icons/block.svg", + "96": "icons/block.svg" + }, + + "applications": { + "gecko": { + "strict_min_version": "55.0a1" + } + }, + + "background": { + "scripts": [ + "background/proxy-handler.js" + ] + }, + + "options_ui": { + "page": "options/options.html" + }, + + "permissions": ["proxy", "storage"] + +} diff --git a/proxy-blocker/options/options.css b/proxy-blocker/options/options.css new file mode 100644 index 0000000..c7f1cc5 --- /dev/null +++ b/proxy-blocker/options/options.css @@ -0,0 +1,14 @@ +body { + width: 25em; + font-family: "Open Sans Light", sans-serif; + font-size: 0.9em; + font-weight: 300; +} + +.title, #blocked-hosts { + margin: 1em; +} + +.title { + font-size: 1.2em; +} diff --git a/proxy-blocker/options/options.html b/proxy-blocker/options/options.html new file mode 100644 index 0000000..7cbcf60 --- /dev/null +++ b/proxy-blocker/options/options.html @@ -0,0 +1,19 @@ + + + +
+ + + + + + +