// ==UserScript==
// @name bilibili toothbrush
// @namespace http://www.icycat.com
// @description 牙刷科技,还原哔哩哔哩播放器,404页面视频需挂代理
// @include *www.bilibili.tv/video/av*
// @include *www.bilibili.com/video/av*
// @include *bilibili.kankanews.com/video/av*
// @version 2.8
// @grant GM_xmlhttpRequest
// @run-at document-start
// ==/UserScript==
var bili = {
aid: null,
cid: null,
bofqi: null,
oPlayer: null,
title: null,
iframe: function() {
return '<iframe id="bofqi_embed" width="950" height="482" class="player" src="https://secure.bilibili.tv/secure,cid=' + this.cid + '&aid=' + this.aid + '" scrolling="no" border="0" frameborder="no" framespacing="0" onload="window.securePlayerFrameLoaded=true"></iframe>';
},
embed: function() {
return '<embed id="bofqi_embed" rel="noreferrer" width="950" height="482" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowFullScreenInteractive="true" flashvars="cid=' + this.cid + '&aid=' + this.aid + '" src="http://static.hdslb.com/play.swf" type="application/x-shockwave-flash" AllowScriptAccess="always" allowfullscreen="true" quality="high" />';
},
embedFull: function() {
return '<embed id="bofqi_embed" rel="noreferrer" width="100%" height="100%" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowFullScreenInteractive="true" flashvars="cid=' + this.cid + '&aid=' + this.aid + '" src="http://static.hdslb.com/play.swf" type="application/x-shockwave-flash" AllowScriptAccess="always" allowfullscreen="true" quality="high" />';
},
catplayer: function(id) {
return '<embed id="catplayer" rel="noreferrer" src="http://v.icycat.com/catplayer.swf" wmode="opaque" quality="high" bgcolor="#000000" width="100%" height="100%" align="middle" allowScriptAccess="always" allowFullScreen="true" flashvars="vid=' + id + '" type="application/x-shockwave-flash" ></embed>'
},
iqiyi: function(id) {
return '<embed width="100%" height="100%" quality="high" allowfullscreen="true" type="application/x-shockwave-flash" flashvars="autoplay=true&definitionID=' + id + '" src="http://www.iqiyi.com/player/20140520104133/Player.swf" rel="noreferrer" allowscriptaccess="always" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">';
}
};
document.addEventListener('DOMContentLoaded', init, false);
function init() {
var z = document.querySelector('.z');
if (z) {
z.scrollIntoView();
}
var ad = document.querySelector('.ad-f');
if (ad) {
ad.style.display = 'none';
}
var title = document.getElementsByTagName('title')[0].text;
var aid_array = document.location.href.match(/av(\d+)/);
bili.aid = aid_array == null ? 'error' : aid_array[1];
console.log('aid:' + bili.aid);
if (bili.aid == 'error') {
return;
}
bili.bofqi = document.getElementById('bofqi');
if (bili.bofqi) {
var embed_array = bili.bofqi.getElementsByTagName('embed');
bili.oPlayer = embed_array.length > 0 ? embed_array[0] : false;
if (bili.oPlayer) {
var flashvars = bili.oPlayer.getAttribute('flashvars');
var cid_array = flashvars.match(/bili-cid=(\d+)/);
if (cid_array) {
console.log('存在版权播放器');
bili.cid = cid_array[1];
console.log('cid:' + bili.cid);
checkCid();
} else {
console.log('乐视源B站播放器,无需替换!');
return;
}
} else {
console.log('播放器无需替换!');
return;
}
} else {
if (title == '出错啦! - bilibili.tv') {
console.log('404啦');
errorAddTip('尝试自动搜寻其他视频源中...');
errorGetCid();
} else {
console.log('页面不存在视频内容');
return;
}
}
}
function checkCid() {
console.log('检查是否存在视频源');
GM_xmlhttpRequest({
method: 'GET',
url: 'http://interface.bilibili.com/playurl?appkey=11ecf7fd943dbc58&cid=' + bili.cid,
headers: {
'User-Agent': navigator.userAgent,
'Cookie': document.cookies,
},
onload: function(responseDetails) {
if (responseDetails.status == 200) {
var from = responseDetails.responseText.match(/<from><\!\[CDATA\[(\w+)\]\]><\/from>/);
if (from) {
var source = checkSource(from[1]);
console.log('存在' + source + '视频源,开始替换播放器');
replacePlayer(source);
} else {
addCheckTip('暂无其他视频源啦!<br/>停止替换播放器!');
console.log('不存在视频源,停止替换');
}
}
}
});
}
function checkSource(from) {
var source;
switch (from) {
case 'sina':
source = '新浪';
break;
case 'youku':
source = '优酷';
break;
case 'qq':
source = 'QQ';
break;
case 'local':
source = 'B站';
break;
case 'letv':
source = '乐视';
break;
default:
source = '不明';
}
return source;
}
function addCheckTip(str) {
var tip = document.createElement('div');
tip.id = 'check_tip';
tip.innerHTML = str;
document.body.appendChild(tip);
addStyle('#check_tip{transition: all 0.8s ease-out;background: none repeat scroll 0 0 #27a9d8;color: #FFFFFF;font: 1.1em "微软雅黑";right: -200px;margin-left: -250px;overflow: hidden;padding: 10px;position: fixed;text-align: center;bottom: 100px;z-index: 300;}');
var check_tip = document.getElementById('check_tip');
var t1 = setTimeout(function() {
check_tip.style.right = '25px';
}, 300);
var t2 = setTimeout(function() {
check_tip.style.right = '-200px';
}, 5000);
}
function createButton() {
var info = document.querySelector('.viewbox').querySelector('.info');
var btn = document.createElement('div');
btn.id = 'player_btn';
btn.onclick = function() {
playerBackControl(true);
};
btn.appendChild(document.createTextNode('取消替换'));
info.appendChild(btn);
addStyle('#player_btn {background: none repeat scroll 0 0 #27A9D8;border-radius: 5px;cursor: pointer;color: #FFFFFF;font: 1em "微软雅黑";padding: 6px;position: absolute;right: 110px;width:30px;text-align: center;top: 8px;');
}
function addStyle(css) {
var style = document.createElement('style');
style.type = 'text/css';
var node = document.createTextNode(css);
style.appendChild(node);
document.head.appendChild(style);
}
function replacePlayer(source) {
bili.bofqi.innerHTML = bili.iframe();
var player_fullwin = unsafeWindow.player_fullwin;
window.addEventListener("message", function(e) {console.log(e.data);eval(e.data.substr(6));}, false);
createButton();
addCheckTip('找到' + source + '源啦!<br/>替换播放器成功!');
console.log('播放器替换成功!');
}
function playerBackControl(status) {
console.log('backStatus:' + status);
var player_btn = document.getElementById('player_btn');
if (status) {
player_btn.onclick = function() {
playerBackControl(false);
};
player_btn.innerHTML = '重新替换';
bili.bofqi.removeChild(document.getElementById('bofqi_embed'));
bili.bofqi.appendChild(bili.oPlayer);
} else {
player_btn.onclick = function() {
playerBackControl(true);
};
player_btn.innerHTML = '取消替换';
bili.bofqi.innerHTML = bili.iframe();
}
}
/* 404页面处理 */
function errorAddTip(str) {
var isTip = document.getElementById('tip');
if (isTip) {
isTip.parentNode.removeChild(isTip);
}
var tip = document.createElement('div');
tip.id = 'tip';
tip.innerHTML = str;
document.body.appendChild(tip);
addStyle('body{text-align:center;}#tip{background: none repeat scroll 0 0 #27a9d8;color: #FFFFFF;font: 1.5em "微软雅黑";padding: 30px;position: fixed;text-align: center;top: 40%;z-index: 300;margin:0 auto;left:0;right:0;width:500px;}');
}
function errorGetCid() {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://api.bilibili.com/view?type=json&batch=1&appkey=11ecf7fd943dbc58&id=' + bili.aid,
headers: {
'User-Agent': 'bilibili toothbrush/2.6 ([email protected])',
'Cookie': document.cookies,
},
onload: function(responseDetails) {
if (responseDetails.status == 200) {
var data = JSON.parse(responseDetails.responseText);
bili.cid = data.list[0].cid;
bili.title = data.title;
console.log('cid:' + bili.cid);
console.log('视频来源:' + data.list[0].type);
if (data.list[0].type == 'iqiyi') {
errorAddiqiyi(data.list[0].vid);
} else {
errorCheckCid(data.list[0].type);
}
}
}
});
}
function errorCheckCid(vtype) {
console.log('检查是否存在视频源');
GM_xmlhttpRequest({
method: 'GET',
url: 'http://interface.bilibili.com/playurl?appkey=11ecf7fd943dbc58&cid=' + bili.cid,
headers: {
'User-Agent': navigator.userAgent,
'Cookie': document.cookies,
},
onload: function(responseDetails) {
if (responseDetails.status == 200) {
var from = responseDetails.responseText.match(/<from><\!\[CDATA\[(\w+)\]\]><\/from>/);
if (from) {
var source = checkSource(from[1]);
console.log('存在' + source + '视频源,开始替换播放器');
errorAddPlayer(source);
} else {
console.log('不存在视频源,查找其他源');
if (vtype == 'sina') {
errorGetSinaUrl();
} else if (vtype == "letv") {
errorAddTip('存在乐视源!<br/>需要挂国外代理观看!<br/>若无法观看请按CTRL+F5刷新页面!');
} else {
errorAddTip('视频被和谐啦!<br/>暂时找不到其他视频源!');
}
}
}
}
});
}
function errorAddPlayer(source) {
errorAddTip('找到' + source + '源!<br/>2秒后开始播放!');
addStyle('body{margin: 0px;padding: 0px;}');
document.getElementsByTagName('title')[0].text = bili.title;
setTimeout(function() {
document.body.innerHTML = bili.embedFull();
}, 2000);
}
function errorAddCatPlayer(id) {
errorAddTip('找到<b>无弹幕</b>视频源!<br/>5秒后自动播放!不能播放请刷新页面!<br/><b>若想观看弹幕,请挂国外代理然后刷新页面!</b>');
addStyle('body{margin: 0px;padding: 0px;}');
document.getElementsByTagName('title')[0].text = bili.title;
setTimeout(function() {
document.body.innerHTML = bili.catplayer(id);
}, 5000);
}
function errorAddiqiyi(id) {
errorAddTip('找到<b>无弹幕</b>视频源!<br/>5秒后自动播放!不能播放请刷新页面!<br/><b>若想观看弹幕,请等待B站视频源!</b>');
addStyle('body{margin: 0px;padding: 0px;}');
document.getElementsByTagName('title')[0].text = bili.title;
setTimeout(function() {
document.body.innerHTML = bili.iqiyi(id);
}, 5000);
}
function errorGetSinaUrl() {
console.log('查找视频源');
GM_xmlhttpRequest({
method: 'GET',
url: 'http://interface.bilibili.com/player?id=cid:' + bili.cid + '&aid=' + bili.aid,
headers: {
'User-Agent': navigator.userAgent,
'Cookie': document.cookies,
},
onload: function(responseDetails) {
if (responseDetails.status == 200) {
var match = responseDetails.responseText.match(/vid=(\d+)/);
if (match) {
vid = match[1];
console.log('vid:' + vid);
//预留
//errorAddCatPlayer(vid);
} else {
errorAddTip('视频被和谐啦!<br/>暂时找不到其他视频源!');
}
}
}
});
}