]>
git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/util.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Some basic, useful utility functions.
8 * Pours the data in template string.
9 * @param {Object} datObject The data object to be filled in template string.
10 * @return {string} The new string created from template string and filled
11 * with the given data.
13 String
.prototype.supplant = function(datObject
) {
14 return this.replace(/{([^{}]*)}/g,
15 function(match
, firstSubMatch
) {
16 var replace
= datObject
[firstSubMatch
];
17 return (typeof replace
=== 'string' || typeof replace
=== 'number') ?
24 * @param {string} selector Selector to execute.
25 * @param {HTMLElement=} context HTMLElement to query (optional).
26 * @return {Array.<HTMLElement>} Array of matched elements (non-live).
28 function $(selector
, context
) {
29 if (!(context
&& context
instanceof HTMLElement
)) {
32 return Array
.prototype.slice
.call(context
.querySelectorAll(selector
));
36 * Provides units for time, given in hours, as hours or days.
37 * @param {number} time The time, in hours.
38 * @return {Object} The time and units of time (pluralized and localized).
40 function getUnitsForTime(time
) {
46 units
= chrome
.i18n
.getMessage(units
+ (time
=== 1 ? '' : 's') + 'String');