Last active
July 6, 2017 07:58
-
-
Save nkmathew/b8d5da192f8862886017 to your computer and use it in GitHub Desktop.
Greasemonkey script that changes gmail's timestamps to relative time in basic html view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// ==UserScript== | |
// @name GMail Age-Counter | |
// @namespace http://nkmathew.net | |
// @description Describes time elapsed since an email message was received | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js | |
// @include https://mail.google.com/mail/u/0/h/* | |
// @version 0.1 | |
// @grant none | |
// ==/UserScript== | |
/** | |
* Works in Basic GMail only | |
*/ | |
(function () { | |
window.addEventListener('load', function () { | |
function pluralize(number, text) { | |
if (number == 1) { | |
return number + ' ' + text; | |
} else { | |
return number + ' ' + text + 's'; | |
} | |
} | |
function describeDateTime(date_time, fmt) { | |
var val = moment().diff(moment(date_time, fmt)); | |
var msg = ''; | |
val = moment.duration(val)['_data']; | |
if (val.years && !val.months) { | |
msg = pluralize(val.years, 'year'); | |
} else if (val.years && val.months) { | |
msg = pluralize(val.years, 'year') + ', ' + pluralize(val.months, 'month'); | |
} else if (val.months && !val.days) { | |
msg = pluralize(val.months, 'month'); | |
} else if (val.months && val.days) { | |
msg = pluralize(val.months, 'month') + ', ' + pluralize(val.days, 'day'); | |
} else if (val.days && !val.hours) { | |
msg = pluralize(val.days, 'day'); | |
} else if (val.days && val.hours) { | |
msg = pluralize(val.days, 'day') + ', ' + pluralize(val.hours, 'hour'); | |
} else if (val.hours && !val.minutes) { | |
msg = pluralize(val.hours, 'hour'); | |
} else if (val.hours && val.minutes) { | |
msg = pluralize(val.hours, 'hour') + ', ' + pluralize(val.minutes, 'minute'); | |
} else if (val.minutes) { | |
msg = pluralize(val.minutes, 'minute') | |
} else { | |
if (!val.seconds) { | |
msg = 'a second'; | |
} else { | |
msg = val.seconds + ' seconds'; | |
} | |
} | |
return 'Sent ' + msg + ' ago'; | |
} | |
const selector = 'td > table > tbody > tr > td > table > tbody > tr > td > table > tbody > tr > td[align="right"]'; | |
const fmt = 'ddd MMM D yyyy HH:mm A'; | |
$(selector).each(function() { | |
var currDate = this.textContent; | |
const currHTML = this.innerHTML.replace(currDate, ''); | |
currDate = currDate.replace(' at', ''); | |
var datetime = moment(currDate, fmt).format(); | |
const newHTML = '<time is="relative-time" class="agecounter" datetime="' + datetime + '" title="' + currDate + '"> ' + describeDateTime(currDate, fmt) + '</time>'; | |
$(this).html(currHTML + newHTML); | |
$('.agecounter').css({ | |
'font-family': 'Helvetica,Arial' | |
}); | |
}); | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment