aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
diff options
context:
space:
mode:
authorJez Ng2012-01-15 17:39:58 +0800
committerJez Ng2012-01-15 18:01:37 +0800
commitfbdff8626a8db7be33a6b7790a35e48c4c5231aa (patch)
tree9aa312a6869b455cbc54789f07a2c305a128499c /lib/utils.js
parent3ff0518014a51f237d1d98ebc15c0ce4be24c2b5 (diff)
downloadvimium-fbdff8626a8db7be33a6b7790a35e48c4c5231aa.tar.bz2
Handle pasted URLs intelligently.
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js
index 0c86970e..a0668409 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -74,4 +74,24 @@ var utils = {
};
return null;
},
+
+ /**
+ * Tries to convert :str into a valid URL.
+ * We don't bother with escaping characters, however, as Chrome will do that for us.
+ */
+ ensureUrl: function(str) {
+ // trim str
+ str = str.replace(/^\s+|\s+$/g, '');
+
+ // definitely not a valid URL; treat as a search query
+ if (str.indexOf(" ") != -1 || (str.indexOf('.') == -1 && !/^((http|https|ftp):\/\/)?localhost/.test(str)))
+ return "http://www.google.com/search?q=" + str;
+ // possibly a valid URL, but not canonical
+ else if (!/^(http|https|ftp|chrome):\/\//.test(str))
+ return "http://" + str;
+ // cross our fingers and hope it is valid
+ else
+ return str;
+ },
+
};