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';
21 unsafeWindow.ujs_ff_load = function(url) {
22 var e = unsafeWindow.document.createElement('script');
23 e.type = 'text/javascript';
25 unsafeWindow.document.getElementsByTagName('head')[0].appendChild(e);
28 unsafeWindow.ujs_ff_load_entries = function(str) {
29 if (str.entries && str.entries.length > 0) {
30 var doc = unsafeWindow.document;
32 var pdiv = doc.createElement('div');
33 pdiv.setAttribute('id', 'ujs_friendfeed');
34 pdiv.setAttribute('title', 'FriendFeed comments on this page');
35 pdiv.style.position = 'absolute';
38 pdiv.style.zIndex = 100000;
39 pdiv.style.maxWidth = '30%';
40 pdiv.style.height='auto';
41 pdiv.style.border='3px outset lightblue';
42 pdiv.style.margin = 0;
43 pdiv.style.padding = 0;
44 pdiv.style.textAlign = 'center';
45 pdiv.style.fontSize = '0px';
46 pdiv.style.lineHeight = 1;
47 // pdiv.style.opacity = 0.7;
48 pdiv.style.backgroundColor = 'white';
50 doc.body.appendChild(pdiv);
52 var logo = doc.createElement('img');
53 logo.src = 'http://friendfeed.com/favicon.ico';
54 logo.setAttribute('alt', 'FriendFeed logo');
55 logo.setAttribute('title', 'FriendFeed logo');
56 logo.style.margin='0 auto 0 auto';
58 pdiv.appendChild(logo);
60 var div = doc.createElement('div');
61 div.style.fontSize = 'small';
63 var ecount = str.entries.length;
64 for (var i=0; i < ecount; ++i) {
65 var entry = str.entries[i];
67 var date2link = function(d, l) {
68 var day = d.substring(0, 10);
69 var tm = d.substring(11, 19);
71 return '<a href="' + l + '">' + day + ' at ' + tm + '</a>';
73 return day + ' at ' + tm;
76 var from2link = function(from) {
77 return '<a href="http://friendfeed.com/' + from.id + '">' + from.name + '</a>';
80 var from2linkimg = function(from) {
81 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>';
85 var fed = '<p>Fed by ' + from2link(entry.from);
86 fed += ' on ' + date2link(entry.date, entry.url);
87 var ediv = doc.createElement('div');
89 ediv.style.padding='.5ex';
90 ediv.style.borderTop='.25ex dashed lightblue';
91 ediv.style.textAlign = 'justify';
93 div.appendChild(ediv);
95 var likes = entry.likes || [];
96 var jcount = likes.length;
98 var liked = '<p style="margin:0 2ex;text-indent:-2ex">Liked by ';
99 for (var j=0; j < jcount; ++j) {
102 liked += (j > 0 ? ', ' : '') + from2link(like.from);
104 liked += ' and ' + like.body;
107 var ldiv = doc.createElement('div');
108 ldiv.innerHTML = liked;
109 ediv.appendChild(ldiv);
112 var comments = entry.comments || [];
113 jcount = comments.length;
115 for (var j=0 ; j < jcount; ++j) {
116 var comment = comments[j];
117 var cdiv = doc.createElement('div');
118 cdiv.style.borderTop = '.1ex dotted blue';
119 cdiv.style.padding = '1ex .5ex';
120 ediv.appendChild(cdiv);
123 cdiv.innerHTML = from2linkimg(comment.from) +
124 '<p style="margin:0;font-size:smaller">' + date2link(comment.date) + ' by ' + from2link(comment.from) +
125 '<p style="margin:0">' + comment.body;
127 cdiv.innerHTML += comment.body;
134 div.style.display = 'none';
135 pdiv.appendChild(div);
137 function displayToggle() {
138 // don't toggle if we were dragging
140 pdiv.dragged = false;
143 var d = pdiv.lastChild;
144 if (d.style.display == 'none') {
145 d.style.display = 'block';
146 logo.src = 'http://friendfeed.com/static/images/logo-small.png';
147 logo.style.padding = '5px';
149 d.style.display = 'none';
150 logo.src = 'http://friendfeed.com/favicon.ico';
151 logo.style.padding = 0;
154 logo.addEventListener('click', displayToggle, false);
160 var dx = ev.pageX - dragging.x;
161 var dy = ev.pageY - dragging.y;
162 pdiv.style.left = (parseInt(pdiv.style.left) + dx) + 'px';
163 pdiv.style.top = (parseInt(pdiv.style.top) + dy) + 'px';
164 dragging.x = ev.pageX;
165 dragging.y = ev.pageY;
167 ev.stopPropagation();
172 logo.addEventListener('mousedown',
174 dragging=new Object();
175 dragging.x = ev.pageX;
176 dragging.y = ev.pageY;
178 ev.stopPropagation();
180 logo.addEventListener('mouseup',
184 ev.stopPropagation();
186 // handle on the whole document because otherwise fast
187 // movements will make the mouse get out of the logo, thus
188 // 'dropping' the drag
189 doc.addEventListener('mousemove', drag, false);
193 // escape() does not escape +, but we want it escaped too
194 var encoded_loc = escape(unsafeWindow.location.toString()).replace('+','%2b');
195 var ff_url = 'http://friendfeed-api.com/v2/url?url=' + encoded_loc + '&callback=ujs_ff_load_entries';
197 unsafeWindow.ujs_ff_load(ff_url);