aboutsummaryrefslogtreecommitdiff
path: root/js/background.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/background.js')
-rw-r--r--js/background.js70
1 files changed, 37 insertions, 33 deletions
diff --git a/js/background.js b/js/background.js
index e66be8f..c025281 100644
--- a/js/background.js
+++ b/js/background.js
@@ -6,27 +6,7 @@ function log(msg) {
console.log('REDIRECTOR: ' + msg);
}
}
-log.enabled = false;
-
-
-var isFirefox = false;
-
-if (typeof chrome == 'undefined') {
- console.log('Creating fake chrome...');
- isFirefox = true;
- var firefoxShim = require('./firefox/background-shim');
- chrome = firefoxShim.chrome;
- Redirect = firefoxShim.Redirect;
- log = firefoxShim.log;
- exports.onUnload = function (reason) {
- log('Unloading (' + reason + '), removing listeners');
- redirectEvent.removeListener(checkRedirects);
- chrome.storage.onChanged.removeListener(monitorChanges);
- chrome.storage.clearCache(); //<-Firefox specific
- };
-}
-//Hopefully Firefox will fix this at some point and we can just use onBeforeRequest everywhere...
-var redirectEvent = isFirefox ? chrome.webRequest.onBeforeSendHeaders : chrome.webRequest.onBeforeRequest;
+log.enabled = true;
//Redirects partitioned by request type, so we have to run through
//the minimum number of redirects for each request.
@@ -66,12 +46,6 @@ function setIcon(image) {
//decide whether or not we want to redirect.
function checkRedirects(details) {
- //Oh Firefox, please fix your broken url matching soon...
- if (isFirefox && !details.url.match(/^https?:\/\//)) {
- log('Not http: ' + details.url);
- return {};
- }
-
//We only allow GET request to be redirected, don't want to accidentally redirect
//sensitive POST parameters
if (details.method != 'GET') {
@@ -136,7 +110,7 @@ function monitorChanges(changes, namespace) {
if (changes.disabled.newValue == true) {
log('Disabling Redirector, removing listener');
- redirectEvent.removeListener(checkRedirects);
+ chrome.webRequest.onBeforeRequest.removeListener(checkRedirects);
} else {
log('Enabling Redirector, setting up listener');
setUpRedirectListener();
@@ -163,10 +137,8 @@ function createFilter(redirects) {
}
types.sort();
- //FIXME: The Firefox implementation of the url matching is seriously broken still,
- //so we can't filter by url on Firefox for now, have to cut non http urls out in checkRedirects.
return {
- urls: isFirefox ? null : ["https://*/*", "http://*/*"],
+ urls: ["https://*/*", "http://*/*"],
types : types
};
}
@@ -192,7 +164,7 @@ function createPartitionedRedirects(redirects) {
//Sets up the listener, partitions the redirects, creates the appropriate filters etc.
function setUpRedirectListener() {
- redirectEvent.removeListener(checkRedirects); //Unsubscribe first, in case there are changes...
+ chrome.webRequest.onBeforeRequest.removeListener(checkRedirects); //Unsubscribe first, in case there are changes...
chrome.storage.local.get({redirects:[]}, function(obj) {
var redirects = obj.redirects;
@@ -205,7 +177,7 @@ function setUpRedirectListener() {
var filter = createFilter(redirects);
log('Setting filter for listener: ' + JSON.stringify(filter));
- redirectEvent.addListener(checkRedirects, filter, ["blocking"]);
+ chrome.webRequest.onBeforeRequest.addListener(checkRedirects, filter, ["blocking"]);
});
}
@@ -215,6 +187,38 @@ function updateIcon() {
});
}
+
+//Firefox doesn't allow the "content script" which is actually privileged
+//to access the objects it gets from chrome.storage directly, so we
+//proxy it through here.
+chrome.runtime.onMessage.addListener(
+ function(request, sender, sendResponse) {
+ log('Received background message: ' + JSON.stringify(request));
+ if (request.type == 'getredirects') {
+ log('Getting redirects from storage');
+ chrome.storage.local.get({redirects:[]}, function(obj) {
+ log('Got redirects from storage: ' + JSON.stringify(obj));
+ sendResponse(obj);
+ log('Sent redirects to content page');
+ });
+ } else if (request.type == 'saveredirects') {
+ console.log('Saving redirects, count=' + request.redirects.length);
+ delete request.type;
+ chrome.storage.local.set(request, function(a) {
+ log('Finished saving redirects to storage');
+ sendResponse({message:"Redirects saved"});
+ });
+ } else {
+ log('Unexpected message: ' + JSON.stringify(request));
+ return false;
+ }
+
+ return true; //This tells the browser to keep sendResponse alive because
+ //we're sending the response asynchronously.
+ }
+);
+
+
//First time setup
updateIcon();