2 // @name FriendFeed comments
3 // @description Add FriendFeed 'likes' and comments to any page
4 // @namespace http://oblomov.myopenid.com
6 // @exclude http://friendfeed.com/*
7 // @exclude http://*.friendfeed.com/*
8 // @exclude http://friendfeed-api.com/*
9 // @author Giuseppe "Oblomov" Bilotta
13 if (typeof(unsafeWindow) == 'undefined') unsafeWindow = window;
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';
22 unsafeWindow.ujs_now = new Date();
24 unsafeWindow.ujs_ff_load = function(url) {
25 var e = unsafeWindow.document.createElement('script');
26 e.type = 'text/javascript';
28 unsafeWindow.document.getElementsByTagName('head')[0].appendChild(e);
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"];
35 if (str.entries && str.entries.length > 0) {
36 var doc = unsafeWindow.document;
38 var pdiv = doc.getElementById('ujs_friendfeed');
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';
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';
61 doc.body.appendChild(pdiv);
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';
69 pdiv.appendChild(logo);
71 div = doc.createElement('div');
72 div.style.fontSize = 'small';
73 div.style.display = 'none';
74 pdiv.appendChild(div);
76 function displayToggle() {
77 // don't toggle if we were dragging
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';
88 d.style.display = 'none';
89 logo.src = 'http://friendfeed.com/favicon.ico';
90 logo.style.padding = 0;
94 logo.addEventListener('click', displayToggle, false);
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;
107 ev.stopPropagation();
112 logo.addEventListener('mousedown',
114 dragging=new Object();
115 dragging.x = ev.pageX;
116 dragging.y = ev.pageY;
118 ev.stopPropagation();
120 logo.addEventListener('mouseup',
124 ev.stopPropagation();
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);
133 var ecount = str.entries.length;
134 for (var i=0; i < ecount; ++i) {
135 var entry = str.entries[i];
137 var p_entry_id = 'ujs_ff_entry/' + entry.id;
139 // skip this entry if we already added it from a different source
140 if (doc.getElementById(p_entry_id))
143 var date2link = function(d, l) {
144 var cal = new Date();
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)));
154 var delta = (ujs_now - cal)/1000;
156 if (delta < 48*3600) {
157 if (cal.getDate() == ujs_now.getDate()) {
162 } else if (cal.getYear() == ujs_now.getYear()) {
163 str = 'on ' + weekday[cal.getDay()] + ', ' + month[cal.getMonth()] + ' ' + cal.getDate();
165 str = 'on ' + [cal.getYear(), cal.getMonth() + 1, cal.getDate()].join('-');
168 str += ' at ' + [cal.getHours(), cal.getMinutes()].join(':');
171 return '<a href="' + l + '">' + str + '</a>';
176 var from2link = function(from) {
177 return '<a href="http://friendfeed.com/' + from.id + '">' + from.name + '</a>';
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>';
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');
189 ediv.style.padding='.5ex';
190 ediv.style.borderTop='.25ex dashed lightblue';
191 ediv.style.textAlign = 'justify';
192 ediv.style.display = 'block';
194 div.appendChild(ediv);
196 var likes = entry.likes || [];
197 var jcount = likes.length;
199 var liked = '<p style="margin:0 2ex;text-indent:-2ex">Liked by ';
200 for (var j=0; j < jcount; ++j) {
203 liked += (j > 0 ? ', ' : '') + from2link(like.from);
205 liked += ' and ' + like.body;
208 var ldiv = doc.createElement('div');
209 ldiv.innerHTML = liked;
210 ediv.appendChild(ldiv);
213 var comments = entry.comments || [];
214 jcount = comments.length;
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);
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;
228 cdiv.innerHTML += comment.body;
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);
241 unsafeWindow.ujs_ff_load(ff_url);
243 ff_url = 'http://friendfeed-api.com/v2/search?q=' + encoded_loc + '&callback=ujs_ff_load_entries&cache=' + Math.floor(Math.random()*1024*1024);
245 unsafeWindow.ujs_ff_load(ff_url);