使用滔滔给WordPress添加博主状态
断桥残雪在写Follow5 WordPress插件以及Follow5 API的时候就已经知道了腾讯QQ的滔滔接口,可是腾讯QQ的滔滔一直很低调,不知是因为害怕出现饭否的结局呢?还是腾讯根本不想推广,如果腾讯推广滔滔,凭着QQ的强大用户群体,应该是轻而易举的。滔滔是QQ的一个微博,功能上类似于国外的twitter,我在很早就获得了滔滔未公布的API接口,比如我的滔滔的接口地址是:http://www.taotao.com/cgi-bin/msgj?qq=94756618&num=10&t=1
接口参数解释:
qq:QQ号
num:获取滔滔的条数
t:类型,目前我测试只有0,1两个类型,其他的类型应该也存在可是好像官方屏蔽了,0为自己的滔滔内容,1包括自己给好友的滔滔内容
滔滔接口返回的数据时JSONP的格式,如果你JS比较熟悉并且想自己安排滔滔显示的内容,可以自己根据API返回的数据格式写一个doApi的JS函数,关于获取滔滔头像图片地址的方法点击此处。如果你不是很熟悉可以参考滔滔官方的doApi代码,地址是:http://www.taotao.com/js/dkapi.js。当然断桥残雪也根据滔滔官方的doApi函数的代码改写了一个适合大众使用的一段JS代码,此代码可以获取滔滔最新的状态,添加到固定ID的div中去。
滔滔API的JS处理代码
下面的代码是修改自官方的doApi代码,大家可以自己写,我就不罗嗦啦~直接看代码
代码中taotao_qq为自己的QQ号;taotao_num为显示滔滔的条数;taotao_type为返回数据的类型,0只显示当前叨客的滔滔,1包括当前叨客回复叨友的内容,并且自动添加@来区分; taotao_target为插入div的IDvar taotao_qq="94756618"; var taotao_num=3;var taotao_type=1;var taotao_target='taotao_target'; var MAX_ITEMS = 20, taotao_pane_id = "id_apiPane", KEY_WORD_YJ = "[假]"; var taotao_sr = new Array('网页', 'QQ机器人', 'QQ空间', 'QQ签名', '短信', '手机上网', 'MSN机器人', 'MSN签名', '手机QQ', '彩信', '订阅', "", 'QQ状态'); function LK(str) { return document.createElement(str); } function LL(str) { return document.getElementById(str); } function LG(str, target) { var re = new RegExp("http(s)?://([a-z0-9?.=%&-_;#\\/])+", "ig"); if (target == '_blank') { str = str.replace(re, "<a target='_blank' href='$&'>$&</a>"); } else { str = str.replace(re, "<a href='$&'>$&</a>"); } return str; } function LH(str) { if (typeof(str) == "string") { str = str.replace(/[\r\n\t\0]/g, ""); return str; } } function LE(str, target) { str = LH(str); var re = /(\[URL=(.[^\[]*)\])(.*?)(\[\/URL\])/ig; if (target == '_blank') { str = str.replace(re, "<A HREF=$2 TARGET=_blank>$3</A>"); } else { str = str.replace(re, "<A HREF=$2>$3</A>"); } return str; } function initApi() { if (typeof taotao_qq == 'undefined') { alert("we can not get qq num,show nothing...!"); return; } if (typeof taotao_num == 'undefined' || taotao_num <= 0 || taotao_num > 20) { taotao_num = 20; } if (typeof taotao_type == 'undefined' || taotao_type < 0 || taotao_type > 1) { taotao_type = 0; } } function LI(url) { g_cximg = null; g_cximg = new Array(); var re = /\[IMG\](.+?)\[\/IMG\]/ig, tt = url.match(re); g_ubbcon = url.replace(re, ''); for (var i = 0; i < tt.length; ++i) { g_cximg[g_cximg.length] = tt[i].replace(re, "$1"); } } function LF(str) { if (str.substr(0, 3) == KEY_WORD_YJ) { str = '<font style="color:red">' + KEY_WORD_YJ + '</font>' + str.substr(3, str.length - 3); } return str; } function doApi(obj) { if (obj.ret != 0) { LL(taotao_pane_id).innerHTML = "对不起!暂时无法获取滔滔的唠叨,请稍候重试..."; return; } var qq = obj.ui.qq, name = obj.ui.name, usn = obj.ui.usn, rank = obj.ui.rank, lrank = obj.ui.lrank, rec = obj.rec; var arr = obj.posts, i = 0, data, pane = LL(taotao_pane_id), li, span, a, bD = false; for (i = 0; i < arr.length; i++) { data = arr[i]; li = LK("li"); li.style.wordBreak = "break-all"; li.style.height = "auto"; li.style.overflowY = "auto"; if (obj.type == 1) { a = LK('a'); a.style.marginRight = "8px"; a.innerText = data.nm; a.textContent = data.nm; a.href = "http://www.taotao.com/v1/space/" + data.qq; li.appendChild(a); if (data.pqq) { li.appendChild(document.createTextNode("@")); a = LK('a'); a.style.marginRight = "8px"; a.innerText = data.pnm?data.pnm:data.pqq; a.textContent = data.pnm?data.pnm:data.pqq; a.href = "http://www.taotao.com/v1/space/" + data.pqq; li.appendChild(a); } } span = LK("span"); if (data.sr == 10) { span.innerHTML = LE(data.cn, "_blank"); } else if (data.sr == 9) { LI(data.cn); var cxlen = g_cximg.length; span.innerHTML = g_ubbcon + ' <a target="_blank" href="http://www.taotao.com/caixin.shtml?qq=' + qq + "&tid=" + data.id + '">点击查看彩信</a>'; } else { span.innerHTML = LF(LG(data.cn, "_blank")); } li.appendChild(span); span = LK("span"); span.style.marginLeft = "8px"; a = LK('a'); a.style.color = "#929091"; if (parseInt(data.pqq) > 0) { a.href = 'http://www.taotao.com/v1/reply/t.' + data.pid + '/u.' + data.pqq; } else { a.href = 'http://www.taotao.com/v1/reply/t.' + data.id + '/u.' + qq; } a.innerHTML = LJ(data.time); li.appendChild(span); span.appendChild(a); span = LK("span"); span.style.marginLeft = "4px"; span.innerText = "通过 " + taotao_sr[data.sr]; span.textContent = "通过 " + taotao_sr[data.sr]; li.appendChild(span); pane.appendChild(li); } } function LJ(t) { if (t.charAt(1) != ',') { return t; } if (t.length < 2) { return ""; } var n = t.charAt(0), v = t.substr(2, t.length), s = ''; if (n == 1) { s = '约 ' + v + ' 秒前'; } else if (n == 2) { s = '约 ' + v + ' 分钟前'; } else if (n == 3) { s = '约 ' + v + ' 小时前'; } else if (n == 4) { s = '约 1 天前'; } else if (n == 5) { s = '约 3 天前'; } else if (n == 6) { s = '约 1 周前'; } else if (n == 7) { s = v; } return s; } function inclApi() { var src = "http://www.taotao.com/cgi-bin/msgj?qq=" + taotao_qq + "&num=" + taotao_num + "&t=" + taotao_type, htm = '<div>' + '<ul id=' + taotao_pane_id + ' style="margin-bottom:5px">' + '</ul>' + '<table cellpadding="0" cellspacing="0" border="0" width="100%">' + '<tr>' + '<td align="left">' + '<a href="http://www.taotao.com/v1/space/' + taotao_qq + '" style="color:#0278c2;float:left;display:block;">更多唠叨</a>' + '</td>' + '<td align="right">' + '</td>' + '</tr>' + '</table>' + '</div>'; LL(taotao_target).innerHTML=htm; window.document.write('<sc' + 'ript type="text/javascript" charset="utf-8" src="' + src + '"></' + 'script>'); } initApi(); inclApi();
代码使用方法
在想要添加滔滔状态的地方,写一个div,id为“taotao_target”(此ID可以在上面代码中修改),然后在自己博客网站的底部,body之前添加上以上JS代码就可以啦,或者也可保存为JS文件,在body标签之前引入即可,WordPress用户,可以找到模板的footer.php的文件在body标签之前添加或者引入上面的js即可。添加到网站的底部主要是为了不影响网站的加载。
如果你想要做的更加绚丽一点,可以改改CSS或者通过jQuery做一个漂亮的动画效果