🏠 

bilibiliPlayComment

提取B站评论只B站播放器右侧,实现评论和播放器同时观看,拒绝下拉!


安装此脚本?
  1. // ==UserScript==
  2. // @name bilibiliPlayComment
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.4
  5. // @description 提取B站评论只B站播放器右侧,实现评论和播放器同时观看,拒绝下拉!
  6. // @author shepen
  7. // @match http://*/*
  8. // @grant none
  9. // ==/UserScript==
  10. if (!window.jQuery){
  11. var jq = document.createElement('script');
  12. jq.src = "https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js";
  13. document.getElementsByTagName('head')[0].appendChild(jq);
  14. }
  15. var _style = ".bilibiliPlayer-comment::-webkit-scrollbar { width: 0 !important } .bilibiliPlayer-comment { -ms-overflow-style: none; } .bilibiliPlayer-comment-item-child-wrapper::-webkit-scrollbar { width: 0 !important } .bilibiliPlayer-comment-item-child-wrapper { -ms-overflow-style: none; } .rotate { animation-name:myfirst;animation-duration:1s;animation-timing-function:ease;animation-delay:0s;animation-iteration-count:infinite;animation-direction:normal;animation-play-state:running;} @keyframes myfirst{from{transform:rotate(0deg);-ms-transform:rotate(0deg);-moz-transform:rotate(0deg);-webkit-transform:rotate(0deg);-o-transform:rotate(0deg)}to{transform:rotate(-360deg);-ms-transform:rotate(-360deg);-moz-transform:rotate(-360deg);-webkit-transform:rotate(-360deg);-o-transform:rotate(-360deg)}}@-webkit-keyframes myfirst{from{transform:rotate(0deg);-ms-transform:rotate(0deg);-moz-transform:rotate(0deg);-webkit-transform:rotate(0deg);-o-transform:rotate(0deg)}to{transform:rotate(-360deg);-ms-transform:rotate(-360deg);-moz-transform:rotate(-360deg);-webkit-transform:rotate(-360deg);-o-transform:rotate(-360deg)}} .bilibiliPlayer-comment { overflow: -moz-scrollbars-none; } .bilibiliPlayer-comment-item-child-wrapper { overflow: -moz-scrollbars-none; }"
  16. var style = document.createElement('style');
  17. style.innerHTML = _style;
  18. document.getElementsByTagName('head')[0].appendChild(style);
  19. var bilibiliComment = {
  20. b_video:'',
  21. b_video_pos : '',
  22. page:1,
  23. data:[],
  24. append:false,
  25. replyPages:[],
  26. init:function(){
  27. this.getVideoAid();
  28. this.getBilibilPlayer();
  29. },
  30. getBilibilPlayer:function(){
  31. var bilibiliPlayer = document.getElementById('bilibili-player');
  32. this.b_video_pos = {'top':bilibiliPlayer.offsetTop,'left':bilibiliPlayer.offsetLeft,'width':bilibiliPlayer.offsetWidth,'height':bilibiliPlayer.offsetHeight}
  33. this.createCommentShowLayer();
  34. this.showLoading();
  35. this.addRefreshLoaingAnimattion('add');
  36. this.commentGet();
  37. this.bindEventListen();
  38. },
  39. createCommentShowLayer:function(){
  40. var bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
  41. var bodyWidth = document.documentElement.clientWidth || document.body.clientWidth;
  42. var _commentLayerStyle = "width:{width}px; height:{height}px; position:absolute; z-index:99999;top:{top}px;left:{left}px;background:rgba(0,0,0,.8)";
  43. var width = bodyWidth-this.b_video_pos['width']-(this.b_video_pos['left']+50);
  44. _commentLayerStyle = _commentLayerStyle.replace('{height}',bodyHeight-this.b_video_pos['top']).replace('{top}',this.b_video_pos['top']).replace('{left}',this.b_video_pos['left']+this.b_video_pos['width']).replace('{width}',width);
  45. if (_commentLayer = document.getElementById('bilibiliCommentPlay')){
  46. document.body.removeChild(_commentLayer);
  47. }
  48. var _commentLayerHtml = '<div style="height:40px;color:#fff; line-height:40px; font-size:15px; padding-left:10px;">哔哩哔哩评论插件 <span style="font-size:13px; color:#ff6600;">共 <i id="comment_count">(-)</i>条 评论</span><div class="bilibiliPlayer-comment-opration" style="float:right; height:30px; padding-top:5px; padding-top:5px; margin-right:10px;"><a href="javascript:;" id="refresh-comment" title="刷新评论" style="width:20px; height:20px; display:inline-block; margin-right:5px; background:url(http://chuantu.xyz/t6/741/1605963079x992249049.png)"></a><a href="javascript:;" id="close-comment" title="关闭插件" style="width:20px; height:20px; display:inline-block; background:url(http://chuantu.xyz/t6/741/1605962908x-1224475230.png)"></a></div></div>';
  49. _commentLayerHtml += '<div class="bilibiliPlayer-comment"id="bilibiliPlayer-comment"style="width:90%; margin:0 auto;height:'+(bodyHeight-this.b_video_pos['top']-50)+'px;min-height:60px; overflow-y:scroll;"><div class="bilibiliPlayer-comment-item"style="height:auto; min-height:60px;"><div class="bilibiliPlayer-comment-no-data"style="height:40px; line-height:40px; color:#ccc;">暂无评论数据</div></div></div>'
  50. var _commentLayer = document.createElement('div');
  51. _commentLayer.style = _commentLayerStyle;
  52. _commentLayer.id = 'bilibiliCommentPlay';
  53. _commentLayer.innerHTML = _commentLayerHtml;
  54. document.body.appendChild(_commentLayer);
  55. },
  56. commentGet:function(){
  57. console.log(this.ajax);
  58. if (this.ajax){
  59. return;
  60. }
  61. this.ajax = true;
  62. var api_url = "https://yunxingyu.wojiong.com/querycomment.php";
  63. var _this = this;
  64. $.ajax({
  65. url:api_url,
  66. type:'get',
  67. data:{'aid':this.aid,page:this.page},
  68. dataType:"jsonp",
  69. success:function(res){
  70. if (res['code']!=0){
  71. _this.outMessage('api服务器错误');
  72. }else{
  73. if (!res.data['replies']){
  74. this.ajax = true;
  75. _this.addRefreshLoaingAnimattion('remove');
  76. _this.outEmptyNoData();
  77. }else{
  78. _this.addRefreshLoaingAnimattion('remove');
  79. _this.setCommentNum(res.data['page']['acount']);
  80. _this.commentData = res.data['replies'];
  81. _this.merge(res.data['replies']);
  82. _this.renderHtml();
  83. _this.bindScrollEvent();
  84. _this.bindShowReply();
  85. }
  86. }
  87. }
  88. })
  89. },
  90. commentReplyGet:function(obj){
  91. if (this.ajax){
  92. return;
  93. }
  94. this.ajax = true;
  95. var api_url = "https://yunxingyu.wojiong.com/queryreplycomment.php";
  96. var _this = this;
  97. $.ajax({
  98. url:api_url,
  99. type:'get',
  100. data:{'aid':this.aid,'rootid':obj['datarootid'],page:this.replyPages[obj['index']]['page']},
  101. dataType:"jsonp",
  102. success:function(res){
  103. if (res['code']!=0){
  104. _this.outMessage('api服务器错误');
  105. }else{
  106. if (!res.data['replies']){
  107. this.ajax = true;
  108. }else{
  109. _this.addRefreshLoaingAnimattion('remove');
  110. _this.commentData = res.data['replies'];
  111. _this.renderMoreComment(obj['index']);
  112. _this.mergeToCommentChild(res.data['replies'],obj['datarootid']);
  113. }
  114. }
  115. }
  116. })
  117. },
  118. merge:function(data){
  119. for (var k in data){
  120. this.data.push(data[k]);
  121. }
  122. },
  123. mergeToCommentChild:function(data,val){
  124. var index = this.getIndexInArr('rpid',val);
  125. for (var k in data){
  126. this.data[index]['replies'].push(data[k]);
  127. }
  128. },
  129. outEmptyNoData:function(){
  130. var _loading = '<div class="data-empty" style="font-size:14px; color:#ccc;">暂时没有任何评论^-^ o)</div>';
  131. document.getElementById('bilibiliPlayer-comment').innerHTML = _loading;
  132. },
  133. addRefreshLoaingAnimattion:function(type){
  134. var refreshComment = document.getElementById('refresh-comment');
  135. if (type=='add'){
  136. refreshComment.className = 'rotate';
  137. }else{
  138. refreshComment.className = '';
  139. }
  140. },
  141. bindEventListen:function(){
  142. var _this = this;
  143. $("#refresh-comment").click(function(){
  144. _this.data = [];
  145. _this.replyPages = [];
  146. _this.page = 1;
  147. _this.commentGet();
  148. _this.addRefreshLoaingAnimattion('add');
  149. });
  150. $("#close-comment").click(function(){
  151. // 关闭评论
  152. _this.commentLayCtr();
  153. _this.appendCommentIcon();
  154. });
  155. window.onresize = function(){
  156. console.log('窗口大小改变');
  157. _this.reRenderPos();
  158. }
  159. var _wr = function(type) {
  160. var orig = history[type];
  161. return function() {
  162. var rv = orig.apply(this, arguments);
  163. var e = new Event(type);
  164. e.arguments = arguments;
  165. window.dispatchEvent(e);
  166. return rv;
  167. };
  168. };
  169. history.pushState = _wr('pushState');
  170. history.replaceState = _wr('replaceState');
  171. window.addEventListener('pushState', function(e) {
  172. window.location.reload();
  173. });
  174. window.addEventListener('replaceState', function(e) {
  175. });
  176. },
  177. bindScrollEvent:function(){
  178. var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
  179. var nScrollTop = 0; //滚动到的当前位置
  180. var _this = this;
  181. var nDivHight = $("#bilibiliPlayer-comment").height();
  182. $("#bilibiliPlayer-comment").scroll(function(){
  183. nScrollHight = $(this)[0].scrollHeight;
  184. nScrollTop = $(this)[0].scrollTop;
  185. if(nScrollTop + nDivHight >= nScrollHight){;
  186. _this.page = _this.page+1;
  187. _this.append = true;
  188. _this.addRefreshLoaingAnimattion('add');
  189. _this.commentGet();
  190. }
  191. })
  192. },
  193. reRenderPos:function(){
  194. var bilibiliPlayer = document.getElementById('bilibili-player');
  195. this.b_video_pos = {'top':bilibiliPlayer.offsetTop,'left':bilibiliPlayer.offsetLeft,'width':bilibiliPlayer.offsetWidth,'height':bilibiliPlayer.offsetHeight}
  196. var bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
  197. var bodyWidth = document.documentElement.clientWidth || document.body.clientWidth;
  198. var width = bodyWidth-this.b_video_pos['width']-(this.b_video_pos['left']+50);
  199. var bilibiliCommentPlay = document.getElementById('bilibiliCommentPlay');
  200. bilibiliCommentPlay.style.left = this.b_video_pos['left']+this.b_video_pos['width']+'px';
  201. bilibiliCommentPlay.style.top = this.b_video_pos['top']+'px';
  202. bilibiliCommentPlay.style.width = width+'px';
  203. bilibiliCommentPlay.style.height = bodyHeight-this.b_video_pos['top']+'px'
  204. },
  205. bindShowReply:function(){
  206. $('.no-read-show').click(function(){
  207. var datarootid = $(this).attr('data-root');
  208. var rcount_str = $(this).find('span').html();
  209. console.log(rcount_str);
  210. var rcount = parseInt(rcount_str.replace(/[^0-9]/ig,""));
  211. var replyPages = bilibiliComment.replyPages;
  212. var _page_config_index = false;
  213. if (replyPages.length>0){
  214. for (k in replyPages){
  215. if (replyPages[k] == datarootid){
  216. _page_config_index = k;
  217. }
  218. }
  219. if (!_page_config_index) {
  220. var _page_config = {'rootid':datarootid,'page':1,allrcount:rcount+2};
  221. bilibiliComment.replyPages.push(_page_config);
  222. _page_config_index = bilibiliComment.replyPages.length-1;
  223. }else{
  224. bilibiliComment.replyPages[_page_config_index]['page'] = replyPages[_page_config_index]['page']+=1;
  225. }
  226. }else{
  227. var _page_config = {'rootid':datarootid,'page':1,allrcount:rcount+2};
  228. bilibiliComment.replyPages.push(_page_config);
  229. _page_config_index = bilibiliComment.replyPages.length-1;
  230. }
  231. bilibiliComment.parent = $(this).parent();
  232. console.log('加载更多评论')
  233. bilibiliComment.commentReplyGet({'index':_page_config_index,'datarootid':datarootid});
  234. })
  235. },
  236. commentLayCtr:function(display){
  237. var block = display?'block':'none';
  238. document.getElementById('bilibiliCommentPlay').style.display = block;
  239. },
  240. appendCommentIcon:function(){
  241. var bilibiliPlayerCommentIcon = document.createElement('div');
  242. bilibiliPlayerCommentIcon.style = 'width:47px; height:39px; background:#f7f9fa; border:1px solid #e5e9ef; border-radius:3px; padding-top:8px; cursor: pointer; position:fixed;top:75%; right:6px; ';
  243. bilibiliPlayerCommentIcon.innerHTML = "<div style='width:30px; height:30px; margin:0 auto; background:url(http://chuantu.xyz/t6/741/1606024283x1700338641.png)'></div>"
  244. bilibiliPlayerCommentIcon.title = '打开哔哩哔哩评论插件';
  245. document.body.appendChild(bilibiliPlayerCommentIcon);
  246. bilibiliPlayerCommentIcon.onclick = function(){
  247. bilibiliComment.commentLayCtr(true);
  248. document.body.removeChild(bilibiliPlayerCommentIcon);
  249. }
  250. },
  251. showLoading:function(){
  252. var _loading = '<div class="loading" style="font-size:14px; color:#ccc;">正在加载评论(^-^ o)</div>';
  253. document.getElementById('bilibiliPlayer-comment').innerHTML = _loading;
  254. },
  255. renderHtml:function(){
  256. var html = "";
  257. var data = this.commentData;
  258. for (var k in data){
  259. html += '<div class="bilibiliPlayer-comment-item" style="height:auto; min-height:60px; margin-bottom:10px">'
  260. html += '<div class="bilibiliPlayer-comment-item-main"><div style="width:60px; height:60px; border-radius: 30px; background:#ff6600;overflow:hidden; display:inline-block; margin-right:10px;"><img src="'+data[k]['member']['avatar']+'" style="width:100%";></div><div class="bilibiliPlayer-comment-item-content"style="height:auto; vertical-align: top; width:70%; display:inline-block;"><div class="user-name"style="font-size:14px;height:25px; line-height:25px;color:#ccc;">'+data[k]['member']['uname']+'</div><div class="bilibili-comment-con"style="padding:5px; color:#eee; padding-left:0;">'+data[k]['content']['message']+'</div></div></div>';
  261. if (data[k]['replies']){
  262. var dataLen = data[k]['replies'].length>2?2:data[k]['replies'].length;
  263. html += '<div class="bilibiliPlayer-comment-item-child-wrapper" style="position:relative; max-height:400px; overflow-y:scroll;">'
  264. for (var i = 0; i < dataLen; i++) {
  265. html += '<div class="bilibiliPlayer-comment-item-child"style="padding-left:60px"><div style="width:40px; height:40px; border-radius: 30px; background:#ff6600;overflow:hidden; display:inline-block; margin-right:10px;"><img src="'+data[k]['replies'][i]['member']['avatar']+'"style="width:100%";></div><div class="bilibiliPlayer-comment-item-content"style="height:auto; vertical-align: top; width:70%; display:inline-block;"><div class="user-name"style="font-size:14px;height:20px; line-height:20px;color:#ccc;">'+data[k]['replies'][i]['member']['uname']+'</div><div class="bilibili-comment-con"style="padding:2px; color:#eee; padding-left:0;">'+data[k]['replies'][i]['content']['message']+'</div></div></div>';
  266. }
  267. if (data[k]['replies'].length>2){
  268. html+= '<div class="no-read-show" style="position:absolute; bottom:5px; right:10px; cursor:pointer; color:#ccc;" data-root="'+data[k]['rpid']+'">剩余<span style="color:#00CCCC;">'+(data[k]['rcount']-2)+'</span>条未看</div>'
  269. }
  270. html += "</div>";
  271. }
  272. html += '</div>';
  273. };
  274. if (bilibiliComment.append){
  275. document.getElementById('bilibiliPlayer-comment').innerHTML += html;;
  276. }else{
  277. document.getElementById('bilibiliPlayer-comment').innerHTML = html;
  278. }
  279. bilibiliComment.ajax = false;
  280. },
  281. renderMoreComment:function(index){
  282. var data = this.commentData;
  283. console.log(this.replyPages);
  284. var html = '';
  285. for (var k in data) {
  286. html += '<div class="bilibiliPlayer-comment-item-child"style="padding-left:60px"><div style="width:40px; height:40px; border-radius: 30px; background:#ff6600;overflow:hidden; display:inline-block; margin-right:10px;"><img src="'+data[k]['member']['avatar']+'"style="width:100%";></div><div class="bilibiliPlayer-comment-item-content"style="height:auto; vertical-align: top; width:70%; display:inline-block;"><div class="user-name"style="font-size:14px;height:20px; line-height:20px;color:#ccc;">'+data[k]['member']['uname']+'</div><div class="bilibili-comment-con"style="padding:2px; color:#eee; padding-left:0;">'+data[k]['content']['message']+'</div></div></div>';
  287. if (this.replyPages[index]['allrcount']>this.replyPages[index]['page']*data.length){
  288. html+= '<div class="no-read-show" style="position:absolute; bottom:5px; right:10px; cursor:pointer; color:#ccc;" data-root="'+data[k]['rpid']+'">剩余<span style="color:#00CCCC;">'+(this.replyPages[index]['allrcount']-this.replyPages[index]['page']*data.length)+'</span>条未看</div>'
  289. }
  290. }
  291. bilibiliComment.parent.html(html);
  292. bilibiliComment.ajax = false;
  293. },
  294. getIndexInArr:function(key,val){
  295. for (var i = 0; i < this.data.length; i++) {
  296. if (this.data[i][key]==val){
  297. return i;
  298. }
  299. }
  300. },
  301. setCommentNum:function(num){
  302. document.getElementById('comment_count').innerHTML = num;
  303. },
  304. getVideoAid:function(){
  305. this.aid = window.aid;
  306. console.log(this);
  307. }
  308. }
  309. window.onload = function(){
  310. console.log('bilibiliPlayComment 已加载');
  311. bilibiliComment.init();
  312. }