Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions background.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -12,52 +12,49 @@ 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) {
// Check if we have a matching redirect
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: ["<all_urls>"] },
["blocking"]
);

// Track changes to data object.
chrome.storage.onChanged.addListener(function(changes, namespace) {
Expand All @@ -69,4 +66,4 @@ if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.indexOf(str) === 0;
};
}
}
10 changes: 5 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -15,7 +15,7 @@
"manifest_version": 2,
"name": "URL Alias",
"options_page": "options.html",
"permissions": [ "tabs", "storage" ],
"permissions": [ "tabs", "storage", "webRequest", "webRequestBlocking", "<all_urls>" ],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "0.0.1"
}
"version": "1.0.2"
}