Skip to content

Instantly share code, notes, and snippets.

@deinspanjer
Created February 12, 2013 19:04
Show Gist options
  • Save deinspanjer/4772379 to your computer and use it in GitHub Desktop.
Save deinspanjer/4772379 to your computer and use it in GitHub Desktop.
Little JS hack to pretty print FHR payloads. The biggest feature is converting date codes into yyyy-MM-dd along with age.
/* Usage:
1. open about:healthreport
2. open a Web Console panel in that page
3. Pop the console out and expand it
4. Copy this JS code to the clipboard
5. Paste it into the command prompt at the bottom of the console
*/
/* Credit: http://stackoverflow.com/a/7838061 */
function getAge(fromdate, todate){
if(todate) todate= new Date(todate);
else todate= new Date();
var age= [], fromdate= new Date(fromdate),
y= [todate.getFullYear(), fromdate.getFullYear()],
ydiff= y[0]-y[1],
m= [todate.getMonth(), fromdate.getMonth()],
mdiff= m[0]-m[1],
d= [todate.getDate(), fromdate.getDate()],
ddiff= d[0]-d[1];
if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff;
if(mdiff<0) mdiff+= 11;
if(ddiff<0){
fromdate.setMonth(m[1]+1, 0);
ddiff= fromdate.getDate()-d[1]+d[0];
--mdiff;
}
if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' '));
if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':''));
if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':''));
if(age.length>1) age.splice(age.length-1,0,' and ');
return age.join('');
}
function getDateAndAgeString(dteCode) {
let dte = new Date(dteCode * 24 * 60 * 60 * 1000);
let dteString = JSON.stringify(dte).slice(3,11);
dteString += '('+getAge(dte)+')';
return dteString;
}
function dateReplacer(key,value) {
if (
key == 'profileCreation' ||
key == 'startDay' ||
key == 'installDay' ||
key == 'updateDay')
{
return getDateAndAgeString(value);
}
else return value;
}
function prettyPrintFHRReport(data) {
return JSON.stringify(data,dateReplacer,2);
}
let reporter = Components.classes["@mozilla.org/datareporting/service;1"].getService(Components.interfaces.nsISupports).wrappedJSObject.healthReporter;
reporter.collectAndObtainJSONPayload(true).then(
function onJSON(data) {
console.log(prettyPrintFHRReport(data));
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment