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
6 for (var i = 0; i < 16; i++) {
7 trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
10 var $ = window[trueName];
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)) {
19 if (theScripts[i].innerHTML) {
20 $.a = $.f.parseJson(theScripts[i].innerHTML);
27 $.f.buildPresentation();
28 theScripts[i].parentNode.insertBefore($.s, theScripts[i]);
29 theScripts[i].parentNode.removeChild(theScripts[i]);
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"};
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­') , ');'].join(''));
45 return {"err":"trouble parsing JSON object"};
48 loadDefaults : function() {
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",
65 "server" : "identi.ca"
67 for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
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");
76 var s = document.styleSheets[document.styleSheets.length - 1];
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;}"
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;
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);
110 ieRules += selector + important;
114 if (window.createPopup) { s.cssText = ieRules; }
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';
130 a.href = 'http://kentbrewster.com/identica-badge';
131 $.s.f.appendChild(a);
132 $.s.appendChild($.s.f);
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');
144 a.id = r.screen_name;
145 $.f.removeScript(id);
148 var url = 'http://' + $.a.server + '/api/users/show/' + $.a.user + '.json?callback=' + id;
149 $.f.runScript(url, id);
151 changeUserTo : function(el) {
153 $.s.h.a.innerHTML = el.rev + $.a.headerText;
154 $.s.h.a.href = 'http://' + $.a.server + '/' + el.id;
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);
168 var url = 'http://' + $.a.server + '/api/statuses/friends/' + $.a.user + '.json?callback=' + id;
169 $.f.runScript(url, id);
171 renderResult: function(r) {
172 for (var i = 0; i < r.length; i++) {
176 r[i].status_id = parseInt(r[i].status.id);
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;
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;
194 var img = document.createElement('IMG');
195 img.src = r[i].profile_image_url;
196 icon.appendChild(img);
197 li.appendChild(icon);
199 var user = document.createElement('CITE');
200 var a = document.createElement('A');
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);
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);
228 updated.innerHTML = 'has not updated yet';
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;
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;
241 var a = p.getElementsByTagName('A');
242 for (var j = 0; j < a.length; j++) {
243 if (a[j].className == 'changeUserTo') {
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);
253 $.s.r.appendChild(li);
256 sortArray : function(r, k, x) {
257 if (window.createPopup) {
270 runScript : function(url, id) {
271 var s = document.createElement('script');
273 s.type ='text/javascript';
275 document.getElementsByTagName('body')[0].appendChild(s);
277 removeScript : function(id) {
278 if (document.getElementById(id)) {
279 var s = document.getElementById(id);
280 s.parentNode.removeChild(s);
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); });