From 2fe029089cb23ddb122719ad81d6faec1a11bef6 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 30 Dec 2012 23:32:43 -0500 Subject: [PATCH] A better way to generate the sitekey --- core.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/core.js b/core.js index 9c5499b..b63c5de 100644 --- a/core.js +++ b/core.js @@ -196,8 +196,28 @@ SkeletonKey.prototype._initChromeExtension = function() { if (url == null || url == "") return; - var matches = url.match(/https?:\/\/(www|login|accounts?|.*\.)\.?(.*)\.(com?|net|org|edu|biz|info)?.*/); - this._sitekey.value = matches[2]; + // Use a link to clevely parse the URL into the hostname. + var parser = document.createElement("a"); + parser.href = url; + var hostname = parser.hostname.split("."); + + // Filter out common subdomains and TLDs to keep the siteky short and + // memorable. + ["www", "login", "account", "accounts"].forEach(function(subdomain) { + if (hostname[0] == subdomain) { + hostname.shift(); + return; + } + }); + + ["com", "net", "org", "edu", "info"].forEach(function(tld) { + if (hostname[hostname.length - 1] == tld) { + hostname.pop(); + return; + } + }); + + this._sitekey.value = hostname.join("."); }.bind(this)); }; -- 2.22.5