aboutsummaryrefslogtreecommitdiffstats
path: root/lib/utils.js
blob: a0ca97155b75e761085736a2218c6df848450150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var utils = {
  getCurrentVersion: function() {
    // Chromium #15242 will make this XHR request to access the manifest unnecessary.
    var manifestRequest = new XMLHttpRequest();
    manifestRequest.open("GET", chrome.extension.getURL("manifest.json"), false);
    manifestRequest.send(null);
    return JSON.parse(manifestRequest.responseText).version;
  },

  /*
   * Takes a dot-notation object string and call the function
   * that it points to with the correct value for 'this'.
   */
  invokeCommandString: function(str, argArray) {
    var components = str.split('.');
    var obj = window;
    for (var i = 0; i < components.length - 1; i++)
      obj = obj[components[i]];
    var func = obj[components.pop()];
    return func.apply(obj, argArray);
  },

  /**
   * Generates a unique ID
   */
  createUniqueId: (function() {
    id = 0;
    return function() { return ++id; };
  })(),

  /**
   * Completes a partial URL (without scheme)
   */
  createFullUrl: function(partialUrl) {
    if (!/^[a-z]{3,}:\/\//.test(partialUrl))
      partialUrl = 'http://' + partialUrl;
    return partialUrl
  },

  /**
   * Tries to detect, whether :str is a valid URL.
   */
  isUrl: function(str) {
    // more or less RFC compliant URL host part parsing. This should be sufficient
    // for our needs
    var urlRegex = new RegExp(
      '^(?:([^:]+)(?::([^:]+))?@)?' +   // user:password (optional)     => \1, \2
      '([^:]+|\\[[^\\]]+\\])'       +   // host name (IPv6 addresses in square brackets allowed) => \3
      '(?::(\\d+))?$'                   // port number (optional)       => \4
      );

    // these are all official ASCII TLDs that are longer than 3 characters
    // (including the inofficial .onion TLD used by TOR)
    var longTlds = [ 'arpa', 'asia', 'coop', 'info', 'jobs', 'local', 'mobi', 'museum', 'name', 'onion' ];

    // are there more?
    var specialHostNames = [ 'localhost' ];

    // it starts with a scheme, so it's definitely an URL
    if (/^[a-z]{3,}:\/\//.test(str))
      return true;

    // spaces => definitely not a valid URL
    if (str.indexOf(' ') >= 0)
      return false;

    // assuming that this is an URL, try to parse it into its meaningful parts. If matching fails, we're
    // pretty sure that we don't have some kind of URL here.
    var match = urlRegex.exec(str.split('/')[0]);
    if (!match)
      return false;
    var hostname = match[3];

    // allow known special host names
    if (specialHostNames.indexOf(hostname) >= 0)
      return true;

    // allow IPv6 addresses (need to be wrapped in brackets, as required by RFC).  It is sufficient to check
    // for a colon here, as the regex wouldn't match colons in the host name unless it's an v6 address
    if (hostname.indexOf(':') >= 0)
      return true;

    // at this point we have to make a decision. As a heuristic, we check if the input has dots in it. If
    // yes, and if the last part could be a TLD, treat it as an URL.
    var dottedParts = hostname.split('.');
    var lastPart = dottedParts[dottedParts.length-1];
    if (dottedParts.length > 1 && ((lastPart.length >= 2 && lastPart.length <= 3)
                                   || longTlds.indexOf(lastPart) >= 0))
      return true;

    // also allow IPv4 addresses
    if (/^(\d{1,3}\.){3}\d{1,3}$/.test(hostname))
      return true;

    // fallback: no URL
    return false
  },

  /**
   * Creates a search URL from the given :query.
   */
  createSearchUrl: function(query) {
    // we need to escape explictely to encode characters like "+" correctly
    return "http://www.google.com/search?q=" + encodeURIComponent(query);
  },

  /**
   * 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, '');
    if (utils.isUrl(str))
      return utils.createFullUrl(str);
    else
      return utils.createSearchUrl(str);
  },

};