-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_calendar_age_adder.js
49 lines (40 loc) · 1.83 KB
/
google_calendar_age_adder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ==UserScript==
// @name google_calendar_age_adder
// @namespace http://darrinholst.com
// @description Adds the age to gcal events ending with " - 9999" where 9999 is the start year of the event.
// @description Great for birthday and anniversary events.This just modifies the html,it doesn't touch the actual event.
// @description New settings allows to display only the age without the year. Just add [bd:YYYY] at the end
// @include http://www.google.com/calendar/*
// @include https://www.google.com/calendar/*
// ==/UserScript==
(function() {
var DATE_CONTAINER = 'dateunderlay',
EVENT_CONTAINER = 'mainbody',
EVENT_TAG = 'div';
var addEm = function() {
var year = /^.*(\d{4})$/.exec(document.getElementById(DATE_CONTAINER).innerHTML)[1];
var events = document.getElementById(EVENT_CONTAINER).getElementsByTagName(EVENT_TAG);
for (var i = 0, j = events.length; i < j; i++) {
var childNodes = events[i].childNodes
for(var m = 0, n = childNodes.length; m < n; m++){
var childNode = childNodes[m];
if(childNode.nodeType == 3) {
var nodeValue = childNode.nodeValue;
var oldMatch = /^.* - ((?:19|20)\d\d)$$/.exec(nodeValue);
var newMatch = /(^.* )(\[bd:(?:19|20)\d\d\])$$/.exec(nodeValue);
if (oldMatch) {
// shows: xxxxx - YYYY (age)
childNode.nodeValue = nodeValue + ' (' + (year - oldMatch[1]) + ')';
}
if (newMatch) {
// shows: xxxxx (age)
var birthyear = newMatch[2].substring(4, 8);
childNode.nodeValue = newMatch[1] + ' (' + (year - birthyear) + ')';
}
}
}
}
}
document.getElementById(EVENT_CONTAINER).addEventListener('DOMSubtreeModified', addEm, true);
addEm();
})();