diff --git a/background.js b/background.js index 94ff388..6452673 100644 --- a/background.js +++ b/background.js @@ -1,9 +1,9 @@ -// Load stored preferenced -store = new Object(); +// Load stored preferences +var store = {}; function updateStore() { chrome.storage.sync.get('urlalias', function (obj) { - store = new Object(); + store = {}; // First time, initialize. if (obj != null) { @@ -12,18 +12,21 @@ function updateStore() { // Add default keys if empty. if (store == null || Object.keys(store).length == 0) { - store = new Object(); + store = {}; store["m"] = "https://mail.google.com"; store["c"] = "https://calendar.google.com"; store["d"] = "https://drive.google.com"; chrome.storage.sync.set({ 'urlalias': store}); } }); -}; +} + updateStore(); // Checks if 'server' is to be redirected, and executes the redirect. -function doRedirectIfSaved(tabId, server, others) { +function doRedirectIfSaved(details) { + var url = new URL(details.url); + var server = url.hostname; var redirect = store[server]; if (redirect == null) { @@ -31,33 +34,27 @@ function doRedirectIfSaved(tabId, server, others) { for (var key in store) { if (key.startsWith(server)) { // Found the server - redirect = store[key].replace("###", others.join('/')); + redirect = store[key].replace("###", url.pathname.slice(1)); break; } } } - if (redirect.indexOf('://') < 0) { - // Add a default protocol - redirect = "http://" + redirect; - } - chrome.tabs.update(tabId, { url: redirect }); -} - -// Called when the user changes the url of a tab. -function onTabUpdate(tabId, changeInfo, tab) { - var url = tab.url; - - var url_protocol_stripped = /^http[s]?:\/\/(.*)/g.exec(url); - - if (url_protocol_stripped != null && url_protocol_stripped.length >= 2) { - var match = url_protocol_stripped[[1]].split("/"); - doRedirectIfSaved(tabId, match[0], match.splice(1)); + if (redirect) { + if (redirect.indexOf('://') < 0) { + // Add a default protocol + redirect = "http://" + redirect; + } + return { redirectUrl: redirect }; } } -// Listen for any changes to the URL of any tab. -chrome.tabs.onUpdated.addListener(onTabUpdate); +// Intercept requests before they're sent +chrome.webRequest.onBeforeRequest.addListener( + doRedirectIfSaved, + { urls: [""] }, + ["blocking"] +); // Track changes to data object. chrome.storage.onChanged.addListener(function(changes, namespace) { @@ -69,4 +66,4 @@ if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (str){ return this.indexOf(str) === 0; }; -} +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index e8765e2..2e32af4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "background": { - "persistent": false, + "persistent": true, "scripts": [ "background.js" ] }, - "author": "Karan Goel", + "author": "Karan Goel [Base] , m4xy07 [Updates]", "content_security_policy": "script-src 'self' https://code.jquery.com https://storage.googleapis.com https://fonts.googleapis.com; object-src 'self'", "description": "Set URL aliases ('m/' goes to 'mail.google.com')", "homepage_url": "https://github.com/karan/chrome-url-alias", @@ -15,7 +15,7 @@ "manifest_version": 2, "name": "URL Alias", "options_page": "options.html", - "permissions": [ "tabs", "storage" ], + "permissions": [ "tabs", "storage", "webRequest", "webRequestBlocking", "" ], "update_url": "http://clients2.google.com/service/update2/crx", - "version": "0.0.1" -} + "version": "1.0.2" +} \ No newline at end of file