]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - js/identica-badge.js
Merge branch '0.7.x' into cometplugin
[quix0rs-gnu-social.git] / js / identica-badge.js
1 // identica badge -- updated to work with the native API, 12-4-2008
2 // Modified to point to Identi.ca, 2-20-2009 by Zach
3 // copyright Kent Brewster 2008
4 // see http://kentbrewster.com/identica-badge for info
5 ( function() { 
6    var trueName = '';
7    for (var i = 0; i < 16; i++) { 
8       trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); 
9    }
10    window[trueName] = {};
11    var $ = window[trueName];
12    $.f = function() {
13       return { 
14          runFunction : [],
15          init : function(target) {
16             var theScripts = document.getElementsByTagName('SCRIPT');
17             for (var i = 0; i < theScripts.length; i++) {
18                if (theScripts[i].src.match(target)) {
19                   $.a = {};
20                   if (theScripts[i].innerHTML) {
21                      $.a = $.f.parseJson(theScripts[i].innerHTML);
22                   }
23                   if ($.a.err) {
24                      alert('bad json!');
25                   }
26                   $.f.loadDefaults();
27                   $.f.buildStructure();
28                   $.f.buildPresentation();
29                   theScripts[i].parentNode.insertBefore($.s, theScripts[i]);
30                   theScripts[i].parentNode.removeChild(theScripts[i]);
31                   break;
32                }
33             }         
34          },
35          parseJson : function(json) {
36             this.parseJson.data = json;
37             if ( typeof json !== 'string') {
38                return {"err":"trying to parse a non-string JSON object"};
39             }
40             try {
41                var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
42                   'Array,String,Math,RegExp,Image,ActiveXObject;',
43                   'return (' , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function&shy;') , ');'].join(''));
44                return f();
45             } catch (e) {
46                return {"err":"trouble parsing JSON object"};
47             }
48          },
49          loadDefaults : function() {
50             $.d = { 
51                "user":"7000",
52                "headerText" : "",
53                "height" : 350,
54                "width" : 300,
55                "background" : "#193441",
56                "border" : "1px solid black",
57                "userFontSize" : "inherit",
58                "userColor" : "inherit",
59                "headerBackground" : "transparent", 
60                "headerColor" : "white",
61                "evenBackground" : "#fff",
62                "oddBackground" : "#eee",
63                "thumbnailBorder" : "1px solid black",
64                "thumbnailSize" : 24,
65                "padding" : 3,
66                "server" : "identi.ca"
67             };
68             for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
69          },
70           buildPresentation : function () {
71             var ns = document.createElement('style');
72             document.getElementsByTagName('head')[0].appendChild(ns);
73             if (!window.createPopup) {
74                ns.appendChild(document.createTextNode(''));
75                ns.setAttribute("type", "text/css");
76             }
77             var s = document.styleSheets[document.styleSheets.length - 1];
78             var rules = {
79                "" : "{zoom:1;margin:0;padding:0;width:" + $.a.width + "px;background:" + $.a.background + ";border:" + $.a.border + ";font:13px/1.2em tahoma, veranda, arial, helvetica, clean, sans-serif;*font-size:small;*font:x-small;}",
80                "a" : "{cursor:pointer;text-decoration:none;}",
81                "a:hover" : "{text-decoration:underline;}",
82                "cite" : "{font-weight:bold;margin:0 0 0 4px;padding:0;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;}",
83                "cite a" : "{color:#C15D42;}",
84                "date":"{font-size:87%;margin:0 0 0 4px;padding:0;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;}",
85                "date:after" : "{clear:both; content:\".\"; display:block; height:0; visibility:hidden; }",
86                "date a" : "{color:#676;}",
87                "h3" : "{margin:0;padding:" + $.a.padding + "px;font-weight:bold;background:" + $.a.headerBackground + " url('http://" + $.a.server + "/favicon.ico') " + $.a.padding + "px 50% no-repeat;text-indent:" + ($.a.padding + 16) + "px;}",
88                "h3.loading" : "{background-image:url('http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif');}",
89                "h3 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
90                "h4" : "{font-weight:normal; background:" + $.a.headerBackground + ";text-align:right;margin:0;padding:" + $.a.padding + "px;}",
91                "h4 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
92                "img":"{float:left; height:" + $.a.thumbnailSize + "px;width:" + $.a.thumbnailSize + "px;border:" + $.a.thumbnailBorder + ";margin-right:" + $.a.padding + "px;}",
93                "p" : "{margin:0; padding:0;width:" + ($.a.width - 22) + "px;overflow:hidden;font-size:87%;}",
94                "p a" : "{color:#C15D42;}",
95                "ul":"{margin:0; padding:0; height:" + $.a.height + "px;width:" + $.a.width + "px;overflow:auto;}",
96                "ul li":"{background:" + $.a.evenBackground + ";margin:0;padding:" + $.a.padding + "px;list-style:none;width:" + ($.a.width - 22) + "px;overflow:hidden;border-bottom:1px solid #D8E2D7;}",
97                "ul li:hover":"{background:#f3f8ea;}"
98             };
99             var ieRules = "";
100             // brute-force each and every style rule here to !important
101             // sometimes you have to take off and nuke the site from orbit; it's the only way to be sure
102             for (var z in rules) {
103                var selector = '.' + trueName + ' ' + z;
104                var rule = rules[z];
105                if (typeof rule === 'string') {
106                   var important = rule.replace(/;/gi, '!important;');
107                   if (!window.createPopup) {
108                      var theRule = document.createTextNode(selector + important);
109                      ns.appendChild(theRule);
110                   } else {
111                      ieRules += selector + important;
112                   }
113                }
114             }
115             if (window.createPopup) { s.cssText = ieRules; }
116          },
117          buildStructure : function() {
118             $.s = document.createElement('DIV');
119             $.s.className = trueName;         
120             $.s.h = document.createElement('H3');
121             $.s.h.a = document.createElement('A');
122             $.s.h.a.target = '_laconica';
123             $.s.h.appendChild($.s.h.a);
124             $.s.appendChild($.s.h);
125             $.s.r = document.createElement('UL');
126             $.s.appendChild($.s.r);
127             $.s.f = document.createElement('H4');
128             var a = document.createElement('A');
129             a.innerHTML = 'get this';
130             a.target = '_blank';
131             a.href = 'http://identi.ca/doc/badge';
132             $.s.f.appendChild(a);
133             $.s.appendChild($.s.f);
134             $.f.getUser();
135          },
136          getUser : function() {
137             if (!$.f.runFunction) { $.f.runFunction = []; }
138             var n = $.f.runFunction.length;
139             var id = trueName + '.f.runFunction[' + n + ']';
140             $.f.runFunction[n] = function(r) {
141                delete($.f.runFunction[n]);
142                var a = document.createElement('A');
143                a.rel = $.a.user;
144                a.rev = r.name; 
145                a.id = r.screen_name;
146                $.f.removeScript(id);
147                $.f.changeUserTo(a);
148             };
149             var url = 'http://' + $.a.server + '/api/users/show/' + $.a.user + '.json?callback=' + id;
150             $.f.runScript(url, id);
151          },
152          changeUserTo : function(el) {
153             $.a.user = el.rel;
154             $.s.h.a.innerHTML = el.rev + $.a.headerText;
155             $.s.h.a.href = 'http://' + $.a.server + '/' + el.id;
156             $.f.runSearch(); 
157          },
158          runSearch : function() {
159             $.s.h.className = 'loading';
160             $.s.r.innerHTML = '';
161             if (!$.f.runFunction) { $.f.runFunction = []; }
162             var n = $.f.runFunction.length;
163             var id = trueName + '.f.runFunction[' + n + ']';
164             $.f.runFunction[n] = function(r) {
165                delete($.f.runFunction[n]);
166                $.f.removeScript(id);
167                $.f.renderResult(r); 
168             };
169             var url = 'http://' + $.a.server + '/api/statuses/friends/' + $.a.user + '.json?callback=' + id;
170             $.f.runScript(url, id);
171          },
172          renderResult: function(r) { 
173             for (var i = 0; i < r.length; i++) {
174                if (!r[i].status) {
175                   r.splice(i, 1);
176                } else {
177                   r[i].status_id = parseInt(r[i].status.id);
178                }
179             }
180             r = $.f.sortArray(r, "status_id", true);
181             $.s.h.className = '';
182             for (var i = 0; i < r.length; i++) {
183                var li = document.createElement('LI');
184                var icon = document.createElement('A');
185                if (r[i] && r[i].url) {
186                   icon.href = r[i].url;
187                   icon.target = '_laconica'; 
188                   icon.title = 'Visit ' + r[i].screen_name + ' at ' + r[i].url;
189                } else {
190                   icon.href = 'http://' + $.a.server + '/' + r[i].screen_name;
191                   icon.target = '_laconica'; 
192                   icon.title = 'Visit ' + r[i].screen_name + ' at http://' + $.a.server + '/' + r[i].screen_name;
193                }
194
195                var img = document.createElement('IMG');
196                img.src = r[i].profile_image_url;
197                icon.appendChild(img);
198                li.appendChild(icon); 
199                
200                var user = document.createElement('CITE');
201                var a = document.createElement('A');
202                a.rel = r[i].id;
203                a.rev = r[i].name;
204                a.id = r[i].screen_name;
205                a.innerHTML = r[i].name; 
206                a.href = 'http://' + $.a.server + '/' + r[i].screen_name;
207                a.onclick = function() {
208                   $.f.changeUserTo(this);
209                   return false;
210                };
211                user.appendChild(a);
212                li.appendChild(user);
213                var updated = document.createElement('DATE');
214                if (r[i].status && r[i].status.created_at) {
215                   var date_link = document.createElement('A');
216                   date_link.innerHTML = r[i].status.created_at.split(/\+/)[0];
217                   date_link.href = 'http://' + $.a.server + '/notice/' + r[i].status.id;
218                   date_link.target = '_laconica';
219                   updated.appendChild(date_link);
220                   if (r[i].status.in_reply_to_status_id) {
221                      updated.appendChild(document.createTextNode(' in reply to '));
222                      var in_reply_to = document.createElement('A');
223                      in_reply_to.innerHTML = r[i].status.in_reply_to_status_id;
224                      in_reply_to.href = 'http://' + $.a.server + '/notice/' + r[i].status.in_reply_to_status_id;
225                      in_reply_to.target = '_laconica';
226                      updated.appendChild(in_reply_to);
227                   }
228                } else {
229                   updated.innerHTML = 'has not updated yet';
230                }
231                li.appendChild(updated);
232                var p = document.createElement('P');
233                if (r[i].status && r[i].status.text) {
234                   var raw = r[i].status.text;
235                   var cooked = raw;
236                   cooked = cooked.replace(/http:\/\/([^ ]+)/g, "<a href=\"http://$1\" target=\"_laconica\">http://$1</a>");
237                   cooked = cooked.replace(/@([\w*]+)/g, '@<a href="http://' + $.a.server + '/$1" target=\"_laconica\">$1</a>');
238                   cooked = cooked.replace(/#([\w*]+)/g, '#<a href="http://' + $.a.server + '/tag/$1" target="_laconica">$1</a>');
239                   p.innerHTML = cooked;
240                }
241                li.appendChild(p);
242                var a = p.getElementsByTagName('A');
243                for (var j = 0; j < a.length; j++) {
244                   if (a[j].className == 'changeUserTo') {
245                      a[j].className = '';
246                      a[j].href = 'http://' + $.a.server + '/' + a[j].innerHTML;
247                      a[j].rel = a[j].innerHTML;
248                      a[j].onclick = function() { 
249                         $.f.changeUserTo(this); 
250                         return false;
251                      } 
252                   }
253                }
254                $.s.r.appendChild(li);
255             }         
256          },
257          sortArray : function(r, k, x) {
258             if (window.createPopup) { 
259                return r; 
260             }
261             function s(a, b) {
262                if (x === true) {
263                    return b[k] - a[k];
264                } else {
265                    return a[k] - b[k];
266                }
267             }
268             r = r.sort(s);
269             return r;
270          },         
271          runScript : function(url, id) {
272             var s = document.createElement('script');
273             s.id = id;
274             s.type ='text/javascript';
275             s.src = url;
276             document.getElementsByTagName('body')[0].appendChild(s);
277          },
278          removeScript : function(id) {
279             if (document.getElementById(id)) {
280                var s = document.getElementById(id);
281                s.parentNode.removeChild(s);
282             }
283          }         
284       };
285    }();
286 //   var thisScript = /^https?:\/\/[^\/]*r8ar.com\/identica-badge.js$/;
287    var thisScript = /identica-badge.js$/;
288    if(typeof window.addEventListener !== 'undefined') {
289       window.addEventListener('load', function() { $.f.init(thisScript); }, false);
290    } else if(typeof window.attachEvent !== 'undefined') {
291       window.attachEvent('onload', function() { $.f.init(thisScript); });
292    }
293 } )();
294