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