Tieba Chat

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

  1. // ==UserScript==
  2. // @name Tieba Chat
  3. // @version 1.14
  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. // @noframes
  12. // @namespace http://ext.ccloli.com
  13. // ==/UserScript==
  14.  
  15. // 本脚本基于 GPLv3 协议开源 http://www.gnu.org/licenses/gpl.html‎
  16. // (c) 86497600cc. Some Rights Reserved.
  17.  
  18. var hex_md5 = (function(g, k) {
  19. function l(a) {
  20. return n(m(o(a)))
  21. };
  22.  
  23. function m(a) {
  24. return h(j(p(a), a.length * 8))
  25. };
  26.  
  27. function n(e) {
  28. try {} catch (a) {
  29. g = 0
  30. };
  31. var b = g ? '0123456789ABCDEF' : '0123456789abcdef';
  32. var c = '';
  33. var d;
  34. for (var a = 0; a < e.length; a++) d = e.charCodeAt(a), c += b.charAt(d >>> 4 & 15) + b.charAt(d & 15);
  35. return c
  36. };
  37.  
  38. function o(d) {
  39. var b = '';
  40. var c = -1;
  41. var a, e;
  42. 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));
  43. return b
  44. };
  45.  
  46. function p(c) {
  47. var b = Array(c.length >> 2);
  48. for (var a = 0; a < b.length; a++) b[a] = 0;
  49. for (var a = 0; a < c.length * 8; a += 8) b[a >> 5] |= (c.charCodeAt(a / 8) & 255) << a % 32;
  50. return b
  51. };
  52.  
  53. function h(c) {
  54. var b = '';
  55. for (var a = 0; a < c.length * 32; a += 8) b += String.fromCharCode(c[a >> 5] >>> a % 32 & 255);
  56. return b
  57. };
  58.  
  59. function j(j, l) {
  60. j[l >> 5] |= 128 << l % 32, j[(l + 64 >>> 9 << 4) + 14] = l;
  61. var g = 1732584193;
  62. var h = -271733879;
  63. var i = -1732584194;
  64. var f = 271733878;
  65. for (var k = 0; k < j.length; k += 16) {
  66. var n = g;
  67. var o = h;
  68. var p = i;
  69. var m = f;
  70. 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)
  71. };
  72. return Array(g, h, i, f)
  73. };
  74.  
  75. function f(a, b, c, d, f, g) {
  76. return e(i(e(e(b, a), e(d, g)), f), c)
  77. };
  78.  
  79. function a(b, a, c, d, e, g, h) {
  80. return f(a & c | ~a & d, b, a, e, g, h)
  81. };
  82.  
  83. function b(c, a, d, b, e, g, h) {
  84. return f(a & b | d & ~b, c, a, e, g, h)
  85. };
  86.  
  87. function c(b, a, c, d, e, g, h) {
  88. return f(a ^ c ^ d, b, a, e, g, h)
  89. };
  90.  
  91. function d(b, a, c, d, e, g, h) {
  92. return f(c ^ (a | ~d), b, a, e, g, h)
  93. };
  94.  
  95. function e(b, c) {
  96. var a = (b & 65535) + (c & 65535);
  97. var d = (b >> 16) + (c >> 16) + (a >> 16);
  98. return d << 16 | a & 65535
  99. };
  100.  
  101. function i(a, b) {
  102. return a << b | a >>> 32 - b
  103. };
  104. return g = 0, k = '', l
  105. }());
  106.  
  107. var user_id = window.PageData.user.id || window.PageData.user.user_id;
  108.  
  109. var _client_id = 'wappc_1398857293796_956',
  110. _client_type = '2',
  111. _client_version = '4.2.7',
  112. _phone_imei = '092887406663530',
  113. net_type = '3',
  114. sign_key = 'tiebaclient!!!';
  115.  
  116. /*if(document.cookie.match(/BDUSS=(.{192,}?);?/))var BDUSS=document.cookie.match(/BDUSS=(.{192,}?);?/)[1];
  117. else if(GM_getValue('BDUSS',0)!=0)var BDUSS=GM_getValue('BDUSS');
  118. else{
  119. 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 贴在下方并按确定键。');
  120. if(BDUSS!=null&&BDUSS!=''&&BDUSS.match(/.{192,}?/)){
  121. //document.cookie='BDUSS='+BDUSS.match(/.{192,}?/)+'; expires='+(new Date(new Date().getTime()+60*60*24*365*10*1000)).toGMTString()+'; path=/';
  122. GM_setValue('BDUSS',BDUSS);
  123. var BDUSS=BDUSS;
  124. window.location.href=window.location.href;
  125. }
  126. throw 'Tieba Chat can\'t get BDUSS!!!';
  127. }*/
  128.  
  129. var tb_chat_data = JSON.parse(window.localStorage.getItem('tb_chat_data')) || {};
  130. var message_update_timestamp = window.localStorage.getItem('tb_chat_message_update_timestamp') || new Date().getTime();
  131. var message_update_count = window.localStorage.getItem('tb_chat_message_update_count') || 0;
  132. var user_portrait = '';
  133. var com_portrait = '';
  134. var chatting_list_page = 1;
  135. var this_last_msg = 0;
  136. var this_early_msg = 0;
  137. var this_username = null;
  138. var this_userid = 0;
  139. var this_msg_update = null;
  140. var model = 0;
  141.  
  142. var emo_list = {
  143. "呵呵": "http://tb2.bdstatic.com/tb/editor/images/face/i_f01.png",
  144. "哈哈": "http://tb2.bdstatic.com/tb/editor/images/face/i_f02.png",
  145. "吐舌": "http://tb2.bdstatic.com/tb/editor/images/face/i_f03.png",
  146. "啊": "http://tb2.bdstatic.com/tb/editor/images/face/i_f04.png",
  147. "酷": "http://tb2.bdstatic.com/tb/editor/images/face/i_f05.png",
  148. "怒": "http://tb2.bdstatic.com/tb/editor/images/face/i_f06.png",
  149. "开心": "http://tb2.bdstatic.com/tb/editor/images/face/i_f07.png",
  150. "汗": "http://tb2.bdstatic.com/tb/editor/images/face/i_f08.png",
  151. "泪": "http://tb2.bdstatic.com/tb/editor/images/face/i_f09.png",
  152. "黑线": "http://tb2.bdstatic.com/tb/editor/images/face/i_f10.png",
  153. "鄙视": "http://tb2.bdstatic.com/tb/editor/images/face/i_f11.png",
  154. "不高兴": "http://tb2.bdstatic.com/tb/editor/images/face/i_f12.png",
  155. "真棒": "http://tb2.bdstatic.com/tb/editor/images/face/i_f13.png",
  156. "钱": "http://tb2.bdstatic.com/tb/editor/images/face/i_f14.png",
  157. "疑问": "http://tb2.bdstatic.com/tb/editor/images/face/i_f15.png",
  158. "阴险": "http://tb2.bdstatic.com/tb/editor/images/face/i_f16.png",
  159. "吐": "http://tb2.bdstatic.com/tb/editor/images/face/i_f17.png",
  160. "咦": "http://tb2.bdstatic.com/tb/editor/images/face/i_f18.png",
  161. "委屈": "http://tb2.bdstatic.com/tb/editor/images/face/i_f19.png",
  162. "花心": "http://tb2.bdstatic.com/tb/editor/images/face/i_f20.png",
  163. "呼~": "http://tb2.bdstatic.com/tb/editor/images/face/i_f21.png",
  164. "笑眼": "http://tb2.bdstatic.com/tb/editor/images/face/i_f22.png",
  165. "冷": "http://tb2.bdstatic.com/tb/editor/images/face/i_f23.png",
  166. "太开心": "http://tb2.bdstatic.com/tb/editor/images/face/i_f24.png",
  167. "滑稽": "http://tb2.bdstatic.com/tb/editor/images/face/i_f25.png",
  168. "勉强": "http://tb2.bdstatic.com/tb/editor/images/face/i_f26.png",
  169. "狂汗": "http://tb2.bdstatic.com/tb/editor/images/face/i_f27.png",
  170. "乖": "http://tb2.bdstatic.com/tb/editor/images/face/i_f28.png",
  171. "睡觉": "http://tb2.bdstatic.com/tb/editor/images/face/i_f29.png",
  172. "惊哭": "http://tb2.bdstatic.com/tb/editor/images/face/i_f30.png",
  173. "升起": "http://tb2.bdstatic.com/tb/editor/images/face/i_f31.png",
  174. "惊讶": "http://tb2.bdstatic.com/tb/editor/images/face/i_f32.png",
  175. "喷": "http://tb2.bdstatic.com/tb/editor/images/face/i_f33.png",
  176. "爱心": "http://tb2.bdstatic.com/tb/editor/images/face/i_f34.png",
  177. "心碎": "http://tb2.bdstatic.com/tb/editor/images/face/i_f35.png",
  178. "玫瑰": "http://tb2.bdstatic.com/tb/editor/images/face/i_f36.png",
  179. "礼物": "http://tb2.bdstatic.com/tb/editor/images/face/i_f37.png",
  180. "彩虹": "http://tb2.bdstatic.com/tb/editor/images/face/i_f38.png",
  181. "星星月亮": "http://tb2.bdstatic.com/tb/editor/images/face/i_f39.png",
  182. "太阳": "http://tb2.bdstatic.com/tb/editor/images/face/i_f40.png",
  183. "钱币": "http://tb2.bdstatic.com/tb/editor/images/face/i_f41.png",
  184. "灯泡": "http://tb2.bdstatic.com/tb/editor/images/face/i_f42.png",
  185. "茶杯": "http://tb2.bdstatic.com/tb/editor/images/face/i_f43.png",
  186. "蛋糕": "http://tb2.bdstatic.com/tb/editor/images/face/i_f44.png",
  187. "音乐": "http://tb2.bdstatic.com/tb/editor/images/face/i_f45.png",
  188. "haha": "http://tb2.bdstatic.com/tb/editor/images/face/i_f46.png",
  189. "胜利": "http://tb2.bdstatic.com/tb/editor/images/face/i_f47.png",
  190. "大拇指": "http://tb2.bdstatic.com/tb/editor/images/face/i_f48.png",
  191. "弱": "http://tb2.bdstatic.com/tb/editor/images/face/i_f49.png",
  192. "OK": "http://tb2.bdstatic.com/tb/editor/images/face/i_f50.png",
  193. "哭着跑": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_047.gif",
  194. "惆怅~": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_050.gif",
  195. "摸头": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_051.gif",
  196. "蹭": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_052.gif",
  197. "打滚": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_053.gif",
  198. "叩拜": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_054.gif",
  199. "摸": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_055.gif",
  200. "数钱": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_056.gif",
  201. "加1": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_059.gif",
  202. "压力": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_060.gif",
  203. "表逼我": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_061.gif",
  204. "人呢": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_062.gif",
  205. "摇晃": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_063.gif",
  206. "打地鼠": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_064.gif",
  207. "这个屌": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_065.gif",
  208. "恐慌": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_066.gif",
  209. "晕乎乎": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_067.gif",
  210. "浮云": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_068.gif",
  211. "给力": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_069.gif",
  212. "杯具了": "http://tb2.bdstatic.com/tb/editor/images/ali/ali_070.gif",
  213. "焦糖舞": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_001.gif",
  214. "翻滚": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_002.gif",
  215. "拍屁股做鬼脸": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_003.gif",
  216. "不": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_004.gif",
  217. "河蟹掉啦": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_005.gif",
  218. "哦耶": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_006.gif",
  219. "我倒": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_007.gif",
  220. "投降": "http://tb2.bdstatic.com/tb/editor/images/shadow/yz_008.gif",
  221. "微笑": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b01.gif",
  222. "帅哥": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b02.gif",
  223. "美女": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b03.gif",
  224. "老大": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b04.gif",
  225. "哈哈哈": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b05.gif",
  226. "奸笑": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b06.gif",
  227. "傻乐": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b07.gif",
  228. "飞吻": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b08.gif",
  229. "害羞": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b09.gif",
  230. "花痴": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b10.gif",
  231. "憧憬": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b11.gif",
  232. "你牛": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b12.gif",
  233. "鼓掌": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b13.gif",
  234. "可爱": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b14.gif",
  235. "太委屈": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b15.gif",
  236. "大哭": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b16.gif",
  237. "泪奔": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b17.gif",
  238. "寻死": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b18.gif",
  239. "非常惊讶": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b19.gif",
  240. "表示疑问": "http://tb2.bdstatic.com/tb/editor/images/qpx_n/b20.gif",
  241. "蹦蹦跳跳": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_001.gif",
  242. "晃悠": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_002.gif",
  243. "摇摇摆摆": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_003.gif",
  244. "大撒花": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_004.gif",
  245. "高高兴兴": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_005.gif",
  246. "弹吉他": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_006.gif",
  247. "魔法棒": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_007.gif",
  248. "扛大旗": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_008.gif",
  249. "点蜡烛": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_009.gif",
  250. "大秧歌": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_010.gif",
  251. "贴吧十周年": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_011.gif",
  252. "十周年": "http://tb2.bdstatic.com/tb/editor/images/10th/10th_012.gif"
  253. };
  254. var emo_regex = /#\((呵呵|哈哈|吐舌|啊|酷|怒|开心|汗|泪|黑线|鄙视|不高兴|真棒|钱|疑问|阴险|吐|咦|委屈|花心|呼~|笑眼|冷|太开心|滑稽|勉强|狂汗|乖|睡觉|惊哭|升起|惊讶|喷|爱心|心碎|玫瑰|礼物|彩虹|星星月亮|太阳|钱币|灯泡|茶杯|蛋糕|音乐|haha|胜利|大拇指|弱|OK|哭着跑|惆怅~|摸头|蹭|打滚|叩拜|摸|数钱|加1|压力|表逼我|人呢|摇晃|打地鼠|这个屌|恐慌|晕乎乎|浮云|给力|杯具了|焦糖舞|翻滚|拍屁股做鬼脸|不|河蟹掉啦|哦耶|我倒|投降|微笑|帅哥|美女|老大|哈哈哈|奸笑|傻乐|飞吻|害羞|花痴|憧憬|你牛|鼓掌|可爱|太委屈|大哭|泪奔|寻死|非常惊讶|表示疑问|蹦蹦跳跳|晃悠|摇摇摆摆|大撒花|高高兴兴|弹吉他|魔法棒|扛大旗|点蜡烛|大秧歌|贴吧十周年|十周年)\)/gi;
  255.  
  256. if (tb_chat_data.first_install == null) {
  257. 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>', {
  258. title: "Tieba Chat 欢迎界面",
  259. width: 800
  260. })
  261. tb_chat_data.first_install = 1;
  262. window.localStorage.setItem('tb_chat_data', JSON.stringify(tb_chat_data));
  263. }
  264.  
  265. function get_string(c) {
  266. var str = '';
  267. var str2 = '';
  268. for (var i in c) {
  269. str += i + '=' + c[i];
  270. str2 += i + '=' + encodeURIComponent(c[i]) + '&';
  271. }
  272. var sign = hex_md5(str + sign_key);
  273. str2 += 'sign=' + sign;
  274. return str2;
  275. }
  276.  
  277. function get_locale_time(t) {
  278. var time = new Date(t * 1000);
  279. return (time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds());
  280. }
  281.  
  282. function get_message_update() {
  283. var xhr = new XMLHttpRequest();
  284. xhr.onreadystatechange = function() {
  285. if (xhr.readyState == 4 && xhr.status == 200) {
  286. var t = JSON.parse(xhr.responseText);
  287. if (t.error_code == '0') {
  288. if (t.error && t.error.errno != '0') {
  289. var s = show_error(t.error.errno, t.error.errmsg);
  290. var t = setTimeout(function() {
  291. s.parentElement.removeChild(s);
  292. }, 5000);
  293. s.onclick = function() {
  294. get_message_update();
  295. s.parentElement.removeChild(s);
  296. clearTimeout(t);
  297. };
  298. throw 'Tieba Chat Error ' + t.error.errno + ': ' + t.error.errmsg;
  299. }
  300. message_update_timestamp = new Date().getTime();
  301. message_update_count = t.message.pletter;
  302. if (message_update_count != '0') {
  303. document.title = '【' + message_update_count + ' 条新聊天】' + original_title;
  304. panel_top.style.color = 'orange';
  305. } else {
  306. document.title = original_title;
  307. panel_top.style.color = '#414243';
  308. }
  309. window.localStorage.setItem('tb_chat_message_update_timestamp', message_update_timestamp);
  310. window.localStorage.setItem('tb_chat_message_update_count', t.message.pletter);
  311. } else {
  312. var s = show_error(t.error_code, t.error_msg);
  313. var t = setTimeout(function() {
  314. s.parentElement.removeChild(s);
  315. }, 5000);
  316. s.onclick = function() {
  317. get_message_update();
  318. s.parentElement.removeChild(s);
  319. clearTimeout(t);
  320. };
  321. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  322. }
  323. }
  324. };
  325. xhr.open('POST', '//tieba.baidu.com/c/s/msg?' + get_string());
  326. xhr.send();
  327. }
  328.  
  329. function get_chatting_list(pn) {
  330. var xhr = new XMLHttpRequest();
  331. xhr.onreadystatechange = function() {
  332. if (xhr.readyState == 4 && xhr.status == 200) {
  333. var t = JSON.parse(xhr.responseText);
  334. if (t.error_code == '0') {
  335. if (t.error && t.error.errno != '0') {
  336. var s = show_error(t.error.errno, t.error.errmsg);
  337. var t = setTimeout(function() {
  338. s.parentElement.removeChild(s);
  339. }, 5000);
  340. s.onclick = function() {
  341. get_chatting_list(pn);
  342. s.parentElement.removeChild(s);
  343. clearTimeout(t);
  344. };
  345. throw 'Tieba Chat Error ' + t.error.errno + ': ' + t.error.errmsg;
  346. }
  347. switch (model) {
  348. case 0:
  349. panel_head.textContent = '贴吧聊天';
  350. break;
  351. case 1:
  352. panel_head.textContent = '贴吧聊天(标记阅读模式)';
  353. break;
  354. case 2:
  355. panel_head.textContent = '贴吧聊天(删除用户模式)';
  356. break;
  357. }
  358. panel.setAttribute('model', model);
  359. panel_main.innerHTML = '';
  360. var panel_list = document.createElement('div');
  361. panel_list.className = 'tb_chat_panel_list';
  362. panel_main.appendChild(panel_list);
  363. chatting_list_page = pn;
  364. if (pn > 1) {
  365. var msg_more = document.createElement('div');
  366. msg_more.textContent = '点击加载上一页';
  367. msg_more.style.cssText = 'width:200px;margin:20px auto;text-align:center;font-size:13px;background:#5b97ff;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#ffffff';
  368. panel_list.appendChild(msg_more);
  369. msg_more.onclick = function() {
  370. get_chatting_list(pn - 1);
  371. msg_more.textContent = '正在载入......';
  372. };
  373. }
  374. for (var c in t.record) {
  375. var p = document.createElement('div');
  376. p.className = 'tb_chat_row';
  377. p.setAttribute('user_id', t.record[c].user_id);
  378. p.setAttribute('user_name', t.record[c].user_name)
  379. 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 + (t.record[c].unread_count == '0' ? '' : ' <small>(' + t.record[c].unread_count + ')</small>') + '</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>';
  380. if (t.record[c].unread_count != '0') {
  381. p.style.color = 'orange';
  382. p.onclick = function() {
  383. panel_top.style.color = '#414243';
  384. switch (model) {
  385. case 0:
  386. this_userid = this.getAttribute('user_id');
  387. this_username = this.getAttribute('user_name');
  388. get_latest_msg(this.getAttribute('user_id'));
  389. break;
  390. case 1:
  391. get_latest_msg(this.getAttribute('user_id'), true);
  392. this.style.color = '#414243';
  393. break;
  394. case 2:
  395. delete_user(this.getAttribute('user_id'), true);
  396. break;
  397. }
  398. }
  399. } else p.onclick = function() {
  400. switch (model) {
  401. case 0:
  402. this_userid = this.getAttribute('user_id');
  403. this_username = this.getAttribute('user_name');
  404. get_latest_msg(this.getAttribute('user_id'));
  405. break;
  406. case 1:
  407. get_latest_msg(this.getAttribute('user_id'), true);
  408. break;
  409. case 2:
  410. delete_user(this.getAttribute('user_id'), true);
  411. break;
  412. }
  413. }
  414. panel_list.appendChild(p);
  415. }
  416. if (t.has_more == 1) {
  417. var msg_more = document.createElement('div');
  418. msg_more.textContent = '点击加载下一页';
  419. msg_more.style.cssText = 'width:200px;margin:20px auto;text-align:center;font-size:13px;background:#5b97ff;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#ffffff';
  420. panel_list.appendChild(msg_more);
  421. msg_more.onclick = function() {
  422. get_chatting_list(pn + 1);
  423. msg_more.textContent = '正在载入......';
  424. };
  425. }
  426. panel_top_left.textContent = '+';
  427. panel_top_right.textContent = '≡';
  428. panel_top_left.onclick = function() {
  429. var username = prompt('请输入用户名以发起聊天');
  430. if (username != null && username != '') get_userid(username);
  431. }
  432. panel_top_right.onclick = function() {
  433. switch (prompt('请键入以下数字并点击“确定”以完成对应操作:\n1. 进入标记已读模式\n2. 进入删除用户模式\n3. 回到聊天模式\n\n(比较简陋真是抱歉了 _(:з」∠)_ )')) {
  434. case '1':
  435. alert('现在你可以通过点击聊天面板上的用户来标记已读,标记已读的用户将不会再提醒以前的消息\n警告:该操作仅是方便标记已查阅的消息记录,请注意辨别未查阅的消息,标记过程将不会出现提醒,且操作不可逆\n如果需要回到聊天模式,请点击右上角的三道杠');
  436. model = 1;
  437. get_chatting_list(pn);
  438. break;
  439. case '2':
  440. alert('现在你可以通过点击聊天面板上的用户来删除该用户,删除的用户将不会再出现在聊天面板上\n警告:该操作仅是方便删除垃圾信息,请注意辨别消息发送用户,标记过程将不会出现提醒,且操作不可逆\n如果需要回到聊天模式,请点击右上角的三道杠');
  441. model = 2;
  442. get_chatting_list(pn);
  443. break;
  444. case '3':
  445. alert('已回到聊天模式');
  446. model = 0;
  447. get_chatting_list(pn);
  448. }
  449. }
  450. panel_main.scrollTop = 0;
  451. this_last_msg = 0;
  452. this_early_msg = 0;
  453. this_username = null;
  454. this_userid = 0;
  455. } else {
  456. var s = show_error(t.error_code, t.error_msg);
  457. var t = setTimeout(function() {
  458. s.parentElement.removeChild(s);
  459. }, 5000);
  460. s.onclick = function() {
  461. get_chatting_list(pn);
  462. s.parentElement.removeChild(s);
  463. clearTimeout(t);
  464. };
  465. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  466. }
  467. }
  468. };
  469. xhr.open('POST', '//tieba.baidu.com/c/s/comlist?' + get_string({
  470. //BDUSS:BDUSS,
  471. _client_id: _client_id,
  472. _client_type: _client_type,
  473. _client_version: _client_version,
  474. _phone_imei: _phone_imei,
  475. net_type: net_type,
  476. pn: pn
  477. }));
  478. xhr.send();
  479. }
  480.  
  481. function get_latest_msg(id, silent) {
  482. var silent = silent || false;
  483. var xhr = new XMLHttpRequest();
  484. xhr.onreadystatechange = function() {
  485. if (xhr.readyState == 4 && xhr.status == 200) {
  486. var t = JSON.parse(xhr.responseText);
  487. if (t.error_code == '0') {
  488. if (silent == false) {
  489. //console.log(this_early_msg+'|'+this_last_msg);
  490. if (id != this_userid || document.getElementsByClassName('tb_chat_panel_list')[0]) panel_main.innerHTML = '';
  491. panel_head.textContent = this_username;
  492. if (document.getElementsByClassName('tb_chat_panel_msg')[0]) var panel_msg = document.getElementsByClassName('tb_chat_panel_msg')[0];
  493. else {
  494. var panel_msg = document.createElement('div');
  495. panel_msg.className = 'tb_chat_panel_msg';
  496. panel_main.appendChild(panel_msg);
  497. }
  498. if (t.error && t.error.errno != '0') {
  499. var s = show_error(t.error.errno, t.error.errmsg + ' ' + t.error.usermsg);
  500. var t = setTimeout(function() {
  501. s.parentElement.removeChild(s);
  502. }, 5000);
  503. s.onclick = function() {
  504. get_latest_msg(id);
  505. s.parentElement.removeChild(s);
  506. clearTimeout(t);
  507. };
  508. //throw 'Tieba Chat Error '+t.error.errno+': '+t.error.errmsg+' '+t.error.usermsg;
  509. } else {
  510. com_portrait = t.com_portrait;
  511. user_portrait = t.user_portrait;
  512. if (t.has_more == 1) {
  513. var msg_more = document.createElement('div');
  514. msg_more.textContent = '点击加载更多';
  515. msg_more.style.cssText = 'width:200px;margin:20px auto;text-align:center;font-size:13px;background:#5b97ff;padding:5px;-webkit-user-select:none;-moz-user-select:none;cursor:pointer;color:#ffffff';
  516. panel_msg.appendChild(msg_more);
  517. msg_more.onclick = function() {
  518. get_early_msg(id, panel_msg, msg_more);
  519. };
  520. }
  521. for (var c in t.message) {
  522. if (t.message[c].content.length == 0) continue;
  523. var p = document.createElement('div');
  524. p.className = 'tb_chat_message';
  525. if (t.message[c].from == '0') {
  526. p.className += ' com';
  527. 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.replace(emo_regex, function(m, n) {
  528. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  529. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  530. } else {
  531. p.className += ' user';
  532. 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.replace(emo_regex, function(m, n) {
  533. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  534. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  535. }
  536. p.setAttribute('msg_id', t.message[c].msg_id);
  537. p.title = 'Post @ ' + get_locale_time(t.message[c].time);
  538. panel_msg.appendChild(p);
  539. if (parseInt(t.message[c].msg_id, 10) < parseInt(this_early_msg, 10) || this_early_msg == 0) this_early_msg = t.message[c].msg_id;
  540. if (parseInt(t.message[c].msg_id, 10) > parseInt(this_last_msg, 10) || this_last_msg == 0) this_last_msg = t.message[c].msg_id;
  541. //console.log(this_early_msg+'|'+this_last_msg);
  542. }
  543. panel_msg.scrollTop = panel_msg.scrollHeight;
  544. }
  545. this_userid = id;
  546. if (!document.getElementsByClassName('tb_chat_msg_panel')[0]) {
  547. var msg_panel = document.createElement('div');
  548. var msg_textarea = document.createElement('textarea');
  549. var msg_send = document.createElement('div');
  550. msg_panel.style.cssText = 'width:100%;height:100px;position:absolute;bottom:0;left:0';
  551. msg_panel.className = 'tb_chat_msg_panel';
  552. panel_main.appendChild(msg_panel);
  553. msg_textarea.style.cssText = 'width:385px;height:52px;font-size:13px;line-height: 1.5em;margin:7px;padding: 5px;box-sizing: border-box;position:absolute;left:0;top:0;resize:none';
  554. msg_textarea.className = 'tb_chat_msg_textarea';
  555. msg_panel.appendChild(msg_textarea);
  556. msg_send.style.cssText = 'width:50px;height:25px;font-size:14px;line-height:25px;text-align:center;position:absolute;right:6px;bottom:8px;background:#5b97ff;cursor:pointer;color:#ffffff';
  557. msg_send.textContent = '发送';
  558. msg_panel.appendChild(msg_send);
  559. var panel_emoji = document.createElement('div');
  560. var msg_emoji = document.createElement('div');
  561. msg_emoji.style.cssText = 'width:50px;height:25px;font-size:14px;line-height:25px;text-align:center;position:absolute;right:66px;bottom:8px;background:#5b97ff;cursor:pointer;color:#ffffff';
  562. msg_emoji.textContent = '表情';
  563. msg_panel.appendChild(msg_emoji);
  564. panel_emoji.className = 'tb_chat_msg_emoji';
  565. for (var i in emo_list) {
  566. var panel_emoji_sub = document.createElement('img');
  567. panel_emoji_sub.src = emo_list[i];
  568. panel_emoji_sub.setAttribute('title', i);
  569. panel_emoji.appendChild(panel_emoji_sub);
  570. panel_emoji_sub.onclick = function() {
  571. msg_textarea.value += '#(' + this.getAttribute('title') + ')'
  572. }
  573. }
  574. msg_panel.appendChild(panel_emoji);
  575. msg_send.onclick = function() {
  576. if (msg_textarea.value != '') {
  577. add_message(id, this_last_msg, msg_textarea.value);
  578. if (panel_emoji.hasAttribute('show')) panel_emoji.removeAttribute('show');
  579. }
  580. };
  581. msg_textarea.onfocus = function(event) {
  582. msg_textarea.onkeydown = function(event) {
  583. if (event.ctrlKey == 1 && event.keyCode == 13 && msg_textarea.value != '') {
  584. add_message(id, this_last_msg, msg_textarea.value);
  585. if (panel_emoji.hasAttribute('show')) panel_emoji.removeAttribute('show');
  586. }
  587. };
  588. };
  589. msg_textarea.onblur = function() {
  590. msg_textarea.onkeydown = null;
  591. };
  592. msg_emoji.onclick = function() {
  593. if (panel_emoji.hasAttribute('show')) panel_emoji.removeAttribute('show');
  594. else panel_emoji.setAttribute('show', '1');
  595. }
  596. window.onfocus = function() {
  597. if (this_msg_update == null) this_msg_update = setInterval(function() {
  598. if (document.getElementsByClassName('tb_chat_msg_panel')[0] && panel.hasAttribute('show')) get_latest_msg(id)
  599. }, 10000);
  600. };
  601. window.onblur = function() {
  602. if (this_msg_update != null) {
  603. clearInterval(this_msg_update);
  604. this_msg_update = null;
  605. }
  606. };
  607. panel_top_left.textContent = '←';
  608. panel_top_right.textContent = '≡';
  609. msg_textarea.focus();
  610. panel_top_right.onclick = function() {
  611. switch (prompt('请键入以下数字并点击“确定”以完成对应操作:\n1. 清空当前用户全部聊天记录\n2. 从聊天列表内删除当前用户\n\n(比较简陋真是抱歉了 _(:з」∠)_ )')) {
  612. case '1':
  613. delete_msg(id);
  614. break;
  615. case '2':
  616. delete_user(id);
  617. break;
  618. }
  619. };
  620. panel_top_left.onclick = function() {
  621. get_chatting_list(chatting_list_page);
  622. if (this_msg_update != null) {
  623. clearInterval(this_msg_update);
  624. this_msg_update = null;
  625. }
  626. window.onfocus = null;
  627. window.onblur = null;
  628. };
  629. }
  630. }
  631. get_message_update();
  632. } else {
  633. var s = show_error(t.error_code, t.error_msg);
  634. var t = setTimeout(function() {
  635. s.parentElement.removeChild(s);
  636. }, 5000);
  637. s.onclick = function() {
  638. get_latest_msg(id);
  639. s.parentElement.removeChild(s);
  640. clearTimeout(t);
  641. };
  642. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  643. }
  644. }
  645. };
  646. xhr.open('POST', '//tieba.baidu.com/c/s/recentmsg?' + get_string({
  647. //BDUSS:BDUSS,
  648. _client_id: _client_id,
  649. _client_type: _client_type,
  650. _client_version: _client_version,
  651. _phone_imei: _phone_imei,
  652. com_id: id,
  653. msg_id: this_last_msg,
  654. net_type: net_type,
  655. user_id: user_id
  656. }));
  657. xhr.send();
  658. }
  659.  
  660. function get_early_msg(id, panel_msg, msg_more) {
  661. msg_more.textContent = '正在载入......';
  662. var scroll_top = panel_msg.scrollHeight - panel_msg.scrollTop;
  663. var xhr = new XMLHttpRequest();
  664. xhr.onreadystatechange = function() {
  665. if (xhr.readyState == 4 && xhr.status == 200) {
  666. var t = JSON.parse(xhr.responseText);
  667. if (t.error_code == '0') {
  668. if (t.error && t.error.errno != '0') {
  669. var s = show_error(t.error.errno, t.error.errmsg);
  670. var t = setTimeout(function() {
  671. s.parentElement.removeChild(s);
  672. }, 5000);
  673. s.onclick = function() {
  674. get_early_msg(id, panel_msg, msg_more);
  675. s.parentElement.removeChild(s);
  676. clearTimeout(t);
  677. };
  678. throw 'Tieba Chat Error ' + t.error.errno + ': ' + t.error.errmsg;
  679. }
  680. var xxx = document.createElement('div');
  681. //console.log(this_early_msg+'|'+this_last_msg);
  682. panel_msg.insertBefore(xxx, msg_more.nextElementSibling);
  683. for (var c in t.message) {
  684. if (t.message[c].content.length == 0) continue;
  685. var p = document.createElement('div');
  686. p.className = 'tb_chat_message';
  687. if (t.message[c].from == '0') {
  688. p.className += ' com';
  689. 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.replace(emo_regex, function(m, n) {
  690. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  691. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  692. } else {
  693. p.className += ' user';
  694. 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.replace(emo_regex, function(m, n) {
  695. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  696. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  697. }
  698. p.setAttribute('msg_id', t.message[c].msg_id);
  699. p.title = 'Post @ ' + get_locale_time(t.message[c].time);
  700. if (parseInt(t.message[c].msg_id, 10) < parseInt(this_early_msg, 10) || this_early_msg == 0) this_early_msg = t.message[c].msg_id;
  701. if (parseInt(t.message[c].msg_id, 10) > parseInt(this_last_msg, 10) || this_last_msg == 0) this_last_msg = t.message[c].msg_id;
  702. xxx.appendChild(p);
  703. //console.log(this_early_msg+'|'+this_last_msg+'|'+t.message[c].msg_id);
  704. }
  705. if (t.has_more == 1) msg_more.textContent = '点击加载更多';
  706. else msg_more.parentElement.removeChild(msg_more);
  707. panel_msg.scrollTop = panel_msg.scrollHeight - scroll_top;
  708. } else {
  709. var s = show_error(t.error_code, t.error_msg);
  710. var t = setTimeout(function() {
  711. s.parentElement.removeChild(s);
  712. }, 5000);
  713. s.onclick = function() {
  714. get_early_msg(id, panel_msg, msg_more);
  715. s.parentElement.removeChild(s);
  716. clearTimeout(t);
  717. };
  718. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  719. }
  720. }
  721. };
  722. xhr.open('POST', '//tieba.baidu.com/c/s/historymsg?' + get_string({
  723. //BDUSS:BDUSS,
  724. _client_id: _client_id,
  725. _client_type: _client_type,
  726. _client_version: _client_version,
  727. _phone_imei: _phone_imei,
  728. com_id: id,
  729. msg_id: this_early_msg,
  730. net_type: net_type,
  731. user_id: user_id
  732. }));
  733. xhr.send();
  734. }
  735.  
  736. function add_message(id, last_msg_id, content) {
  737. var xhr = new XMLHttpRequest();
  738. xhr.onreadystatechange = function() {
  739. if (xhr.readyState == 4 && xhr.status == 200) {
  740. var t = JSON.parse(xhr.responseText);
  741. if (t.error_code == '0') {
  742. if (t.error && t.error.errno != '0') {
  743. var s = show_error(t.error.errno, t.error.errmsg);
  744. var t = setTimeout(function() {
  745. s.parentElement.removeChild(s);
  746. }, 5000);
  747. s.onclick = function() {
  748. add_message(id, last_msg_id, content);
  749. s.parentElement.removeChild(s);
  750. clearTimeout(t);
  751. };
  752. throw 'Tieba Chat Error ' + t.error.errno + ': ' + t.error.errmsg;
  753. }
  754. var panel_msg = document.getElementsByClassName('tb_chat_panel_msg')[0];
  755. for (var c in t.recent.message) {
  756. if (t.recent.message[c].content.length == 0) continue;
  757. var p = document.createElement('div');
  758. p.className = 'tb_chat_message';
  759. if (t.recent.message[c].from == '0') {
  760. p.className += ' com';
  761. 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.replace(emo_regex, function(m, n) {
  762. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  763. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  764. } else {
  765. p.className += ' user';
  766. 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.replace(emo_regex, function(m, n) {
  767. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  768. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  769. }
  770. p.setAttribute('msg_id', t.recent.message[c].msg_id);
  771. p.title = 'Post @ ' + get_locale_time(t.recent.message[c].time);
  772. panel_msg.appendChild(p);
  773. if (parseInt(t.recent.message[c].msg_id, 10) < parseInt(this_early_msg, 10) || this_early_msg == 0) this_early_msg = t.recent.message[c].msg_id;
  774. if (parseInt(t.recent.message[c].msg_id, 10) > parseInt(this_last_msg, 10) || this_last_msg == 0) this_last_msg = t.recent.message[c].msg_id;
  775. }
  776. var p = document.createElement('div');
  777. p.className = 'tb_chat_message';
  778. p.className += ' user';
  779. 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.replace(emo_regex, function(m, n) {
  780. return '<img src="' + emo_list[n] + '" class="tb_chat_message_emo">'
  781. }) || '未知数据') + '</div></div><div style="clear:both"></div>';
  782. p.setAttribute('msg_id', t.message.msg_id);
  783. p.title = 'Post @ ' + get_locale_time(t.message.time);
  784. panel_msg.appendChild(p);
  785. if (parseInt(t.message.msg_id, 10) < parseInt(this_early_msg, 10) || this_early_msg == 0) this_early_msg = t.message.msg_id;
  786. if (parseInt(t.message.msg_id, 10) > parseInt(this_last_msg, 10) || this_last_msg == 0) this_last_msg = t.message.msg_id;
  787. panel_msg.scrollTop = panel_msg.scrollHeight;
  788. document.getElementsByClassName('tb_chat_msg_textarea')[0].value = '';
  789. document.getElementsByClassName('tb_chat_msg_textarea')[0].focus();
  790. } else {
  791. var s = show_error(t.error_code, t.error_msg);
  792. var t = setTimeout(function() {
  793. s.parentElement.removeChild(s);
  794. }, 5000);
  795. s.onclick = function() {
  796. add_message(id, last_msg_id, content);
  797. s.parentElement.removeChild(s);
  798. clearTimeout(t);
  799. };
  800. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  801. }
  802. }
  803. };
  804. xhr.open('POST', '//tieba.baidu.com/c/s/addmsg?' + get_string({
  805. //BDUSS:BDUSS,
  806. _client_id: _client_id,
  807. _client_type: _client_type,
  808. _client_version: _client_version,
  809. _phone_imei: _phone_imei,
  810. com_id: id,
  811. content: content,
  812. last_msg_id: last_msg_id,
  813. net_type: net_type,
  814. user_id: user_id
  815. }));
  816. xhr.send();
  817. }
  818.  
  819. function show_error(c, t) {
  820. var panel_error = document.createElement('div');
  821. panel_error.className = 'tb_chat_error';
  822. panel.appendChild(panel_error);
  823. panel_error.innerHTML = 'Tieba Chat Error!!!<br>Error Code: ' + c + '<br>Error Message:' + t + '<br>Click this panel to retry....';
  824. return panel_error;
  825. }
  826.  
  827. function get_userid(username) {
  828. var xhr = new XMLHttpRequest();
  829. xhr.onreadystatechange = function() {
  830. if (xhr.readyState == 4 && xhr.status == 200) {
  831. var t = JSON.parse(xhr.responseText);
  832. if (t.no == 0) {
  833. this_username = username;
  834. this_last_msg = 0;
  835. this_early_msg = 0;
  836. //this_userid=t.data.id;
  837. get_latest_msg(t.data.id);
  838. } else {
  839. var s = show_error(t.no, t.error);
  840. var t = setTimeout(function() {
  841. s.parentElement.removeChild(s);
  842. }, 5000);
  843. s.onclick = function() {
  844. get_userid(username);
  845. s.parentElement.removeChild(s);
  846. clearTimeout(t);
  847. };
  848. throw 'Tieba Chat Error ' + t.no + ': ' + t.error;
  849. }
  850. }
  851. };
  852. xhr.open('POST', '//tieba.baidu.com/home/get/panel?ie=utf-8&un=' + encodeURIComponent(username));
  853. xhr.send();
  854. }
  855.  
  856. function delete_msg(id) {
  857. var xhr = new XMLHttpRequest();
  858. xhr.onreadystatechange = function() {
  859. if (xhr.readyState == 4 && xhr.status == 200) {
  860. var t = JSON.parse(xhr.responseText);
  861. if (t.error.errno == "0" && t.error_code == "0") {
  862. alert('清除成功!');
  863. document.getElementsByClassName('tb_chat_panel_msg')[0].innerHTML = '';
  864. } else {
  865. var s = show_error(t.error_code, t.error_msg);
  866. var t = setTimeout(function() {
  867. s.parentElement.removeChild(s);
  868. }, 5000);
  869. s.onclick = function() {
  870. delete_msg(id);
  871. s.parentElement.removeChild(s);
  872. clearTimeout(t);
  873. };
  874. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  875. }
  876. }
  877. };
  878. xhr.open('POST', '//tieba.baidu.com/c/s/clearmsg?' + get_string({
  879. _client_id: _client_id,
  880. _client_type: _client_type,
  881. _client_version: _client_version,
  882. _phone_imei: _phone_imei,
  883. com_id: id,
  884. net_type: net_type,
  885. user_id: user_id
  886. }));
  887. xhr.send();
  888. }
  889.  
  890. function delete_user(id, silent) {
  891. var silent = silent || false;
  892. var xhr = new XMLHttpRequest();
  893. xhr.onreadystatechange = function() {
  894. if (xhr.readyState == 4 && xhr.status == 200) {
  895. var t = JSON.parse(xhr.responseText);
  896. if (t.error.errno == "0" && t.error_code == "0") {
  897. if (silent == false) {
  898. alert('删除成功!');
  899. if (this_msg_update != null) {
  900. clearInterval(this_msg_update);
  901. this_msg_update = null;
  902. }
  903. window.onfocus = null;
  904. window.onblur = null;
  905. }
  906. get_chatting_list(chatting_list_page);
  907. } else {
  908. var s = show_error(t.error_code, t.error_msg);
  909. var t = setTimeout(function() {
  910. s.parentElement.removeChild(s);
  911. }, 5000);
  912. s.onclick = function() {
  913. delete_msg(id);
  914. s.parentElement.removeChild(s);
  915. clearTimeout(t);
  916. };
  917. throw 'Tieba Chat Error ' + t.error_code + ': ' + t.error_msg;
  918. }
  919. }
  920. };
  921. xhr.open('POST', '//tieba.baidu.com/c/s/delcom?' + get_string({
  922. _client_id: _client_id,
  923. _client_type: _client_type,
  924. _client_version: _client_version,
  925. _phone_imei: _phone_imei,
  926. com_id: id,
  927. net_type: net_type,
  928. tbs: PageData.tbs ? PageData.tbs : get_tbs(),
  929. user_id: user_id
  930. }));
  931. xhr.send();
  932. }
  933.  
  934. function get_tbs() {
  935. var xhr = new XMLHttpRequest();
  936. xhr.onreadystatechange = function() {
  937. if (xhr.readyState == 4 && xhr.status == 200) return JSON.parse(xhr.responseText).tbs;
  938. };
  939. xhr.open('GET', '//tieba.baidu.com/dc/common/tbs');
  940. xhr.send();
  941. }
  942.  
  943. function insert_chat_button(event) {
  944. if (event.animationName == 'nodeInserted') {
  945. var button = document.createElement('a');
  946. var node = event.target;
  947. button.className = 'btn_chat';
  948. node.parentElement.insertBefore(button, node);
  949. button.onclick = function() {
  950. if (document.getElementById('user_visit_card')) get_userid(JSON.parse(document.getElementById('user_visit_card').getAttribute('data-field')).un);
  951. else if (PageData.product == 'ihome') get_userid(ihome.Interaction.dataObj.un);
  952. panel.setAttribute('show', '1');
  953. };
  954. }
  955. }
  956.  
  957. var message_update_timer = setInterval(function() {
  958. if ((new Date().getTime() - (window.localStorage.getItem('tb_chat_message_update_timestamp') || message_update_timestamp)) > 10000) {
  959. get_message_update();
  960. } else if (window.localStorage.getItem('tb_chat_message_update_count') != '0') {
  961. document.title = '【' + window.localStorage.getItem('tb_chat_message_update_count') + ' 条新聊天】' + original_title;
  962. panel_top.style.color = 'orange';
  963. } else {
  964. document.title = original_title;
  965. panel_top.style.color = '#414243';
  966. }
  967. }, 1000);
  968.  
  969. // ------------------------------------------------------------------------
  970.  
  971.  
  972. var panel = document.createElement('div');
  973. var panel_top = document.createElement('div');
  974. var panel_head = document.createElement('div');
  975. var panel_main = document.createElement('div');
  976. var panel_top_left = document.createElement('div');
  977. var panel_top_right = document.createElement('div');
  978. var panel_ifshow = null;
  979. var original_title = document.title;
  980. var ss = document.createElement('style');
  981. var stylesheet = '.tb_chat_panel{width:400px;height:100%;position:fixed;top:calc(100% - 30px);right:0px;background:rgba(255,255,255,.85);z-index:200000;transition:all 0.5s /*ease-in*/cubic-bezier(.5,0,.8,1);-webkit-user-select:none;-moz-user-select:none;border:1px solid #cccccc;font-size:14px;color:#414243}.tb_chat_panel[show]{top:0px;transition:all 0.5s /*ease-out*/cubic-bezier(0,.5,.8,1)}.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;border-bottom:1px solid #cccccc}.tb_chat_panel_top_left{position:absolute;left:10px;top:3px;width:24px;height:24px;line-height:24px;background:#5b97ff;opacity:0;color:#ffffff;cursor:pointer;text-align:center;border-radius:50%;transition:all 0.5s ease-out}.tb_chat_panel[show] .tb_chat_panel_top_left{opacity:1;transition:all 0.5s ease-in}.tb_chat_panel_top_right{position:absolute;right:10px;top:3px;width:24px;height:24px;line-height:24px;background:#5b97ff;opacity:0;color:#ffffff;cursor:pointer;text-align:center;border-radius:50%;transition:all 0.5s ease-out}.tb_chat_panel[show] .tb_chat_panel_top_right{opacity:1;transition:all 0.5s ease-in}.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:13px;line-height:1.5em;min-height:28px;padding:5px}.tb_chat_message .tb_chat_avatar{width:32px;height:32px}.tb_chat_message.user .tb_chat_pleft,.tb_chat_message.user .tb_chat_pright{float:right;text-align:justify}.tb_chat_message .tb_chat_message_inner{margin:0 10px;padding:6px;background:#ffffff;border:1px solid #cccccc;border-radius:3px;-moz-user-select:text;-webkit-user-select:text;text-align:justify;overflow:hidden}.tb_chat_panel_msg{margin-bottom:100px;height:calc(100% - 100px);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:#ffffff;padding:5px;box-sizing:border-box}.tb_chat_message_inner::selection,.tb_chat_message_inner::-moz-selection{background:rgba(255,255,255,0.5)}.tb_chat_panel[model="1"] .tb_chat_panel_top,.tb_chat_panel[model="2"] .tb_chat_panel_top,.tb_chat_panel[model="2"] .tb_chat_row{color:red!important}.btn_sendmsg{animation:nodeInserted 0.01s;-webkit-animation:nodeInserted 0.01s;-o-animation:nodeInserted 0.01s;-ms-animation:nodeInserted 0.01s}@keyframes nodeInserted{from{opacity:1}to{opacity:1}}@-webkit-keyframes nodeInserted{from{opacity:1}to{opacity:1}}@-o-keyframes nodeInserted{from{opacity:1}to{opacity:1}}@-ms-keyframes nodeInserted{from{opacity:1}to{opacity:1}}.btn_chat{cursor:pointer;margin-left:8px;background-image:url(http://imgsrc.baidu.com/forum/pic/item/c28b2d6d55fbb2fb8303a7d94d4a20a44723dcb3.jpg)!important;background-position:0 0!important}.btn_chat:hover{background-position:-86px 0!important}.btn_chat:active{background-position:-172px 0!important}.interaction_wrap_theme1 .btn_chat{background-position:0 -22px!important;width:70px!important}.interaction_wrap_theme1 .btn_chat:hover{background-position:-86px -22px!important}.interaction_wrap_theme1 .btn_chat:active{background-position:-172px -22px!important}.tb_chat_message_emo,.tb_chat_msg_emoji img{width:20px;height:20px}.tb_chat_msg_emoji img{cursor:pointer}.tb_chat_msg_emoji{width:380px;height:140px;padding:9px;border:1px solid #cccccc;position:absolute;bottom:100px;background:#ffffff;transition:all 0.1s linear;opacity:0;pointer-events:none}.tb_chat_msg_emoji[show]{opacity:1;pointer-events:auto}';
  982. panel.className = 'tb_chat_panel';
  983. document.body.appendChild(panel);
  984. ss.textContent = stylesheet;
  985. panel.appendChild(ss);
  986. panel_top.className = 'tb_chat_panel_top';
  987. panel.appendChild(panel_top);
  988. panel_head.textContent = '贴吧聊天';
  989. panel_top.appendChild(panel_head);
  990. panel_top_left.className = 'tb_chat_panel_top_left';
  991. panel_top_left.textContent = '+';
  992. panel_top.appendChild(panel_top_left);
  993. panel_top_right.className = 'tb_chat_panel_top_right';
  994. panel_top_right.textContent = '≡';
  995. panel_top.appendChild(panel_top_right);
  996. panel_main.className = 'tb_chat_panel_main';
  997. panel.appendChild(panel_main);
  998.  
  999. panel_top.onmouseover = function() {
  1000. if (!panel.hasAttribute('show')) {
  1001. if (this_userid == 0) get_chatting_list(chatting_list_page);
  1002. else get_latest_msg(this_userid);
  1003. }
  1004. panel.setAttribute('show', '1');
  1005. };
  1006. panel.onmouseover = function() {
  1007. if (panel_ifshow != null) clearTimeout(panel_ifshow);
  1008. };
  1009. panel.onmouseout = function() {
  1010. panel_ifshow = setTimeout(function() {
  1011. if (panel.hasAttribute('show')) panel.removeAttribute('show');
  1012. panel_ifshow = null;
  1013. }, 500);
  1014. };
  1015. panel_top_left.onclick = function() {
  1016. var username = prompt('请输入用户名以发起聊天');
  1017. if (username != null && username != '') get_userid(username);
  1018. };
  1019. get_chatting_list(chatting_list_page);
  1020. document.addEventListener('animationstart', insert_chat_button, false);
  1021. document.addEventListener('MSAnimationStart', insert_chat_button, false);
  1022. document.addEventListener('webkitAnimationStart', insert_chat_button, false);