OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style license that can be |
| 4 found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 /** |
| 8 * @fileoverview Common methods for performance-plotting Javascript. |
| 9 */ |
| 10 |
| 11 /** |
| 12 * Fetches a URL asynchronously and invokes a callback when complete. |
| 13 * |
| 14 * @param {string} url URL to fetch. |
| 15 * @param {Function(string, ?string)} callback The function to invoke when the |
| 16 * results of the URL fetch are complete. The function should accept two |
| 17 * strings representing the URL data, and any errors, respectively. |
| 18 */ |
| 19 function Fetch(url, callback) { |
| 20 var r = new XMLHttpRequest(); |
| 21 r.open('GET', url, true); |
| 22 r.setRequestHeader('pragma', 'no-cache'); |
| 23 r.setRequestHeader('cache-control', 'no-cache'); |
| 24 |
| 25 r.onreadystatechange = function() { |
| 26 if (r.readyState == 4) { |
| 27 var text = r.responseText; |
| 28 var error = null; |
| 29 if (r.status != 200) |
| 30 error = url + ': ' + r.status + ': ' + r.statusText; |
| 31 else if (!text) |
| 32 error = url + ': null response'; |
| 33 callback(text, error); |
| 34 } |
| 35 } |
| 36 |
| 37 r.send(null); |
| 38 } |
| 39 |
| 40 /** |
| 41 * Parses the parameters of the current page's URL. |
| 42 * |
| 43 * @return {Object.<string, (string|number)>} An object with properties given |
| 44 * by the parameters specified in the URL's query string. |
| 45 */ |
| 46 function ParseParams() { |
| 47 var result = {}; |
| 48 |
| 49 var query = window.location.search.substring(1); |
| 50 if (query.slice(-1) == '/') { |
| 51 query = query.slice(0, -1); // Strip trailing slash. |
| 52 } |
| 53 var s = query.split('&'); |
| 54 |
| 55 for (i = 0; i < s.length; ++i) { |
| 56 var v = s[i].split('='); |
| 57 var key = v[0]; |
| 58 var value = unescape(v[1]); |
| 59 result[key] = value; |
| 60 } |
| 61 |
| 62 if ('history' in result) { |
| 63 result['history'] = parseInt(result['history']); |
| 64 result['history'] = Math.max(result['history'], 2); |
| 65 } |
| 66 if ('rev' in result) { |
| 67 result['rev'] = parseInt(result['rev']); |
| 68 } |
| 69 |
| 70 return result; |
| 71 } |
| 72 |
| 73 /** |
| 74 * Creates the URL constructed from the current pathname and the given params. |
| 75 * |
| 76 * @param {Object.<string, (string|number)>} params An object containing |
| 77 * parameters for a URL query string. |
| 78 * @return {string} The URL constructed from the given params. |
| 79 */ |
| 80 function MakeURL(params) { |
| 81 var key_values = []; |
| 82 for (key in params) { |
| 83 // better to sanitize key here. |
| 84 key_values.push(key + '=' + encodeURIComponent(params[key])); |
| 85 } |
| 86 return window.location.pathname + '?' + key_values.join('&'); |
| 87 } |
OLD | NEW |