Nicer date/time display
[ff_comments.user.js] / ff_comments.user.js
1 // ==UserScript==
2 // @name        FriendFeed comments
3 // @description Add FriendFeed 'likes' and comments to any page
4 // @namespace   http://oblomov.myopenid.com
5 // @include     http://*
6 // @exclude     http://friendfeed.com/*
7 // @exclude     http://*.friendfeed.com/*
8 // @exclude     http://friendfeed-api.com/*
9 // @author      Giuseppe "Oblomov" Bilotta
10 // @version     20091219
11 // ==/UserScript==
12
13 if (typeof(unsafeWindow) == 'undefined') unsafeWindow = window;
14
15 (function() {
16
17  var ff_preload = new Image();
18  ff_preload.src = 'http://friendfeed.com/favicon.ico';
19  ff_preload.src = 'http://friendfeed.com/static/images/logo-small.png';
20
21
22  unsafeWindow.ujs_now = new Date();
23
24 unsafeWindow.ujs_ff_load = function(url) {
25         var e = unsafeWindow.document.createElement('script');
26         e.type = 'text/javascript';
27         e.src = url;
28         unsafeWindow.document.getElementsByTagName('head')[0].appendChild(e);
29 };
30
31 unsafeWindow.ujs_ff_load_entries = function(str) {
32         var weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
33         var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
34
35         if (str.entries && str.entries.length > 0) {
36                 var doc = unsafeWindow.document;
37
38                 var pdiv = doc.getElementById('ujs_friendfeed');
39                 var div;
40                 if (pdiv) {
41                         div = pdiv.lastChild;
42                 } else {
43                         pdiv = doc.createElement('div');
44                         pdiv.setAttribute('id', 'ujs_friendfeed');
45                         pdiv.setAttribute('title', 'FriendFeed comments on this page');
46                         pdiv.style.position = 'absolute';
47                         pdiv.style.top = 0;
48                         pdiv.style.left = 0;
49                         pdiv.style.zIndex = 100000;
50                         pdiv.style.maxWidth = '30%';
51                         pdiv.style.height='auto';
52                         pdiv.style.border='3px outset lightblue';
53                         pdiv.style.margin = 0;
54                         pdiv.style.padding = 0;
55                         pdiv.style.textAlign = 'center';
56                         pdiv.style.fontSize = '0px';
57                         pdiv.style.lineHeight = 1;
58                         //  pdiv.style.opacity = 0.7;
59                         pdiv.style.backgroundColor = 'white';
60
61                         doc.body.appendChild(pdiv);
62
63                         var logo = doc.createElement('img');
64                         logo.src = 'http://friendfeed.com/favicon.ico';
65                         logo.setAttribute('alt', 'FriendFeed logo');
66                         logo.setAttribute('title', 'FriendFeed logo');
67                         logo.style.margin='0 auto 0 auto';
68                         logo.style.padding=0;
69                         pdiv.appendChild(logo);
70
71                         div = doc.createElement('div');
72                         div.style.fontSize = 'small';
73                         div.style.display = 'none';
74                         pdiv.appendChild(div);
75
76                         function displayToggle() {
77                                 // don't toggle if we were dragging
78                                 if (pdiv.dragged) {
79                                         pdiv.dragged = false;
80                                         return;
81                                 }
82                                 var d = pdiv.lastChild;
83                                 if (d.style.display == 'none') {
84                                         d.style.display = 'block';
85                                         logo.src = 'http://friendfeed.com/static/images/logo-small.png';
86                                         logo.style.padding = '5px';
87                                 } else {
88                                         d.style.display = 'none';
89                                         logo.src = 'http://friendfeed.com/favicon.ico';
90                                         logo.style.padding = 0;
91                                 }
92                         };
93
94                         logo.addEventListener('click', displayToggle, false);
95
96                         var dragging=false;
97                         pdiv.dragged=false;
98                         function drag(ev) {
99                                 if (dragging) {
100                                         var dx = ev.pageX - dragging.x;
101                                         var dy = ev.pageY - dragging.y;
102                                         pdiv.style.left = (parseInt(pdiv.style.left) + dx) + 'px';
103                                         pdiv.style.top = (parseInt(pdiv.style.top) + dy) + 'px';
104                                         dragging.x = ev.pageX;
105                                         dragging.y = ev.pageY;
106                                         ev.preventDefault();
107                                         ev.stopPropagation();
108
109                                         pdiv.dragged = true;
110                                 }
111                         }
112                         logo.addEventListener('mousedown',
113                                         function (ev) {
114                                                 dragging=new Object();
115                                                 dragging.x = ev.pageX;
116                                                 dragging.y = ev.pageY;
117                                                 ev.preventDefault();
118                                                 ev.stopPropagation();
119                                         }, false);
120                         logo.addEventListener('mouseup',
121                                         function (ev) {
122                                                 dragging=false;
123                                                 ev.preventDefault();
124                                                 ev.stopPropagation();
125                                         }, false);
126                         // handle on the whole document because otherwise fast
127                         // movements will make the mouse get out of the logo, thus
128                         // 'dropping' the drag
129                         doc.addEventListener('mousemove', drag, false);
130                 }
131
132
133                 var ecount = str.entries.length;
134                 for (var i=0; i < ecount; ++i) {
135                         var entry = str.entries[i];
136
137                         var p_entry_id = 'ujs_ff_entry/' + entry.id;
138
139                         // skip this entry if we already added it from a different source
140                         if (doc.getElementById(p_entry_id))
141                                 continue;
142
143                         var date2link = function(d, l) {
144                                 var cal = new Date();
145                                 var str
146
147                                 cal.setUTCFullYear(parseInt(d.substring(0,4)));
148                                 cal.setUTCMonth(parseInt(d.substring(5,7)) - 1);
149                                 cal.setUTCDate(parseInt(d.substring(8,10)));
150                                 cal.setUTCHours(parseInt(d.substring(11,13)));
151                                 cal.setUTCMinutes(parseInt(d.substring(14,16)));
152                                 cal.setUTCSeconds(parseInt(d.substring(17,19)));
153
154                                 var delta = (ujs_now - cal)/1000;
155
156                                 if (delta < 48*3600) {
157                                         if (cal.getDate() == ujs_now.getDate()) {
158                                                 str = 'today';
159                                         } else {
160                                                 str = 'yesterday';
161                                         }
162                                 } else if (cal.getYear() == ujs_now.getYear()) {
163                                         str = 'on ' + weekday[cal.getDay()] + ', ' + month[cal.getMonth()] + ' ' + cal.getDate(); 
164                                 } else {
165                                         str = 'on ' + [cal.getYear(), cal.getMonth() + 1, cal.getDate()].join('-');
166                                 }
167
168                                 str += ' at ' + [cal.getHours(), cal.getMinutes()].join(':');
169
170                                 if (l)
171                                         return '<a href="' + l + '">' + str + '</a>';
172                                 else
173                                         return str;
174                         }
175
176                         var from2link = function(from) {
177                                 return '<a href="http://friendfeed.com/' + from.id + '">' + from.name + '</a>';
178                         }
179
180                         var from2linkimg = function(from) {
181                                 return '<a style="margin: 0 1ex .5ex 0;float:left;" href="http://friendfeed.com/' + from.id + '"><img src="http://friendfeed-api.com/v2/picture/' + from.id + '?size=small" /></a>';
182                         }
183
184
185                         var fed = '<p id="' + p_entry_id + '">Fed by ' + from2link(entry.from);
186                         fed += ' ' + date2link(entry.date, entry.url);
187                         var ediv = doc.createElement('div');
188                         ediv.style.margin=0;
189                         ediv.style.padding='.5ex';
190                         ediv.style.borderTop='.25ex dashed lightblue';
191                         ediv.style.textAlign = 'justify';
192                         ediv.style.display = 'block';
193                         ediv.innerHTML = fed
194                         div.appendChild(ediv);
195
196                         var likes = entry.likes || [];
197                         var jcount = likes.length;
198                         if (jcount > 0) {
199                                 var liked = '<p style="margin:0 2ex;text-indent:-2ex">Liked by ';
200                                 for (var j=0; j < jcount; ++j) {
201                                         var like = likes[j];
202                                         if (like.from)
203                                                 liked += (j > 0 ? ', ' : '') + from2link(like.from);
204                                         else
205                                                 liked += ' and ' + like.body;
206                                 }
207
208                                 var ldiv = doc.createElement('div');
209                                 ldiv.innerHTML = liked;
210                                 ediv.appendChild(ldiv);
211                         }
212
213                         var comments = entry.comments || [];
214                         jcount = comments.length;
215                         if (jcount > 0) {
216                                 for (var j=0 ; j < jcount; ++j) {
217                                         var comment = comments[j];
218                                         var cdiv = doc.createElement('div');
219                                         cdiv.style.borderTop = '.1ex dotted blue';
220                                         cdiv.style.padding = '1ex .5ex';
221                                         ediv.appendChild(cdiv);
222
223                                         if (comment.from) {
224                                                 cdiv.innerHTML = from2linkimg(comment.from) +
225                                                         '<p style="margin:0;font-size:smaller">' + date2link(comment.date) + ' by ' + from2link(comment.from) +
226                                                         '<p style="margin:0">' + comment.body;
227                                         } else {
228                                                 cdiv.innerHTML += comment.body;
229                                         }
230                                 }
231
232                         }
233                 }
234         }
235 };
236
237 // escape() does not escape +, but we want it escaped too
238 var encoded_loc = escape(unsafeWindow.location.toString()).replace('+','%2b');
239 var ff_url = 'http://friendfeed-api.com/v2/url?url=' + encoded_loc + '&callback=ujs_ff_load_entries&cache=' + Math.floor(Math.random()*1024*1024);
240
241 unsafeWindow.ujs_ff_load(ff_url);
242
243 ff_url = 'http://friendfeed-api.com/v2/search?q=' + encoded_loc + '&callback=ujs_ff_load_entries&cache=' + Math.floor(Math.random()*1024*1024);
244
245 unsafeWindow.ujs_ff_load(ff_url);
246
247 })();
248