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
7 for (var i = 0; i < 16; i++) {
8 trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
10 window[trueName] = {};
11 var $ = window[trueName];
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)) {
20 if (theScripts[i].innerHTML) {
21 $.a = $.f.parseJson(theScripts[i].innerHTML);
28 $.f.buildPresentation();
29 theScripts[i].parentNode.insertBefore($.s, theScripts[i]);
30 theScripts[i].parentNode.removeChild(theScripts[i]);
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"};
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­') , ');'].join(''));
46 return {"err":"trouble parsing JSON object"};
49 loadDefaults : function() {
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",
66 "server" : "identi.ca"
68 for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
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");
77 var s = document.styleSheets[document.styleSheets.length - 1];
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;}"
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;
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);
111 ieRules += selector + important;
115 if (window.createPopup) { s.cssText = ieRules; }
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';
131 a.href = 'http://identi.ca/doc/badge';
132 $.s.f.appendChild(a);
133 $.s.appendChild($.s.f);
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');
145 a.id = r.screen_name;
146 $.f.removeScript(id);
149 var url = 'http://' + $.a.server + '/api/users/show/' + $.a.user + '.json?callback=' + id;
150 $.f.runScript(url, id);
152 changeUserTo : function(el) {
154 $.s.h.a.innerHTML = el.rev + $.a.headerText;
155 $.s.h.a.href = 'http://' + $.a.server + '/' + el.id;
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);
169 var url = 'http://' + $.a.server + '/api/statuses/friends/' + $.a.user + '.json?callback=' + id;
170 $.f.runScript(url, id);
172 renderResult: function(r) {
173 for (var i = 0; i < r.length; i++) {
177 r[i].status_id = parseInt(r[i].status.id);
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;
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;
195 var img = document.createElement('IMG');
196 img.src = r[i].profile_image_url;
197 icon.appendChild(img);
198 li.appendChild(icon);
200 var user = document.createElement('CITE');
201 var a = document.createElement('A');
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);
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);
229 updated.innerHTML = 'has not updated yet';
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;
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;
242 var a = p.getElementsByTagName('A');
243 for (var j = 0; j < a.length; j++) {
244 if (a[j].className == 'changeUserTo') {
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);
254 $.s.r.appendChild(li);
257 sortArray : function(r, k, x) {
258 if (window.createPopup) {
271 runScript : function(url, id) {
272 var s = document.createElement('script');
274 s.type ='text/javascript';
276 document.getElementsByTagName('body')[0].appendChild(s);
278 removeScript : function(id) {
279 if (document.getElementById(id)) {
280 var s = document.getElementById(id);
281 s.parentNode.removeChild(s);
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); });