A better way to generate the sitekey
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 31 Dec 2012 04:32:43 +0000 (23:32 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 31 Dec 2012 04:32:43 +0000 (23:32 -0500)
core.js

diff --git a/core.js b/core.js
index 9c5499bfec63105e1c43eba290415e9285efd4a5..b63c5decf25db26793b0925d7c476a7df3c4ff8f 100644 (file)
--- 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));
 };