identica-badge.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // Modified for XHTML, 27-9-2009 by Will Daniels
  4. // (see http://willdaniels.co.uk/blog/tech-stuff/26-identica-badge-xhtml)
  5. // copyright Kent Brewster 2008
  6. // see http://kentbrewster.com/identica-badge for info
  7. function createHTMLElement(tagName) {
  8. if(document.createElementNS)
  9. var elem = document.createElementNS("http://www.w3.org/1999/xhtml", tagName);
  10. else
  11. var elem = document.createElement(tagName);
  12. return elem;
  13. }
  14. function isNumeric(value) {
  15. if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  16. return true;
  17. }
  18. function markupPost(raw, server) {
  19. var start = 0; var p = createHTMLElement('p');
  20. raw.replace(/((http|https):\/\/|\!|@|#)(([\w_]+)?[^\s]*)/g,
  21. function(sub, type, scheme, url, word, offset, full)
  22. {
  23. if(!scheme && !word) return; // just punctuation
  24. var label = ''; var href = '';
  25. var pretext = full.substr(start, offset - start);
  26. moniker = word.split('_'); // behaviour with underscores differs
  27. if(type == '#') moniker = moniker.join('');
  28. else word = moniker = moniker[0].toLowerCase();
  29. switch(type) {
  30. case 'http://': case 'https://': // html links
  31. href = scheme + '://' + url; break;
  32. case '@': // link users
  33. href = 'http://' + server + '/' + moniker; break;
  34. case '!': // link groups
  35. href = 'http://' + server + '/group/' + moniker; break;
  36. case '#': // link tags
  37. href = 'http://' + server + '/tag/' + moniker; break;
  38. default: // bad call (just reset position for text)
  39. start = offset;
  40. }
  41. if(scheme) { // only urls will have scheme
  42. label = sub; start = offset + sub.length;
  43. } else {
  44. label = word; pretext += type;
  45. start = offset + word.length + type.length;
  46. }
  47. p.appendChild(document.createTextNode(pretext));
  48. var link = createHTMLElement('a');
  49. link.appendChild(document.createTextNode(label));
  50. link.href = href; link.target = '_statusnet';
  51. p.appendChild(link);
  52. });
  53. if(start != raw.length) {
  54. endtext = raw.substr(start);
  55. p.appendChild(document.createTextNode(endtext));
  56. }
  57. return p;
  58. }
  59. (function() {
  60. var trueName = '';
  61. for (var i = 0; i < 16; i++) {
  62. trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
  63. }
  64. window[trueName] = {};
  65. var $ = window[trueName];
  66. $.f = function() {
  67. return {
  68. runFunction : [],
  69. init : function(target) {
  70. var theScripts = document.getElementsByTagName('script');
  71. for (var i = 0; i < theScripts.length; i++) {
  72. if (theScripts[i].src.match(target)) {
  73. $.a = {};
  74. if (theScripts[i].innerHTML) {
  75. $.a = $.f.parseJson(theScripts[i].innerHTML);
  76. }
  77. if ($.a.err) {
  78. alert('bad json!');
  79. }
  80. $.f.loadDefaults();
  81. $.f.buildStructure();
  82. $.f.buildPresentation();
  83. theScripts[i].parentNode.insertBefore($.s, theScripts[i]);
  84. theScripts[i].parentNode.removeChild(theScripts[i]);
  85. break;
  86. }
  87. }
  88. },
  89. parseJson : function(json) {
  90. this.parseJson.data = json;
  91. if ( typeof json !== 'string') {
  92. return {"err":"trying to parse a non-string JSON object"};
  93. }
  94. try {
  95. var f = Function(['var document,top,self,window,parent,Number,Date,Object,Function,',
  96. 'Array,String,Math,RegExp,Image,ActiveXObject;',
  97. 'return (' , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function&shy;') , ');'].join(''));
  98. return f();
  99. } catch (e) {
  100. return {"err":"trouble parsing JSON object"};
  101. }
  102. },
  103. loadDefaults : function() {
  104. $.d = {
  105. "user":"7000",
  106. "headerText" : "",
  107. "height" : 350,
  108. "width" : 300,
  109. "background" : "#193441",
  110. "border" : "1px solid black",
  111. "userFontSize" : "inherit",
  112. "userColor" : "inherit",
  113. "headerBackground" : "transparent",
  114. "headerColor" : "white",
  115. "evenBackground" : "#fff",
  116. "oddBackground" : "#eee",
  117. "thumbnailBorder" : "1px solid black",
  118. "thumbnailSize" : 24,
  119. "padding" : 3,
  120. "server" : "identi.ca"
  121. };
  122. for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
  123. // fix inout units
  124. if(isNumeric($.a.width)) {
  125. $.a.innerWidth = ($.a.width - 22) + 'px'; $.a.width += 'px';
  126. } else {
  127. $.a.innerWidth = 'auto';
  128. }
  129. if(isNumeric($.a.height)) $.a.height += 'px';
  130. },
  131. buildPresentation : function () {
  132. var setZoom = ''; if(navigator.appName == 'Microsoft Internet Explorer') setZoom = 'zoom:1;';
  133. var ns = createHTMLElement('style');
  134. document.getElementsByTagName('head')[0].appendChild(ns);
  135. if (!window.createPopup) {
  136. ns.appendChild(document.createTextNode(''));
  137. ns.setAttribute("type", "text/css");
  138. }
  139. var s = document.styleSheets[document.styleSheets.length - 1];
  140. var rules = {
  141. "" : "{margin:0px;padding:0px;width:" + $.a.width + ";background:" + $.a.background + ";border:" + $.a.border + ";font:87%/1.2em tahoma, veranda, arial, helvetica, clean, sans-serif;}",
  142. "a" : "{cursor:pointer;text-decoration:none;}",
  143. "a:hover" : "{text-decoration:underline;}",
  144. ".cite" : "{" + setZoom + "font-weight:bold;margin:0px 0px 0px 4px;padding:0px;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;vertical-align:middle;}",
  145. ".cite a" : "{color:#C15D42;}",
  146. ".date":"{margin:0px 0px 0px 4px;padding:0px;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;vertical-align:middle;}",
  147. ".date:after" : "{clear:both;content:\".\"; display:block;height:0px;visibility:hidden;}",
  148. ".date a" : "{color:#676;}",
  149. "h3" : "{margin:0px;padding:" + $.a.padding + "px;font-weight:bold;background:" + $.a.headerBackground + " url('http://" + $.a.server + "/favicon.ico') " + $.a.padding + "px 50% no-repeat;padding-left:" + ($.a.padding + 20) + "px;}",
  150. "h3.loading" : "{background-image:url('http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif');}",
  151. "h3 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
  152. "h4" : "{font-weight:normal;background:" + $.a.headerBackground + ";text-align:right;margin:0px;padding:" + $.a.padding + "px;}",
  153. "h4 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
  154. "img":"{float:left;height:" + $.a.thumbnailSize + "px;width:" + $.a.thumbnailSize + "px;border:" + $.a.thumbnailBorder + ";margin-right:" + $.a.padding + "px;}",
  155. "p" : "{margin:2px 0px 0px 0px;padding:0px;width:" + $.a.innerWidth + ";overflow:hidden;line-height:normal;}",
  156. "p a" : "{color:#C15D42;}",
  157. "ul":"{margin:0px; padding:0px; height:" + $.a.height + ";width:" + $.a.innerWidth + ";overflow:auto;}",
  158. "ul li":"{background:" + $.a.evenBackground + ";margin:0px;padding:" + $.a.padding + "px;list-style:none;width:auto;overflow:hidden;border-bottom:1px solid #D8E2D7;}",
  159. "ul li:hover":"{background:#f3f8ea;}"
  160. };
  161. var ieRules = "";
  162. // brute-force each and every style rule here to !important
  163. // sometimes you have to take off and nuke the site from orbit; it's the only way to be sure
  164. for (var z in rules) {
  165. if(z.charAt(0)=='.') var selector = '.' + trueName + '-' + z.substring(1);
  166. else var selector = '.' + trueName + ' ' + z;
  167. var rule = rules[z];
  168. if (typeof rule === 'string') {
  169. var important = rule.replace(/;/gi, '!important;');
  170. if (!window.createPopup) {
  171. var theRule = document.createTextNode(selector + important + '\n');
  172. ns.appendChild(theRule);
  173. } else {
  174. ieRules += selector + important;
  175. }
  176. }
  177. }
  178. if (window.createPopup) { s.cssText = ieRules; }
  179. },
  180. buildStructure : function() {
  181. $.s = createHTMLElement('div');
  182. $.s.className = trueName;
  183. $.s.h = createHTMLElement('h3');
  184. $.s.h.a = createHTMLElement('a');
  185. $.s.h.a.target = '_statusnet';
  186. $.s.h.appendChild($.s.h.a);
  187. $.s.appendChild($.s.h);
  188. $.s.r = createHTMLElement('ul');
  189. $.s.appendChild($.s.r);
  190. $.s.f = createHTMLElement('h4');
  191. var a = createHTMLElement('a');
  192. a.innerHTML = 'get this';
  193. a.target = '_blank';
  194. a.href = 'http://identi.ca/doc/badge';
  195. $.s.f.appendChild(a);
  196. $.s.appendChild($.s.f);
  197. $.f.getUser();
  198. },
  199. getUser : function() {
  200. if (!$.f.runFunction) { $.f.runFunction = []; }
  201. var n = $.f.runFunction.length;
  202. var id = trueName + '.f.runFunction[' + n + ']';
  203. $.f.runFunction[n] = function(r) {
  204. delete($.f.runFunction[n]);
  205. var a = createHTMLElement('a');
  206. a.rel = $.a.user;
  207. a.rev = r.name;
  208. a.id = r.screen_name;
  209. $.f.removeScript(id);
  210. $.f.changeUserTo(a);
  211. };
  212. var url = 'http://' + $.a.server + '/api/users/show/' + $.a.user + '.json?callback=' + id;
  213. $.f.runScript(url, id);
  214. },
  215. changeUserTo : function(el) {
  216. $.a.user = el.rel;
  217. $.s.h.a.appendChild(document.createTextNode(el.rev + $.a.headerText));
  218. $.s.h.a.href = 'http://' + $.a.server + '/' + el.id;
  219. $.f.runSearch();
  220. },
  221. runSearch : function() {
  222. $.s.h.className = 'loading';
  223. $.s.r.innerHTML = '';
  224. if (!$.f.runFunction) { $.f.runFunction = []; }
  225. var n = $.f.runFunction.length;
  226. var id = trueName + '.f.runFunction[' + n + ']';
  227. $.f.runFunction[n] = function(r) {
  228. delete($.f.runFunction[n]);
  229. $.f.removeScript(id);
  230. $.f.renderResult(r);
  231. };
  232. var url = 'http://' + $.a.server + '/api/statuses/friends/' + $.a.user + '.json?callback=' + id;
  233. $.f.runScript(url, id);
  234. },
  235. renderResult: function(r) {
  236. for (var i = 0; i < r.length; i++) {
  237. if (!r[i].status) {
  238. r.splice(i, 1);
  239. } else {
  240. r[i].status_id = parseInt(r[i].status.id);
  241. }
  242. }
  243. r = $.f.sortArray(r, "status_id", true);
  244. $.s.h.className = ''; // for IE6
  245. $.s.h.removeAttribute('class');
  246. for (var i = 0; i < r.length; i++) {
  247. var li = createHTMLElement('li');
  248. var icon = createHTMLElement('a');
  249. if (r[i] && r[i].url) {
  250. icon.href = r[i].url;
  251. icon.target = '_statusnet';
  252. icon.title = 'Visit ' + r[i].screen_name + ' at ' + r[i].url;
  253. } else {
  254. icon.href = 'http://' + $.a.server + '/' + r[i].screen_name;
  255. icon.target = '_statusnet';
  256. icon.title = 'Visit ' + r[i].screen_name + ' at http://' + $.a.server + '/' + r[i].screen_name;
  257. }
  258. var img = createHTMLElement('img');
  259. img.alt = 'profile image for ' + r[i].screen_name;
  260. img.src = r[i].profile_image_url;
  261. icon.appendChild(img);
  262. li.appendChild(icon);
  263. var user = createHTMLElement('span');
  264. user.className = trueName + '-cite';
  265. var a = createHTMLElement('a');
  266. a.rel = r[i].id;
  267. a.rev = r[i].name;
  268. a.id = r[i].screen_name;
  269. a.innerHTML = r[i].name;
  270. a.href = 'http://' + $.a.server + '/' + r[i].screen_name;
  271. a.onclick = function() {
  272. $.f.changeUserTo(this);
  273. return false;
  274. };
  275. user.appendChild(a);
  276. li.appendChild(user);
  277. var updated = createHTMLElement('span');
  278. updated.className = trueName + '-date';
  279. if (r[i].status && r[i].status.created_at) {
  280. var date_link = createHTMLElement('a');
  281. date_link.innerHTML = r[i].status.created_at.split(/\+/)[0];
  282. date_link.href = 'http://' + $.a.server + '/notice/' + r[i].status.id;
  283. date_link.target = '_statusnet';
  284. updated.appendChild(date_link);
  285. if (r[i].status.in_reply_to_status_id) {
  286. updated.appendChild(document.createTextNode(' in reply to '));
  287. var in_reply_to = createHTMLElement('a');
  288. in_reply_to.innerHTML = r[i].status.in_reply_to_status_id;
  289. in_reply_to.href = 'http://' + $.a.server + '/notice/' + r[i].status.in_reply_to_status_id;
  290. in_reply_to.target = '_statusnet';
  291. updated.appendChild(in_reply_to);
  292. }
  293. } else {
  294. updated.innerHTML = 'has not updated yet';
  295. }
  296. li.appendChild(updated);
  297. var p = createHTMLElement('p');
  298. if (r[i].status && r[i].status.text) {
  299. var raw = r[i].status.text;
  300. p = markupPost(raw, $.a.server);
  301. }
  302. li.appendChild(p);
  303. var a = p.getElementsByTagName('a');
  304. for (var j = 0; j < a.length; j++) {
  305. if (a[j].className == 'changeUserTo') {
  306. a[j].removeAttribute('class');
  307. a[j].href = 'http://' + $.a.server + '/' + a[j].innerHTML;
  308. a[j].rel = a[j].innerHTML;
  309. a[j].onclick = function() {
  310. $.f.changeUserTo(this);
  311. return false;
  312. }
  313. }
  314. }
  315. $.s.r.appendChild(li);
  316. }
  317. },
  318. sortArray : function(r, k, x) {
  319. if (window.createPopup) {
  320. return r;
  321. }
  322. function s(a, b) {
  323. if (x === true) {
  324. return b[k] - a[k];
  325. } else {
  326. return a[k] - b[k];
  327. }
  328. }
  329. r = r.sort(s);
  330. return r;
  331. },
  332. runScript : function(url, id) {
  333. var s = createHTMLElement('script');
  334. s.id = id;
  335. s.type ='text/javascript';
  336. s.src = url;
  337. document.getElementsByTagName('body')[0].appendChild(s);
  338. },
  339. removeScript : function(id) {
  340. if (document.getElementById(id)) {
  341. var s = document.getElementById(id);
  342. s.parentNode.removeChild(s);
  343. }
  344. }
  345. };
  346. }();
  347. // var thisScript = /^https?:\/\/[^\/]*r8ar.com\/identica-badge.js$/;
  348. var thisScript = /identica-badge.js$/;
  349. if(typeof window.addEventListener !== 'undefined') {
  350. window.addEventListener('load', function() { $.f.init(thisScript); }, false);
  351. } else if(typeof window.attachEvent !== 'undefined') {
  352. window.attachEvent('onload', function() { $.f.init(thisScript); });
  353. }
  354. } )();