diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/utils.js | 20 |
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; + }, + }; |
