// ==UserScript==
// @name bilibiliPlayComment
// @namespace http://tampermonkey.net/
// @version 0.1.4
// @description 提取B站评论只B站播放器右侧,实现评论和播放器同时观看,拒绝下拉!
// @author shepen
// @match http://*/*
// @grant none
// @downloadURL https://update.greasyfork.icu/scripts/416628/bilibiliPlayComment.user.js
// @updateURL https://update.greasyfork.icu/scripts/416628/bilibiliPlayComment.meta.js
// ==/UserScript==
if (!window.jQuery){
var jq = document.createElement('script');
jq.src = "https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
}
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; }"
var style = document.createElement('style');
style.innerHTML = _style;
document.getElementsByTagName('head')[0].appendChild(style);
var bilibiliComment = {
b_video:'',
b_video_pos : '',
page:1,
data:[],
append:false,
replyPages:[],
init:function(){
this.getVideoAid();
this.getBilibilPlayer();
},
getBilibilPlayer:function(){
var bilibiliPlayer = document.getElementById('bilibili-player');
this.b_video_pos = {'top':bilibiliPlayer.offsetTop,'left':bilibiliPlayer.offsetLeft,'width':bilibiliPlayer.offsetWidth,'height':bilibiliPlayer.offsetHeight}
this.createCommentShowLayer();
this.showLoading();
this.addRefreshLoaingAnimattion('add');
this.commentGet();
this.bindEventListen();
},
createCommentShowLayer:function(){
var bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
var bodyWidth = document.documentElement.clientWidth || document.body.clientWidth;
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)";
var width = bodyWidth-this.b_video_pos['width']-(this.b_video_pos['left']+50);
_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);
if (_commentLayer = document.getElementById('bilibiliCommentPlay')){
document.body.removeChild(_commentLayer);
}
var _commentLayerHtml = '
哔哩哔哩评论插件 共 条 评论
';
_commentLayerHtml += ''
var _commentLayer = document.createElement('div');
_commentLayer.style = _commentLayerStyle;
_commentLayer.id = 'bilibiliCommentPlay';
_commentLayer.innerHTML = _commentLayerHtml;
document.body.appendChild(_commentLayer);
},
commentGet:function(){
console.log(this.ajax);
if (this.ajax){
return;
}
this.ajax = true;
var api_url = "https://yunxingyu.wojiong.com/querycomment.php";
var _this = this;
$.ajax({
url:api_url,
type:'get',
data:{'aid':this.aid,page:this.page},
dataType:"jsonp",
success:function(res){
if (res['code']!=0){
_this.outMessage('api服务器错误');
}else{
if (!res.data['replies']){
this.ajax = true;
_this.addRefreshLoaingAnimattion('remove');
_this.outEmptyNoData();
}else{
_this.addRefreshLoaingAnimattion('remove');
_this.setCommentNum(res.data['page']['acount']);
_this.commentData = res.data['replies'];
_this.merge(res.data['replies']);
_this.renderHtml();
_this.bindScrollEvent();
_this.bindShowReply();
}
}
}
})
},
commentReplyGet:function(obj){
if (this.ajax){
return;
}
this.ajax = true;
var api_url = "https://yunxingyu.wojiong.com/queryreplycomment.php";
var _this = this;
$.ajax({
url:api_url,
type:'get',
data:{'aid':this.aid,'rootid':obj['datarootid'],page:this.replyPages[obj['index']]['page']},
dataType:"jsonp",
success:function(res){
if (res['code']!=0){
_this.outMessage('api服务器错误');
}else{
if (!res.data['replies']){
this.ajax = true;
}else{
_this.addRefreshLoaingAnimattion('remove');
_this.commentData = res.data['replies'];
_this.renderMoreComment(obj['index']);
_this.mergeToCommentChild(res.data['replies'],obj['datarootid']);
}
}
}
})
},
merge:function(data){
for (var k in data){
this.data.push(data[k]);
}
},
mergeToCommentChild:function(data,val){
var index = this.getIndexInArr('rpid',val);
for (var k in data){
this.data[index]['replies'].push(data[k]);
}
},
outEmptyNoData:function(){
var _loading = '暂时没有任何评论^-^ o)
';
document.getElementById('bilibiliPlayer-comment').innerHTML = _loading;
},
addRefreshLoaingAnimattion:function(type){
var refreshComment = document.getElementById('refresh-comment');
if (type=='add'){
refreshComment.className = 'rotate';
}else{
refreshComment.className = '';
}
},
bindEventListen:function(){
var _this = this;
$("#refresh-comment").click(function(){
_this.data = [];
_this.replyPages = [];
_this.page = 1;
_this.commentGet();
_this.addRefreshLoaingAnimattion('add');
});
$("#close-comment").click(function(){
// 关闭评论
_this.commentLayCtr();
_this.appendCommentIcon();
});
window.onresize = function(){
console.log('窗口大小改变');
_this.reRenderPos();
}
var _wr = function(type) {
var orig = history[type];
return function() {
var rv = orig.apply(this, arguments);
var e = new Event(type);
e.arguments = arguments;
window.dispatchEvent(e);
return rv;
};
};
history.pushState = _wr('pushState');
history.replaceState = _wr('replaceState');
window.addEventListener('pushState', function(e) {
window.location.reload();
});
window.addEventListener('replaceState', function(e) {
});
},
bindScrollEvent:function(){
var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
var nScrollTop = 0; //滚动到的当前位置
var _this = this;
var nDivHight = $("#bilibiliPlayer-comment").height();
$("#bilibiliPlayer-comment").scroll(function(){
nScrollHight = $(this)[0].scrollHeight;
nScrollTop = $(this)[0].scrollTop;
if(nScrollTop + nDivHight >= nScrollHight){;
_this.page = _this.page+1;
_this.append = true;
_this.addRefreshLoaingAnimattion('add');
_this.commentGet();
}
})
},
reRenderPos:function(){
var bilibiliPlayer = document.getElementById('bilibili-player');
this.b_video_pos = {'top':bilibiliPlayer.offsetTop,'left':bilibiliPlayer.offsetLeft,'width':bilibiliPlayer.offsetWidth,'height':bilibiliPlayer.offsetHeight}
var bodyHeight = document.documentElement.clientHeight || document.body.clientHeight;
var bodyWidth = document.documentElement.clientWidth || document.body.clientWidth;
var width = bodyWidth-this.b_video_pos['width']-(this.b_video_pos['left']+50);
var bilibiliCommentPlay = document.getElementById('bilibiliCommentPlay');
bilibiliCommentPlay.style.left = this.b_video_pos['left']+this.b_video_pos['width']+'px';
bilibiliCommentPlay.style.top = this.b_video_pos['top']+'px';
bilibiliCommentPlay.style.width = width+'px';
bilibiliCommentPlay.style.height = bodyHeight-this.b_video_pos['top']+'px'
},
bindShowReply:function(){
$('.no-read-show').click(function(){
var datarootid = $(this).attr('data-root');
var rcount_str = $(this).find('span').html();
console.log(rcount_str);
var rcount = parseInt(rcount_str.replace(/[^0-9]/ig,""));
var replyPages = bilibiliComment.replyPages;
var _page_config_index = false;
if (replyPages.length>0){
for (k in replyPages){
if (replyPages[k] == datarootid){
_page_config_index = k;
}
}
if (!_page_config_index) {
var _page_config = {'rootid':datarootid,'page':1,allrcount:rcount+2};
bilibiliComment.replyPages.push(_page_config);
_page_config_index = bilibiliComment.replyPages.length-1;
}else{
bilibiliComment.replyPages[_page_config_index]['page'] = replyPages[_page_config_index]['page']+=1;
}
}else{
var _page_config = {'rootid':datarootid,'page':1,allrcount:rcount+2};
bilibiliComment.replyPages.push(_page_config);
_page_config_index = bilibiliComment.replyPages.length-1;
}
bilibiliComment.parent = $(this).parent();
console.log('加载更多评论')
bilibiliComment.commentReplyGet({'index':_page_config_index,'datarootid':datarootid});
})
},
commentLayCtr:function(display){
var block = display?'block':'none';
document.getElementById('bilibiliCommentPlay').style.display = block;
},
appendCommentIcon:function(){
var bilibiliPlayerCommentIcon = document.createElement('div');
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; ';
bilibiliPlayerCommentIcon.innerHTML = ""
bilibiliPlayerCommentIcon.title = '打开哔哩哔哩评论插件';
document.body.appendChild(bilibiliPlayerCommentIcon);
bilibiliPlayerCommentIcon.onclick = function(){
bilibiliComment.commentLayCtr(true);
document.body.removeChild(bilibiliPlayerCommentIcon);
}
},
showLoading:function(){
var _loading = '正在加载评论(^-^ o)
';
document.getElementById('bilibiliPlayer-comment').innerHTML = _loading;
},
renderHtml:function(){
var html = "";
var data = this.commentData;
for (var k in data){
html += '';
};
if (bilibiliComment.append){
document.getElementById('bilibiliPlayer-comment').innerHTML += html;;
}else{
document.getElementById('bilibiliPlayer-comment').innerHTML = html;
}
bilibiliComment.ajax = false;
},
renderMoreComment:function(index){
var data = this.commentData;
console.log(this.replyPages);
var html = '';
for (var k in data) {
html += '';
if (this.replyPages[index]['allrcount']>this.replyPages[index]['page']*data.length){
html+= '剩余'+(this.replyPages[index]['allrcount']-this.replyPages[index]['page']*data.length)+'条未看
'
}
}
bilibiliComment.parent.html(html);
bilibiliComment.ajax = false;
},
getIndexInArr:function(key,val){
for (var i = 0; i < this.data.length; i++) {
if (this.data[i][key]==val){
return i;
}
}
},
setCommentNum:function(num){
document.getElementById('comment_count').innerHTML = num;
},
getVideoAid:function(){
this.aid = window.aid;
console.log(this);
}
}
window.onload = function(){
console.log('bilibiliPlayComment 已加载');
bilibiliComment.init();
}