First working version
[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     20091218
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 unsafeWindow.ujs_ff_load = function(url) {
22         var e = unsafeWindow.document.createElement('script');
23         e.type = 'text/javascript';
24         e.src = url;
25         unsafeWindow.document.getElementsByTagName('head')[0].appendChild(e);
26 };
27
28 unsafeWindow.ujs_ff_load_entries = function(str) {
29         if (str.entries && str.entries.length > 0) {
30                 var doc = unsafeWindow.document;
31
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';
36                 pdiv.style.top = 0;
37                 pdiv.style.left = 0;
38                 pdiv.style.zIndex = 100000;
39                 pdiv.style.maxWidth = '30%';
40                 pdiv.style.border='3px outset lightblue';
41                 pdiv.style.margin = 0;
42                 pdiv.style.padding = 0;
43                 pdiv.style.textAlign = 'center';
44                 pdiv.style.fontSize = 'small';
45                 //  pdiv.style.opacity = 0.7;
46                 pdiv.style.backgroundColor = 'white';
47
48                 doc.body.appendChild(pdiv);
49
50                 var logo = doc.createElement('img');
51                 logo.src = 'http://friendfeed.com/favicon.ico';
52                 logo.setAttribute('alt', 'FriendFeed logo');
53                 logo.setAttribute('title', 'FriendFeed logo');
54                 logo.style.margin='0 auto 0 auto';
55                 pdiv.appendChild(logo);
56
57                 var div = doc.createElement('div');
58
59                 for (var i in str.entries) {
60                         var entry = str.entries[i];
61
62                         var date2link = function(d, l) {
63                                 var day = d.substring(0, 10);
64                                 var tm = d.substring(11, 19);
65                                 if (l)
66                                         return '<a href="' + l + '">' + day + ' at ' + tm + '</a>';
67                                 else
68                                         return day + ' at ' + tm;
69                         }
70                         var when = entry.date.substring(0, 16).replace('T', ' ');
71
72                         var from2link = function(from) {
73                                 return '<a href="http://friendfeed.com/' + from.id + '">' + from.name + '</a>';
74                         }
75
76                         var from2linkimg = function(from) {
77                                 return '<a style="margin: .5ex .5ex 0 0;float:left;" href="http://friendfeed.com/' + from.id + '"><img src="http://friendfeed-api.com/v2/picture/' + from.id + '?size=small" /></a>';
78                         }
79
80
81                         var fed = '<p>Fed by ' + from2link(entry.from);
82                         fed += ' on ' + date2link(entry.date, entry.url);
83                         var ediv = doc.createElement('div');
84                         ediv.style.margin=0;
85                         ediv.style.padding='.5ex';
86                         ediv.style.borderTop='.25ex dashed lightblue';
87                         ediv.style.textAlign = 'justify';
88                         ediv.innerHTML = fed
89                         div.appendChild(ediv);
90
91                         var likes = entry.likes || [];
92                         if (likes.length > 0) {
93                                 var liked = '<p>Liked by ';
94                                 for (var j in likes) {
95                                         var like = likes[j];
96                                         if (like.from)
97                                                 liked += (j > 0 ? ', ' : '') + from2link(like.from);
98                                         else
99                                                 liked += ' and ' + like.body;
100                                 }
101
102                                 var ldiv = doc.createElement('div');
103                                 ldiv.innerHTML = liked;
104                                 ediv.appendChild(ldiv);
105                         }
106                         var comments = entry.comments || [];
107                         if (comments.length > 0) {
108                                 for (var j in comments) {
109                                         var comment = comments[j];
110                                         var cdiv = doc.createElement('div');
111                                         cdiv.style.borderTop = '.1ex dotted blue';
112                                         cdiv.style.padding = '1ex .5ex';
113                                         ediv.appendChild(cdiv);
114
115                                         if (comment.from) {
116                                                 cdiv.innerHTML = from2linkimg(comment.from) +
117                                                         '<p style="margin:0;font-size:smaller">' + date2link(comment.date) + ' by ' + from2link(comment.from) +
118                                                         '<p style="margin:0">' + comment.body;
119                                         } else {
120                                                 cdiv.innerHTML += comment.body;
121                                         }
122                                 }
123
124                         }
125                 }
126
127                 div.style.display = 'none';
128                 pdiv.appendChild(div);
129
130                 function displayToggle() {
131                         var d = pdiv.lastChild;
132                         if (d.style.display == 'none') {
133                                 d.style.display = 'block';
134                                 logo.src = 'http://friendfeed.com/static/images/logo-small.png';
135                         } else {
136                                 d.style.display = 'none';
137                                 logo.src = 'http://friendfeed.com/favicon.ico';
138                         }
139                 };
140                 logo.addEventListener('click', displayToggle, false);
141         }
142 };
143
144 // escape() does not escape +, but we want it escaped too
145 var encoded_loc = escape(unsafeWindow.location.toString()).replace('+','%2b');
146 var ff_url = 'http://friendfeed-api.com/v2/url?url=' + encoded_loc + '&callback=ujs_ff_load_entries';
147
148 unsafeWindow.ujs_ff_load(ff_url);
149
150 })();
151