Tieba Chat

Tieba Chat | 这是一个实现在网页端使用贴吧客户端聊天功能的脚本。通过该脚本,您可以与使用贴吧客户端的好友聊天

当前为 2014-07-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Tieba Chat
  3. // @version 1.1
  4. // @description Tieba Chat | 这是一个实现在网页端使用贴吧客户端聊天功能的脚本。通过该脚本,您可以与使用贴吧客户端的好友聊天
  5. // @match http://tieba.baidu.com/*
  6. // @include http://tieba.baidu.com/*
  7. // @author 864907600cc
  8. // @icon http://1.gravatar.com/avatar/147834caf9ccb0a66b2505c753747867
  9. // @run-at document-end
  10. // @grant none
  11. // @namespace http://ext.ccloli.com
  12. // ==/UserScript==
  13.  
  14. // 本脚本基于 GPLv3 协议开源 http://www.gnu.org/licenses/gpl.html‎
  15. // (c) 86497600cc. Some Rights Reserved.
  16.  
  17.  
  18. var hex_md5 = (function(g,k){function l(a){return n(m(o(a)))};function m(a){return h(j(p(a),a.length*8))};function n(e){try{}catch(a){g=0};var b=g?'0123456789ABCDEF':'0123456789abcdef';var c='';var d;for(var a=0;a<e.length;a++)d=e.charCodeAt(a),c+=b.charAt(d>>>4&15)+b.charAt(d&15);return c};function o(d){var b='';var c=-1;var a,e;while(++c<d.length)a=d.charCodeAt(c),e=c+1<d.length?d.charCodeAt(c+1):0,55296<=a&&a<=56319&&56320<=e&&e<=57343&&(a=65536+((a&1023)<<10)+(e&1023),c++),a<=127?b+=String.fromCharCode(a):a<=2047?b+=String.fromCharCode(192|a>>>6&31,128|a&63):a<=65535?b+=String.fromCharCode(224|a>>>12&15,128|a>>>6&63,128|a&63):a<=2097151&&(b+=String.fromCharCode(240|a>>>18&7,128|a>>>12&63,128|a>>>6&63,128|a&63));return b};function p(c){var b=Array(c.length>>2);for(var a=0;a<b.length;a++)b[a]=0;for(var a=0;a<c.length*8;a+=8)b[a>>5]|=(c.charCodeAt(a/8)&255)<<a%32;return b};function h(c){var b='';for(var a=0;a<c.length*32;a+=8)b+=String.fromCharCode(c[a>>5]>>>a%32&255);return b};function j(j,l){j[l>>5]|=128<<l%32,j[(l+64>>>9<<4)+14]=l;var g=1732584193;var h=-271733879;var i=-1732584194;var f=271733878;for(var k=0;k<j.length;k+=16){var n=g;var o=h;var p=i;var m=f;g=a(g,h,i,f,j[k+0],7,-680876936),f=a(f,g,h,i,j[k+1],12,-389564586),i=a(i,f,g,h,j[k+2],17,606105819),h=a(h,i,f,g,j[k+3],22,-1044525330),g=a(g,h,i,f,j[k+4],7,-176418897),f=a(f,g,h,i,j[k+5],12,1200080426),i=a(i,f,g,h,j[k+6],17,-1473231341),h=a(h,i,f,g,j[k+7],22,-45705983),g=a(g,h,i,f,j[k+8],7,1770035416),f=a(f,g,h,i,j[k+9],12,-1958414417),i=a(i,f,g,h,j[k+10],17,-42063),h=a(h,i,f,g,j[k+11],22,-1990404162),g=a(g,h,i,f,j[k+12],7,1804603682),f=a(f,g,h,i,j[k+13],12,-40341101),i=a(i,f,g,h,j[k+14],17,-1502002290),h=a(h,i,f,g,j[k+15],22,1236535329),g=b(g,h,i,f,j[k+1],5,-165796510),f=b(f,g,h,i,j[k+6],9,-1069501632),i=b(i,f,g,h,j[k+11],14,643717713),h=b(h,i,f,g,j[k+0],20,-373897302),g=b(g,h,i,f,j[k+5],5,-701558691),f=b(f,g,h,i,j[k+10],9,38016083),i=b(i,f,g,h,j[k+15],14,-660478335),h=b(h,i,f,g,j[k+4],20,-405537848),g=b(g,h,i,f,j[k+9],5,568446438),f=b(f,g,h,i,j[k+14],9,-1019803690),i=b(i,f,g,h,j[k+3],14,-187363961),h=b(h,i,f,g,j[k+8],20,1163531501),g=b(g,h,i,f,j[k+13],5,-1444681467),f=b(f,g,h,i,j[k+2],9,-51403784),i=b(i,f,g,h,j[k+7],14,1735328473),h=b(h,i,f,g,j[k+12],20,-1926607734),g=c(g,h,i,f,j[k+5],4,-378558),f=c(f,g,h,i,j[k+8],11,-2022574463),i=c(i,f,g,h,j[k+11],16,1839030562),h=c(h,i,f,g,j[k+14],23,-35309556),g=c(g,h,i,f,j[k+1],4,-1530992060),f=c(f,g,h,i,j[k+4],11,1272893353),i=c(i,f,g,h,j[k+7],16,-155497632),h=c(h,i,f,g,j[k+10],23,-1094730640),g=c(g,h,i,f,j[k+13],4,681279174),f=c(f,g,h,i,j[k+0],11,-358537222),i=c(i,f,g,h,j[k+3],16,-722521979),h=c(h,i,f,g,j[k+6],23,76029189),g=c(g,h,i,f,j[k+9],4,-640364487),f=c(f,g,h,i,j[k+12],11,-421815835),i=c(i,f,g,h,j[k+15],16,530742520),h=c(h,i,f,g,j[k+2],23,-995338651),g=d(g,h,i,f,j[k+0],6,-198630844),f=d(f,g,h,i,j[k+7],10,1126891415),i=d(i,f,g,h,j[k+14],15,-1416354905),h=d(h,i,f,g,j[k+5],21,-57434055),g=d(g,h,i,f,j[k+12],6,1700485571),f=d(f,g,h,i,j[k+3],10,-1894986606),i=d(i,f,g,h,j[k+10],15,-1051523),h=d(h,i,f,g,j[k+1],21,-2054922799),g=d(g,h,i,f,j[k+8],6,1873313359),f=d(f,g,h,i,j[k+15],10,-30611744),i=d(i,f,g,h,j[k+6],15,-1560198380),h=d(h,i,f,g,j[k+13],21,1309151649),g=d(g,h,i,f,j[k+4],6,-145523070),f=d(f,g,h,i,j[k+11],10,-1120210379),i=d(i,f,g,h,j[k+2],15,718787259),h=d(h,i,f,g,j[k+9],21,-343485551),g=e(g,n),h=e(h,o),i=e(i,p),f=e(f,m)};return Array(g,h,i,f)};function f(a,b,c,d,f,g){return e(i(e(e(b,a),e(d,g)),f),c)};function a(b,a,c,d,e,g,h){return f(a&c|~a&d,b,a,e,g,h)};function b(c,a,d,b,e,g,h){return f(a&b|d&~b,c,a,e,g,h)};function c(b,a,c,d,e,g,h){return f(a^c^d,b,a,e,g,h)};function d(b,a,c,d,e,g,h){return f(c^(a|~d),b,a,e,g,h)};function e(b,c){var a=(b&65535)+(c&65535);var d=(b>>16)+(c>>16)+(a>>16);return d<<16|a&65535};function i(a,b){return a<<b|a>>>32-b};return g=0,k='',l}());
  19.  
  20. var user_id=window.PageData.user.id||window.PageData.user.user_id;//,
  21. //chatting_list_data='';
  22.  
  23. var _client_id='wappc_1398857293796_956',
  24. _client_type='2',
  25. _client_version='4.2.7',
  26. _phone_imei='092887406663530',
  27. net_type='3',
  28. sign_key='tiebaclient!!!';
  29.  
  30. /*if(document.cookie.match(/BDUSS=(.{192,}?);?/))var BDUSS=document.cookie.match(/BDUSS=(.{192,}?);?/)[1];
  31. else if(GM_getValue('BDUSS',0)!=0)var BDUSS=GM_getValue('BDUSS');
  32. else{
  33. var BDUSS=prompt('无法获取 BDUSS,请尝试在移动版贴吧注销并登录。\n\n1. 将浏览器 UA 设置为移动设备的 UA(例如 Chrome 可在开发者工具中点击右上角的“Show drawer.”,并在下方的 Emulation 里设置 UserAgent;Firefox 可安装 User Agent Overrider 等扩展);\n2. 访问 http://tieba.baidu.com/mo 进行登录操作;\n3. 登录后禁用 UA 设置,回到贴吧页检查是否可用。\n\n如果希望手动配置 BDUSS,请将 BDUSS 贴在下方并按确定键。');
  34. if(BDUSS!=null&&BDUSS!=''&&BDUSS.match(/.{192,}?/)){
  35. //document.cookie='BDUSS='+BDUSS.match(/.{192,}?/)+'; expires='+(new Date(new Date().getTime()+60*60*24*365*10*1000)).toGMTString()+'; path=/';
  36. GM_setValue('BDUSS',BDUSS);
  37. var BDUSS=BDUSS;
  38. window.location.href=window.location.href;
  39. }
  40. throw 'Tieba Chat can\'t get BDUSS!!!';
  41. }*/
  42.  
  43. var tb_chat_data=JSON.parse(window.localStorage.getItem('tb_chat_data')/*||window.localStorage.getItem('tb_chat_data')*/)||{};
  44. var message_update_timestamp=window.localStorage.getItem('tb_chat_message_update_timestamp')||new Date().getTime();
  45. var message_update_count=window.localStorage.getItem('tb_chat_message_update_count')||0;
  46. var user_portrait='';
  47. var com_portrait='';
  48. var chatting_list_page=1;
  49. var this_last_msg=0;
  50. var this_early_msg=0;
  51. var this_username=null;
  52. var this_userid=0;
  53. var this_msg_update=null;
  54.  
  55. if(tb_chat_data.first_install==null){
  56. window.$.dialog.alert('<div style="font-size:14px"><p>欢迎使用 Tieba Chat ~~~ ヽ(//∇//)ノ♡</p><p>这是一个实现在网页端使用贴吧客户端的聊天功能的脚本。通过该脚本,您可以方便地与使用贴吧客户端的好友聊天。</p><p>使用说明:<br>1. 将鼠标划至网页右下角的“贴吧聊天”即可打开聊天界面;<br>2. 点击聊天列表中的用户或贴子楼层右下角“聊天”可以发起聊天,点击“点击加载下/上一页”可以进行翻页操作;<br>3. 在聊天界面下方文本框中输入聊天文字,点击右侧“发送”即可发送消息;<br>4. 在聊天界面点击信息列表最上方的“点击查看更多”即可查看之前的消息(如果存在的话);<br>5. 鼠标移出聊天界面后 0.5 秒可收起聊天界面。</p><p style="color:red">警告:<br>* 该脚本目前仍处于测试阶段,可能存在一些未知错误,请谨慎使用<br><del>* 该脚本需要获取 BDUSS,但并不会将其共享,如果您不信任,请卸载此脚本</del> 已确认不需要 BDUSS<br>* 该脚本目前只能发送和接收文字信息,对于图片、表情、语音等多媒体信息暂不支持(主要是 mini 客户端 api 没有提供)<br>* 本脚本目前只是为实现该功能而编写,暂未考虑效率问题,这可能导致浏览器运行缓慢<br>* 本脚本无缓存功能,所有操作均需要重新访问 api,这可能带来较多的带宽消耗<br>* 此脚本的设计可能比较简陋,暂时还没有设置界面……毕竟只是为了测试而已 OTL</p><p>如有疑问请在 firefox 吧相关贴子讨论……</p></div>',{title:"Tieba Chat 欢迎界面",width:800})
  57. tb_chat_data.first_install=1;
  58. window.localStorage.setItem('tb_chat_data',JSON.stringify(tb_chat_data));
  59. }
  60.  
  61. function get_string(c){
  62. //str='BDUSS='+BDUSS+'&_client_id=wappc_1398857293796_956&_client_type=2&_client_version=4.2.7&_phone_imei=092887406663530&from=tieba&net_type=3';
  63. var str='';
  64. var str2='';
  65. for(var i in c){
  66. str+=i+'='+c[i];
  67. str2+=i+'='+encodeURIComponent(c[i])+'&';
  68. }
  69. var sign=hex_md5(str+sign_key);
  70. str2+='sign='+sign;
  71. return str2;
  72. }
  73.  
  74. function get_message_update(){
  75. var xhr = new XMLHttpRequest();
  76. xhr.onreadystatechange=function(){
  77. if(xhr.readyState==4&&xhr.status==200){
  78. var t=JSON.parse(xhr.responseText);
  79. if(t.error_code=='0'){
  80. if(t.error&&t.error.errno!='0'){
  81. //get_error_command(t.error.errno,t.error.usermsg)
  82. //console.error('Tieba Chat Error '+t.error_code+': '+t.error_msg);
  83. var s=show_error(t.error.errno,t.error.errmsg);
  84. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  85. s.onclick=function(){get_message_update();s.parentElement.removeChild(s);clearTimeout(t);};
  86. throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg;
  87. }
  88. /*if(t.message.pletter!='0'){
  89. panel_top.style.color='orange';
  90. //get_chatting_list();
  91. }*/
  92. message_update_timestamp=new Date().getTime();
  93. message_update_count=t.message.pletter;
  94. //if(message_update_count!=window.localStorage.getItem('tb_chat_message_update_count')){
  95. if(message_update_count!='0'){
  96. document.title='【'+message_update_count+' 条新聊天】'+original_title;
  97. panel_top.style.color='orange';
  98. }
  99. else{
  100. document.title=original_title;
  101. panel_top.style.color='#000';
  102. }
  103. //}
  104. window.localStorage.setItem('tb_chat_message_update_timestamp',message_update_timestamp);
  105. window.localStorage.setItem('tb_chat_message_update_count',t.message.pletter);
  106. }
  107. else{
  108. var s=show_error(t.error_code,t.error_msg);
  109. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  110. s.onclick=function(){get_message_update();s.parentElement.removeChild(s);clearTimeout(t);};
  111. throw 'Tieba Chat Error '+t.error_code+': '+t.error_msg;
  112. }
  113. }
  114. }
  115. xhr.open('POST','//tieba.baidu.com/c/s/msg?'+get_string());
  116. xhr.send();
  117. }
  118.  
  119. function get_chatting_list(pn){
  120. var xhr=new XMLHttpRequest();
  121. xhr.onreadystatechange=function(){
  122. if(xhr.readyState==4&&xhr.status==200){
  123. var t=JSON.parse(xhr.responseText);
  124. if(t.error_code=='0'){
  125. if(t.error&&t.error.errno!='0'){
  126. //get_error_command(t.error.errno,t.error.usermsg)
  127. //console.error('Tieba Chat Error '+t.error_code+': '+t.error_msg);
  128. var s=show_error(t.error.errno,t.error.errmsg);
  129. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  130. s.onclick=function(){get_chatting_list(pn);s.parentElement.removeChild(s);clearTimeout(t);};
  131. throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg;
  132. }
  133. panel_head.textContent='贴吧聊天';
  134. panel_main.innerHTML='';
  135. var panel_list=document.createElement('div');
  136. panel_list.className='tb_chat_panel_list';
  137. panel_main.appendChild(panel_list);
  138. chatting_list_page=pn;
  139. if(pn>1){
  140. var msg_more=document.createElement('div');
  141. msg_more.textContent='点击加载上一页';
  142. msg_more.style.cssText='width:200px;margin:20px auto;text-align:center;font-size:13px;background:#0CF;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#FFF';
  143. panel_list.appendChild(msg_more);
  144. msg_more.onclick=function(){get_chatting_list(pn-1);msg_more.textContent='正在载入......';};
  145. }
  146. //chatting_list_data='';
  147. for(var c in t.record){
  148. var p=document.createElement('div');
  149. p.className='tb_chat_row';
  150. p.setAttribute('user_id',t.record[c].user_id);
  151. p.setAttribute('user_name',t.record[c].user_name)
  152. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+t.record[c].portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_username">'+t.record[c].user_name+'</div><div class="tb_chat_lasttext">'+(t.record[c].abstract.length>0?t.record[c].abstract[t.record[c].abstract.length-1].text:'')+'</div></div><div style="clear:both"></div>';
  153. if(t.record[c].unread_count!='0'){
  154. p.style.color='orange';
  155. p.onclick=function(){panel_top.style.color='#000';this_userid=this.getAttribute('user_id');this_username=this.getAttribute('user_name');get_latest_msg(this.getAttribute('user_id'));}
  156. }
  157. else p.onclick=function(){this_userid=this.getAttribute('user_id');this_username=this.getAttribute('user_name');get_latest_msg(this.getAttribute('user_id'));};
  158. panel_list.appendChild(p);
  159. //chatting_list_data+='<div class="tb_chat_row"'+(t.record[c].unread_count!='0'?' style="background:rgba(255,0,0,.1)"':'')+' user_id="'+t.record[c].user_id+'"><div class="tb_chat_pleft"><img class="tb_chat_avatat" src="http://tb.himg.baidu.com/sys/portrait/item/'+t.record[c].portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_username">'+t.record[c].user_name+'</div><div class="tb_chat_lasttext">'+(t.record[c].abstract.length>0?t.record[c].abstract[t.record[c].abstract.length-1].text:'')+'</div></div></div>';
  160. }
  161. if(t.has_more==1){
  162. var msg_more=document.createElement('div');
  163. msg_more.textContent='点击加载下一页';
  164. msg_more.style.cssText='width:200px;margin:20px auto;text-align:center;font-size:13px;background:#0CF;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#FFF';
  165. panel_list.appendChild(msg_more);
  166. msg_more.onclick=function(){get_chatting_list(pn+1);msg_more.textContent='正在载入......';};
  167. }
  168. //if(has_more=='1')chatting_list_data+='<div class="tb_chat_next">点击加载下一页</div>';
  169. //panel.innerHTML=chatting_list_data;
  170. /*var row_node=document.querySelectorAll('.tb_chat_row');
  171. for(var c in row_node){
  172. row_node[c].onclick=function(){get_latest_msg(row_node[c].getAttribute('user_id'));}
  173. }*/
  174. if(panel_top_back.hasAttribute('show'))panel_top_back.removeAttribute('show');
  175. this_last_msg=0;
  176. this_early_msg=0;
  177. this_username=null;
  178. this_userid=0;
  179. }
  180. else{
  181. var s=show_error(t.error_code,t.error_msg);
  182. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  183. s.onclick=function(){get_chatting_list(pn);s.parentElement.removeChild(s);clearTimeout(t);};
  184.  
  185. throw 'Tieba Chat Error '+t.error_code+': '+t.error_msg;
  186. }
  187. }
  188. }
  189. xhr.open('POST','//tieba.baidu.com/c/s/comlist?'+get_string({
  190. //BDUSS:BDUSS,
  191. _client_id:_client_id,
  192. _client_type:_client_type,
  193. _client_version:_client_version,
  194. _phone_imei:_phone_imei,
  195. net_type:net_type,
  196. pn:pn
  197. }));
  198. xhr.send();
  199. }
  200.  
  201. function get_latest_msg(id){
  202. var xhr=new XMLHttpRequest();
  203. xhr.onreadystatechange=function(){
  204. if(xhr.readyState==4&&xhr.status==200){
  205. var t=JSON.parse(xhr.responseText);
  206. if(t.error_code=='0'){
  207. if(t.error&&t.error.errno!='0'){
  208. //get_error_command(t.error.errno,t.error.usermsg)
  209. //console.error('Tieba Chat Error '+t.error_code+': '+t.error_msg);
  210. var s=show_error(t.error.errno,t.error.errmsg);
  211. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  212. s.onclick=function(){get_latest_msg(id);s.parentElement.removeChild(s);clearTimeout(t);};
  213. throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg;
  214. }
  215. //panel_main.innerHTML=xhr.responseText;
  216. /*if(this_username==null)*/
  217. if(id!=this_userid||document.getElementsByClassName('tb_chat_panel_list')[0])panel_main.innerHTML='';
  218. panel_head.textContent=this_username;
  219. var panel_msg=document.createElement('div');
  220. panel_msg.className='tb_chat_panel_msg';
  221. panel_main.appendChild(panel_msg);
  222. com_portrait=t.com_portrait;
  223. user_portrait=t.user_portrait;
  224. if(t.has_more==1){
  225. var msg_more=document.createElement('div');
  226. msg_more.textContent='点击加载更多';
  227. msg_more.style.cssText='width:200px;margin:20px auto;text-align:center;font-size:13px;background:#0CF;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#FFF';
  228. panel_msg.appendChild(msg_more);
  229. msg_more.onclick=function(){get_early_msg(id,panel_msg,msg_more);};
  230. }
  231. for(var c in t.message){
  232. var p=document.createElement('div');
  233. p.className='tb_chat_message';
  234. if(t.message[c].from=='0'){
  235. p.className+=' com';
  236. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+com_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  237. }
  238. else{
  239. p.className+=' user';
  240. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+user_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  241. }
  242. p.setAttribute('msg_id',t.message[c].msg_id);
  243. p.title='Post @ '+new Date(t.message[c].time*1000);
  244. //p.onclick=function(){get_latest_msg(this.getAttribute('user_id'));}
  245. panel_msg.appendChild(p);
  246. if(t.message[c].msg_id<this_early_msg||this_early_msg==0)this_early_msg=t.message[c].msg_id;
  247. if(t.message[c].msg_id>this_last_msg||this_last_msg==0)this_last_msg=t.message[c].msg_id;
  248. //chatting_list_data+='<div class="tb_chat_row"'+(t.record[c].unread_count!='0'?' style="background:rgba(255,0,0,.1)"':'')+' user_id="'+t.record[c].user_id+'"><div class="tb_chat_pleft"><img class="tb_chat_avatat" src="http://tb.himg.baidu.com/sys/portrait/item/'+t.record[c].portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_username">'+t.record[c].user_name+'</div><div class="tb_chat_lasttext">'+(t.record[c].abstract.length>0?t.record[c].abstract[t.record[c].abstract.length-1].text:'')+'</div></div></div>';
  249. }
  250. //document.querySelector('.tb_chat_panel_msg .tb_chat_message:nth-last-child(1)').scrollIntoView();
  251. panel_msg.scrollTop=panel_msg.scrollHeight;
  252. panel_top_back.setAttribute('show','1');
  253. //tb_chat_data.com[t.record[c].user_id].last_msg=this_last_msg;
  254. //window.localStorage.setItem('tb_chat_data',JSON.stringify(tb_chat_data));
  255. if(!document.getElementsByClassName('tb_chat_msg_panel')[0]){
  256. var msg_panel=document.createElement('div');
  257. var msg_textarea=document.createElement('textarea');
  258. var msg_send=document.createElement('div');
  259. msg_panel.style.cssText='width:100%;height:50px;position:absolute;bottom:0;left:0';
  260. msg_panel.className='tb_chat_msg_panel';
  261. panel_main.appendChild(msg_panel);
  262. msg_textarea.style.cssText='width:320px;height:36px;font-size:12px;margin:7px;position:absolute;left:0;top:0;resize:none';
  263. msg_textarea.className='tb_chat_msg_textarea';
  264. msg_panel.appendChild(msg_textarea);
  265. msg_send.style.cssText='width:50px;height:25px;font-size:14px;line-height:25px;text-align:center;position:absolute;margin:15px 7px;right:0;top:0;background:#0CF;cursor:pointer;color:#FFF';
  266. msg_send.textContent='发送';
  267. msg_panel.appendChild(msg_send);
  268. msg_send.onclick=function(){add_message(id,this_last_msg,msg_textarea.value);};
  269. window.onfocus=function(){if(this_msg_update==null)this_msg_update=setInterval(function(){if(document.getElementsByClassName('tb_chat_msg_panel')[0])get_latest_msg(id)},10000);};
  270. window.onblur=function(){if(this_msg_update!=null){clearInterval(this_msg_update);this_msg_update=null;}};
  271. }
  272. get_message_update();
  273. }
  274. else{
  275. var s=show_error(t.error_code,t.error_msg);
  276. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  277. s.onclick=function(){get_latest_msg(id);s.parentElement.removeChild(s);clearTimeout(t);};
  278. throw 'Tieba Chat Error '+t.error_code+': '+t.error_msg;
  279. }
  280. }
  281. }
  282. xhr.open('POST','//tieba.baidu.com/c/s/recentmsg?'+get_string({
  283. //BDUSS:BDUSS,
  284. _client_id:_client_id,
  285. _client_type:_client_type,
  286. _client_version:_client_version,
  287. _phone_imei:_phone_imei,
  288. com_id:id,
  289. msg_id:this_last_msg,
  290. net_type:net_type,
  291. user_id:user_id
  292. }
  293. /*'msg_id=0&user_id='+user_id+'&com_id='+id*/));
  294. xhr.send();
  295. }
  296.  
  297. function get_early_msg(id,panel_msg,msg_more){
  298. msg_more.textContent='正在载入......';
  299. var scroll_top=panel_msg.scrollHeight-panel_msg.scrollTop;
  300. var xhr=new XMLHttpRequest();
  301. xhr.onreadystatechange=function(){
  302. if(xhr.readyState==4&&xhr.status==200){
  303. var t=JSON.parse(xhr.responseText);
  304. if(t.error_code=='0'){
  305. if(t.error&&t.error.errno!='0'){
  306. var s=show_error(t.error.errno,t.error.errmsg);
  307. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  308. s.onclick=function(){get_early_msg(id,panel_msg,msg_more);s.parentElement.removeChild(s);clearTimeout(t);};
  309. throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg;
  310. }
  311. var xxx=document.createElement('div');
  312. panel_msg.insertBefore(xxx,msg_more.nextElementSibling);
  313. for(var c in t.message){
  314. var p=document.createElement('div');
  315. p.className='tb_chat_message';
  316. if(t.message[c].from=='0'){
  317. p.className+=' com';
  318. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+com_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  319. }
  320. else{
  321. p.className+=' user';
  322. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+user_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  323. }
  324. p.setAttribute('msg_id',t.message[c].msg_id);
  325. p.title='Post @ '+new Date(t.message[c].time*1000);
  326. //p.onclick=function(){get_latest_msg(this.getAttribute('user_id'));}
  327. //panel_msg.appendChild(p);
  328. if(t.message[c].msg_id<this_early_msg||this_early_msg==0)this_early_msg=t.message[c].msg_id;
  329. if(t.message[c].msg_id>this_last_msg||this_last_msg==0)this_last_msg=t.message[c].msg_id;
  330. //panel_msg.insertBefore(p,msg_more.nextElement);
  331. xxx.appendChild(p);
  332. }
  333. if(t.has_more==1)msg_more.textContent='点击加载更多';
  334. else msg_more.parentElement.removeChild(msg_more);
  335. panel_msg.scrollTop=panel_msg.scrollHeight-scroll_top;
  336. }
  337. else{
  338. var s=show_error(t.error_code,t.error_msg);
  339. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  340. s.onclick=function(){get_early_msg(id,panel_msg,msg_more);s.parentElement.removeChild(s);clearTimeout(t);};
  341. throw 'Tieba Chat Error '+t.error_code+': '+t.error_msg;
  342. }
  343. }
  344. }
  345. xhr.open('POST','//tieba.baidu.com/c/s/historymsg?'+get_string({
  346. //BDUSS:BDUSS,
  347. _client_id:_client_id,
  348. _client_type:_client_type,
  349. _client_version:_client_version,
  350. _phone_imei:_phone_imei,
  351. com_id:id,
  352. msg_id:this_early_msg,
  353. net_type:net_type,
  354. user_id:user_id
  355. }
  356. /*'msg_id=0&user_id='+user_id+'&com_id='+id*/));
  357. xhr.send();
  358. }
  359.  
  360. function add_message(id,last_msg_id,content){
  361. var xhr=new XMLHttpRequest();
  362. xhr.onreadystatechange=function(){
  363. if(xhr.readyState==4&&xhr.status==200){
  364. var t=JSON.parse(xhr.responseText);
  365. if(t.error_code=='0'){
  366. if(t.error&&t.error.errno!='0'){
  367. //get_error_command(t.error.errno,t.error.usermsg)
  368. //console.error('Tieba Chat Error '+t.error_code+': '+t.error_msg);
  369. var s=show_error(t.error.errno,t.error.errmsg);
  370. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  371. s.onclick=function(){add_message(id,last_msg_id,content);s.parentElement.removeChild(s);clearTimeout(t);};
  372. throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg;
  373. }
  374. var this_last_msg;
  375. var panel_msg=document.getElementsByClassName('tb_chat_panel_msg')[0];
  376. //var panel_msg=document.createElement('div');
  377. //panel_msg.className='tb_chat_panel_msg';
  378. //panel_main.appendChild(panel_msg);
  379. for(var c in t.recent.message){
  380. var p=document.createElement('div');
  381. p.className='tb_chat_message';
  382. if(t.recent.message[c].from=='0'){
  383. p.className+=' com';
  384. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+com_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.recent.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  385. }
  386. else{
  387. p.className+=' user';
  388. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+user_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.recent.message[c].content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  389. }
  390. p.setAttribute('msg_id',t.recent.message[c].msg_id);
  391. p.title='Post @ '+new Date(t.recent.message[c].time*1000);
  392. panel_msg.appendChild(p);
  393. //panel_top_back.setAttribute('show','1');
  394. if(t.recent.message[c].msg_id<this_early_msg||this_early_msg==0)this_early_msg=t.recent.message[c].msg_id;
  395. if(t.recent.message[c].msg_id>this_last_msg||this_last_msg==0)this_last_msg=t.recent.message[c].msg_id;
  396. }
  397. var p=document.createElement('div');
  398. p.className='tb_chat_message';
  399. p.className+=' user';
  400. p.innerHTML='<div class="tb_chat_pleft"><img class="tb_chat_avatar" src="http://tb.himg.baidu.com/sys/portrait/item/'+user_portrait+'" alt></div><div class="tb_chat_pright"><div class="tb_chat_message_inner">'+(t.message.content[0].text||'未知数据')+'</div></div><div style="clear:both"></div>';
  401. p.setAttribute('msg_id',t.message.msg_id);
  402. panel_msg.appendChild(p);
  403. if(t.message.msg_id<this_early_msg||this_early_msg==0)this_early_msg=t.message.msg_id;
  404. if(t.message.msg_id>this_last_msg||this_last_msg==0)this_last_msg=t.message.msg_id;
  405. //document.querySelector('.tb_chat_panel_msg .tb_chat_message:nth-last-child(1)').scrollIntoView();
  406. panel_msg.scrollTop=panel_msg.scrollHeight;
  407. document.getElementsByClassName('tb_chat_msg_textarea')[0].value='';
  408. }
  409. else{
  410. var s=show_error(t.error_code,t.error_msg);
  411. var t=setTimeout(function(){s.parentElement.removeChild(s);},5000);
  412. s.onclick=function(){add_message(id,last_msg_id,content);s.parentElement.removeChild(s);clearTimeout(t);};
  413. throw 'Tieba Chat Error '+t.error_code+': '+t.error_msg;
  414. }
  415. }
  416. }
  417. xhr.open('POST','//tieba.baidu.com/c/s/addmsg?'+get_string({
  418. //BDUSS:BDUSS,
  419. _client_id:_client_id,
  420. _client_type:_client_type,
  421. _client_version:_client_version,
  422. _phone_imei:_phone_imei,
  423. com_id:id,
  424. content:content,
  425. last_msg_id:last_msg_id,
  426. net_type:net_type,
  427. user_id:user_id
  428. }));
  429. xhr.send();
  430. }
  431.  
  432. function show_error(c,t){
  433. var panel_error=document.createElement('div');
  434. panel_error.className='tb_chat_error';
  435. panel.appendChild(panel_error);
  436. panel_error.innerHTML='Tieba Chat Error!!!<br>Error Code: '+c+'<br>Error Message:'+t+'<br>Click this panel to retry....';
  437. return panel_error;
  438. }
  439.  
  440. function add_enterance(){
  441. var s=document.getElementsByClassName('j_jb_ele');
  442. for(var i=0;i<s.length;i++){
  443. if(s[i].hasAttribute('tb_chat_added')==false){
  444. var k=document.createTextNode(' ');
  445. s[i].appendChild(k);
  446. var j=document.createElement('a');
  447. j.style.cssText='color:#999;cursor:pointer';
  448. j.textContent='聊天';
  449. s[i].appendChild(j);
  450. j.onclick=function(){
  451. var ud=JSON.parse(this.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.getAttribute('data-field')).author;
  452. this_userid=ud.user_id;
  453. this_username=ud.user_name;
  454. panel.setAttribute('show','1');
  455. get_latest_msg(ud.user_id);
  456. }
  457. var k=document.createTextNode(' |');
  458. s[i].appendChild(k);
  459. s[i].setAttribute('tb_chat_added','true');
  460. }
  461. }
  462. }
  463.  
  464. var message_update_timer=setInterval(function(){
  465. if((new Date().getTime()-(window.localStorage.getItem('message_update_timestamp')||message_update_timestamp))>10000){
  466. get_message_update();
  467. }
  468. else if(window.localStorage.getItem('tb_chat_message_update_count')!='0'){
  469. document.title='【'+message_update_count+' 条新聊天】'+original_title;
  470. panel_top.style.color='orange';
  471. }
  472. else{
  473. document.title=original_title;
  474. panel_top.style.color='#000';
  475. }
  476. /*if(message_update_count!=window.localStorage.getItem('tb_chat_message_update_count')&&){
  477. if(message_update_count!='0'){
  478. document.title='【'+message_update_count+' 条新聊天】'+original_title;
  479. panel_top.style.color='orange';
  480. }
  481. else{
  482. document.title=original_title;
  483. panel_top.style.color='#000';
  484. }
  485. }*/
  486. //if(PageData.product=='pb')add_enterance();
  487. },1000);
  488.  
  489. // ------------------------------------------------------------------------
  490.  
  491.  
  492. var panel=document.createElement('div');
  493. var panel_top=document.createElement('div');
  494. var panel_head=document.createElement('div');
  495. var panel_main=document.createElement('div');
  496. var panel_top_back=document.createElement('div');
  497. var panel_ifshow=null;
  498. var original_title=document.title;
  499. var ss=document.createElement('style');
  500. var stylesheet='.tb_chat_panel{width:400px;height:100%;position:fixed;top:calc(100% - 30px);right:0px;background:rgba(255,255,255,.75);z-index:200000;transition:all 0.5s ease-in;-webkit-user-select:none;-moz-user-select:none}.tb_chat_panel[show]{top:0px;transition:all 0.5s ease-out}.tb_chat_panel_top{font-size:14px;line-height:30px;text-align:center;width:100%;height:30px;-webkit-user-select:none;-moz-user-select:none}.tb_chat_panel_top_back{position:absolute;left:10px;top:0px;width:30px;height:30px;background:#0CF;visibility:hidden;color:#FFF;cursor:pointer}.tb_chat_panel_top_back[show]{visibility:visible}.tb_chat_panel_main{height:calc(100% - 30px);overflow:auto}.tb_chat_row{/*width:400px;*/height:60px;padding:10px;cursor:pointer}.tb_chat_row:hover{background:rgba(0,0,0,.1)}.tb_chat_pleft{float:left}.tb_chat_row .tb_chat_avatar{width:60px;height:60px}.tb_chat_pright{float:left;max-width:300px}.tb_chat_row .tb_chat_username{font-size:14px;padding:6px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}.tb_chat_row .tb_chat_lasttext{padding:6px;font-size:12px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}.tb_chat_next{font-size:14px;text-align:center;width:100%;height:30px;line-height:30px}.tb_chat_message{font-size:12px;line-height:14px;min-height:28px;padding:5px}.tb_chat_message .tb_chat_avatar{width:28px;height:28px}.tb_chat_message.user .tb_chat_pleft,.tb_chat_message.user .tb_chat_pright{float:right;text-align:right}.tb_chat_message .tb_chat_message_inner{margin:0 10px;padding:6px;background:#FFF;border:1px solid #CCC;border-radius:3px;-moz-user-select:text;-webkit-user-select:text}.tb_chat_panel_msg{margin-bottom:50px;height:calc(100% - 50px);overflow:auto}.tb_chat_error{background:rgba(255,0,0,.25);font-size:12px;line-height:16px;position:fixed;width:350px;margin:0 25px;top:50px;right:0px;box-shadow:rgba(255,0,0,0.25) 0 0 5px;color:#F00}.tb_chat_message_inner::selection,.tb_chat_message_inner::-moz-selection{background:rgba(255,255,255,0.5)}';
  501. panel.className='tb_chat_panel';
  502. document.body.appendChild(panel);
  503. ss.textContent=stylesheet;
  504. panel.appendChild(ss);
  505. //panel_top.textContent='贴吧聊天';
  506. panel_top.className='tb_chat_panel_top';
  507. panel.appendChild(panel_top);
  508. panel_head.textContent='贴吧聊天';
  509. panel_top.appendChild(panel_head);
  510. panel_top_back.className='tb_chat_panel_top_back';
  511. panel_top_back.textContent='←';
  512. panel_top.appendChild(panel_top_back);
  513. panel_main.className='tb_chat_panel_main';
  514. panel.appendChild(panel_main);
  515. panel_top_back.onclick=function(){
  516. get_chatting_list(chatting_list_page);
  517. if(this_msg_update!=null){
  518. clearInterval(this_msg_update);
  519. this_msg_update=null;
  520. window.onfocus=null;
  521. window.onblur=null;
  522. }
  523. };
  524. panel_top.onmouseover=function(){
  525. panel.setAttribute('show','1');
  526. if(this_username==null)get_chatting_list(chatting_list_page)
  527. };
  528. panel.onmouseover=function(){if(panel_ifshow!=null)clearTimeout(panel_ifshow);};
  529. panel.onmouseout=function(){
  530. panel_ifshow=setTimeout(function(){
  531. if(panel.hasAttribute('show'))panel.removeAttribute('show');
  532. panel_ifshow=null;
  533. },500);
  534. };
  535. get_chatting_list(chatting_list_page);
  536. if(PageData.product=='pb')add_enterance();
  537. if(document.getElementsByClassName('tb_preload_notification')[0]){
  538. var tpn=document.getElementsByClassName('tb_preload_notification')[0];
  539. tpn.addEventListener('DOMSubtreeModified',function(){
  540. if(tpn.hasAttribute('show')==false)add_enterance();
  541. });
  542. }