Update appcache version.
[skeletonkey.git] / core.js
diff --git a/core.js b/core.js
index 9c5499bfec63105e1c43eba290415e9285efd4a5..3986dae01f959e41201b52d72c39b5e007b85926 100644 (file)
--- a/core.js
+++ b/core.js
@@ -69,8 +69,13 @@ SkeletonKey.prototype._init = function() {
   this._sitekey.onkeyup = this._nextFieldInterceptor.bind(this);
   this._username.onkeyup = this._nextFieldInterceptor.bind(this);
 
-  this._password.onclick = this._selectPassword.bind(this);
-  this._password.labels[0].onclick = this._selectPassword.bind(this);
+  if (!this._isTouchDevice()) {
+    this._password.onmousedown = this._selectPassword.bind(this);
+    this._password.onmouseup = function(e) {
+      e.preventDefault();
+      e.stopPropagation();
+    };
+  }
 
   if (this._isChromeExtension()) {
     this._initChromeExtension();
@@ -100,7 +105,7 @@ SkeletonKey.prototype._onGenerate = function(e) {
   if (hexString.length > maxLength)
     hexString = hexString.substr(0, maxLength);
 
-  this._password.value = hexString;
+  this._password.innerText = hexString;
   this._selectPassword();
 };
 
@@ -174,8 +179,19 @@ SkeletonKey.prototype._nextFieldInterceptor = function(e) {
  * @private
  */
 SkeletonKey.prototype._selectPassword = function() {
-  this._password.focus();
-  this._password.select();
+  this._generateButton.blur();
+
+  // Touch devices do not bring up the edit controls (for copy) for
+  // pre-selected text.
+  if (this._isTouchDevice())
+    return;
+
+  var range = document.createRange();
+  range.selectNode(this._password.firstChild);  // Select #text node.
+
+  var selection = window.getSelection();
+  selection.removeAllRanges();
+  selection.addRange(range);
 };
 
 /**
@@ -196,8 +212,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));
 };
 
@@ -207,5 +243,14 @@ SkeletonKey.prototype._initChromeExtension = function() {
  * @private
  */
 SkeletonKey.prototype._isChromeExtension = function() {
-  return typeof chrome !== undefined && typeof chrome.extension !== undefined;
+  return typeof chrome !== 'undefined' && typeof chrome.tabs !== 'undefined';
 };
+
+/**
+ * Checks if SkeletonKey is running on a touch device.
+ * @returns {bool}
+ * @private
+ */
+SkeletonKey.prototype._isTouchDevice = function() {
+  return typeof document.createTouch === 'function';
+}