forked from mdn/webextensions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
6 changed files
with
77 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
# 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. | ||
This extension uses the proxy API listener `onRequest` to listen for requests to visit a web page, compare the webpage's domain with a blocked host list, and proxy domains on the blocked list to 127.0.0.1. | ||
|
||
The PAC script is initialized with a list of hostnames: it blocks requests to any hosts that are in the list. | ||
The list of blocked domains is held in local storage and given the initial value `["example.com", "example.org"]` when the extension installs. The list can be modified through the extension"s options page. | ||
|
||
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 simple: hostnames must match an entry in the list if they are to be blocked. So with the default settings, "example.org" is blocked but "www.example.org" is permitted. | ||
|
||
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: | ||
To try out this extension: | ||
* 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. | ||
* visit `http://example.com` and see it is blocked | ||
* visit `about:addons`, open the add-on's preferences, and change the hostnames in the text box | ||
* visit some 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. | ||
* How to implement `browser.proxy.onRequest` and proxy requests. | ||
* How to store and retrieve lists from local storage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} | ||
|
||
// Register the proxy script | ||
browser.proxy.register(proxyScriptURL); | ||
// Set the default list on installation. | ||
browser.runtime.onInstalled.addListener(details => { | ||
browser.storage.local.set({ | ||
blockedHosts: blockedHosts | ||
}); | ||
}); | ||
|
||
// Log any errors from the proxy script | ||
browser.proxy.onProxyError.addListener(error => { | ||
console.error(`Proxy error: ${error.message}`); | ||
// Get the stored list | ||
browser.storage.local.get(data => { | ||
if (data.blockedHosts) { | ||
blockedHosts = data.blockedHosts; | ||
} | ||
}); | ||
|
||
// 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}); | ||
}); | ||
// Listen for changes in the blocked list | ||
browser.storage.onChanged.addListener(changeData => { | ||
blockedHosts = changeData.blockedHosts.newValue; | ||
}); | ||
|
||
// 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"); | ||
}); | ||
} | ||
// Managed the proxy | ||
|
||
function handleMessage(message, sender) { | ||
// only handle messages from the proxy script | ||
if (sender.url != browser.extension.getURL(proxyScriptURL)) { | ||
return; | ||
} | ||
// Listen for a request to open a webpage | ||
browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]}); | ||
|
||
if (message === "init") { | ||
handleInit(message); | ||
} else { | ||
// after the init message the only other messages are status messages | ||
console.log(message); | ||
// 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"}; | ||
} | ||
|
||
browser.runtime.onMessage.addListener(handleMessage); | ||
// Log any errors from the proxy script | ||
browser.proxy.onError.addListener(error => { | ||
console.error(`Proxy error: ${error.message}`); | ||
}); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
|
||
"description": "Theme with a single image placed centrally and then tiled. Also, illustrates the use of frame_inactive to change the header background color when the browser window isn't in focus. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#themes", | ||
"manifest_version": 2, | ||
"name": "temp", | ||
"version": "1.1", | ||
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/themes/weta_tiled", | ||
|
||
"theme": { | ||
"colors": { | ||
"frame": "blue", | ||
"frame_inactive": "#c6c6c6", | ||
"toolbar_text": "black", | ||
"tab_background_text":"black", | ||
"tab_text":"black", | ||
"sidebar_border":"black", | ||
"toolbar_field_border":"black", | ||
"tab_background_separator":"black", | ||
"toolbar_top_separator":"black", | ||
"toolbar_vertical_separator":"black", | ||
"toolbar_bottom_separator":"black", | ||
"toolbar_field_separator":"black" | ||
} | ||
} | ||
} |