Home | History | Annotate | Download | only in gaia_auth
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * Alias for document.getElementById.
      7  * @param {string} id The ID of the element to find.
      8  * @return {HTMLElement} The found element or null if not found.
      9  */
     10 function $(id) {
     11   return document.getElementById(id);
     12 }
     13 
     14 /**
     15  * Extract query params from given search string of an URL.
     16  * @param {string} search The search portion of an URL to extract params.
     17  * @return {Object} The key value pairs of the extracted params.
     18  */
     19 function getUrlSearchParams(search) {
     20   var params = {};
     21 
     22   if (search) {
     23     // Strips leading '?'
     24     search = search.substring(1);
     25     var pairs = search.split('&');
     26     for (var i = 0; i < pairs.length; ++i) {
     27       var pair = pairs[i].split('=');
     28       if (pair.length == 2) {
     29         params[pair[0]] = decodeURIComponent(pair[1]);
     30       } else {
     31         params[pair] = true;
     32       }
     33     }
     34   }
     35 
     36   return params;
     37 }
     38 
     39 /**
     40  * Creates a new URL which is the old URL with a GET param of key=value.
     41  * Copied from ui/webui/resources/js/util.js.
     42  * @param {string} url The base URL. There is not sanity checking on the URL so
     43  *     it must be passed in a proper format.
     44  * @param {string} key The key of the param.
     45  * @param {string} value The value of the param.
     46  * @return {string} The new URL.
     47  */
     48 function appendParam(url, key, value) {
     49   var param = encodeURIComponent(key) + '=' + encodeURIComponent(value);
     50 
     51   if (url.indexOf('?') == -1)
     52     return url + '?' + param;
     53   return url + '&' + param;
     54 }
     55 
     56 /**
     57  * Creates a new URL by striping all query parameters.
     58  * @param {string} url The original URL.
     59  * @return {string} The new URL with all query parameters stripped.
     60  */
     61 function stripParams(url) {
     62   return url.substring(0, url.indexOf('?')) || url;
     63 }
     64 
     65 /**
     66  * Extract domain name from an URL.
     67  * @param {string} url An URL string.
     68  * @return {string} The host name of the URL.
     69  */
     70 function extractDomain(url) {
     71   var a = document.createElement('a');
     72   a.href = url;
     73   return a.hostname;
     74 }
     75