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.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';
48 doc.body.appendChild(pdiv);
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);
57 var div = doc.createElement('div');
59 for (var i in str.entries) {
60 var entry = str.entries[i];
62 var date2link = function(d, l) {
63 var day = d.substring(0, 10);
64 var tm = d.substring(11, 19);
66 return '<a href="' + l + '">' + day + ' at ' + tm + '</a>';
68 return day + ' at ' + tm;
70 var when = entry.date.substring(0, 16).replace('T', ' ');
72 var from2link = function(from) {
73 return '<a href="http://friendfeed.com/' + from.id + '">' + from.name + '</a>';
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>';
81 var fed = '<p>Fed by ' + from2link(entry.from);
82 fed += ' on ' + date2link(entry.date, entry.url);
83 var ediv = doc.createElement('div');
85 ediv.style.padding='.5ex';
86 ediv.style.borderTop='.25ex dashed lightblue';
87 ediv.style.textAlign = 'justify';
89 div.appendChild(ediv);
91 var likes = entry.likes || [];
92 if (likes.length > 0) {
93 var liked = '<p>Liked by ';
94 for (var j in likes) {
97 liked += (j > 0 ? ', ' : '') + from2link(like.from);
99 liked += ' and ' + like.body;
102 var ldiv = doc.createElement('div');
103 ldiv.innerHTML = liked;
104 ediv.appendChild(ldiv);
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);
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;
120 cdiv.innerHTML += comment.body;
127 div.style.display = 'none';
128 pdiv.appendChild(div);
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';
136 d.style.display = 'none';
137 logo.src = 'http://friendfeed.com/favicon.ico';
140 logo.addEventListener('click', displayToggle, false);
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';
148 unsafeWindow.ujs_ff_load(ff_url);