diff options
Diffstat (limited to 'js/background.js')
-rw-r--r-- | js/background.js | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/js/background.js b/js/background.js index e66be8f..5b393db 100644 --- a/js/background.js +++ b/js/background.js @@ -6,25 +6,13 @@ 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 - }; -} +log.enabled = true; + + +var isFirefox = !!navigator.userAgent.match(/Firefox/i); + +log('Is Firefox: ' + isFirefox); + //Hopefully Firefox will fix this at some point and we can just use onBeforeRequest everywhere... var redirectEvent = isFirefox ? chrome.webRequest.onBeforeSendHeaders : chrome.webRequest.onBeforeRequest; @@ -121,6 +109,8 @@ function checkRedirects(details) { ignoreNextRequest[result.redirectTo] = new Date().getTime(); return { redirectUrl: result.redirectTo }; + } else { + log(details.url + ' is not a match for ' + r.includePattern + ', type ' + r.patternType); } } @@ -163,10 +153,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 }; } @@ -215,6 +203,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(); |