bili_fix_player

修复B站播放器,黑科技

От 10.05.2014. Виж последната версия.

// ==UserScript==
// @name        bili_fix_player
// @namespace   bili
// @description 修复B站播放器,黑科技
// @include     *www.bilibili.tv/video/*
// @include     *bilibili.kankanews.com/video/*
// @version     3.3
// @grant       GM_xmlhttpRequest
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_addStyle
// ==/UserScript==
/**
出现无法播放情况先关闭自动修复
2013-12-14修复B站播放器无法在火狐魔镜弹窗播放
2014-01-23替换优酷、爱奇艺、搜狐为B站播放器
2014-03-28增加下载视频按钮
2014-05-10收益于自己的B站追番计划(http://v.myacg.ga或者http://weiyun.jd-app.com),代码逻辑重构(不再区分视频源再解析视频),并重写UI
------------以下信息提供给开发者-----------
//https://static-s.bilibili.tv/play.swf---新版播放器
//http://static.hdslb.com/play.swf---旧版播放器
//https://static-s.bilibili.tv/play_old.swf---考古级别播放器
-------------------------------------------
*/
//初始化 init
if (GM_getValue('auto') == undefined) GM_setValue('auto', 1);
if (GM_getValue('player_size') == undefined) GM_setValue('player_size', 1);
//初始化jquery支持
var $=unsafeWindow.$;
/**
-------------------------------用户界面GUI View-------------------------------------
*/
//函数,插入可视化操作视图
function insert_html(type) {
var auto= GM_getValue('auto') ? '已打开' : '已关闭';
var player_size= GM_getValue('player_size') ? '大型' : '小型';
var div  = '<a style="color:red">脚本(`・ω・´)</a>\
							<ul class="i_num" id="bili_fix_script">\
								<li><a class="font">遇到播放错误请关闭自动修复后刷新页面</a></li>\
								<li><a>本页视频源:<b style="color:#F489AD">' + type + '</b></a></li>\
								<li><a class="font">视频下载(右键复制以下视频分段下载链接,然后在新标签粘贴打开即可不被403)</a><div class="m_num" id="av_source">\
									</div></li>\
								<li><a>自动修复(修改后请刷新页面):<a id="bili_fix" class="btn">' + auto + '</a></a></li>\
								<li><a class="font">播放器大小(小型在火狐弹窗无BUG):<a id="player_size" class="btn">' + player_size+ '</a></a></li>\
								<li><a id="bili_set_status">就绪中→_→</a></li>\
							</ul>\
							<span class="addnew_5">+10086</span>';
$('div.num:nth-child(4) > ul:nth-child(1) > li:nth-child(1)').html(div);
var css = '.btn{font-size: 12px;height: 25.6px;line-height: 25.6px;padding: 0px 2px;transition-property: #000, color;\
transition-duration: 0.3s;\
box-shadow: none;\
color: #FFF;\
text-shadow: none;\
border: medium none;\
background: none repeat scroll 0% 0% #00A1CB!important;}\
.btn.active{\
background: none repeat scroll 0% 0%  #F489AD!important;}\
.btn.notice{\
background-color:#A300C0!important;}\
.font{\
font-size:11px!important;}\
';
	GM_addStyle(css);

	//监听修复按钮
	var btn = document.querySelector("#bili_fix");
	btn.addEventListener("click", set_auto, false); 
	//监听播放器大小按钮
	var btn = document.querySelector("#player_size");
	btn.addEventListener("click", set_player, false); 
}

//函数,插入下载按钮
function insert_download_button(url, count) {
	$('#av_source').append('<a href="' + url + '" target="blank">分段【' + count + '】</a>');
}


//设置参数
//修复按钮事件
function set_auto() {
	GM_getValue('auto') ? GM_setValue('auto', 0) : GM_setValue('auto', 1);
	var s = GM_getValue('auto') ? '已打开' : '已关闭';
	document.getElementById('bili_fix').innerHTML = s;
	$("#bili_fix").toggleClass("active");
	$('#bili_set_status').html('<a class="btn notice font">已更改,刷新生效_(:3」∠)_</a>');
}
//播放器大小按钮事件
function set_player() {
	GM_getValue('player_size') ? GM_setValue('player_size', 0) : GM_setValue('player_size', 1);
	var s = GM_getValue('player_size') ? '大型' : '小型';
	document.getElementById('player_size').innerHTML = s;
	$("#player_size").toggleClass("active");
	$('#bili_set_status').html('<a class="btn active font">已更改,刷新生效_(:3」∠)_</a>');
}
/**
-------------------------------控制 Control-------------------------------------
*/
//取出aid和分P
var url = document.location.href;
var aid_reg = /\/av(.{6,10})\/(?:index_(.)\.html)?/ig;
var aid_array = aid_reg.exec(url); 

var aid=typeof(aid_array)===null?'':aid_array[1];//aid
var page=typeof(aid_array)===null?'1':typeof(aid_array[2])=='undefined'?'1':aid_array[2];//分p

//播放器的html
var content = document.getElementById('bofqi').innerHTML;
api_get_cid(aid,page);//按照aid和分p获取cid并且替换播放器

/**
-------------------------------函数 Model-------------------------------------
*/
//函数,替换播放器
function Replace_player(aid, cid) {
	if (GM_getValue('auto') == '1') {
	if (GM_getValue('player_size') == '1') {
		document.getElementById('bofqi').innerHTML = '<iframe class="player" src="https://secure.bilibili.tv/secure,cid='+cid+'&amp;aid='+aid+'" scrolling="no" border="0" framespacing="0" onload="window.securePlayerFrameLoaded=true" frameborder="no" height="482" width="950"></iframe> ';
		}else{
				document.getElementById('bofqi').outerHTML = '<embed id="bofqi_embed" class="player" allowfullscreeninteractive="true" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowscriptaccess="always" rel="noreferrer" flashvars="cid=' + cid + '&amp;aid=' + aid + '" src="http://static.hdslb.com/play.swf" type="application/x-shockwave-flash" allowfullscreen="true" quality="high" wmode="window" height="482" width="950">';
		}
	}
}
//api获取cid
function api_get_cid(aid,page) {
	var url = 'http://api.bilibili.tv/view?type=json&appkey=0a99fa1d87fdd38c&batch=1&id=' + aid;
	GM_xmlhttpRequest({
		method: 'GET',
		url: url,
		synchronous: false,
		onload: function(responseDetails) {
			if (responseDetails.status == 200) {
				var Content = eval('(' + responseDetails.responseText + ')');
				var list = Content.list;
				var p=page-1;
				var cid=list[p].cid;
				var type=list[p].type;
				insert_html(type); //UI
				Replace_player(aid, cid);//替换播放器 
				cid_get_videodown(cid);//获取下载链接
			}
		}
	});
}
//cid获取视频链接
function cid_get_videodown(cid) {
	var url = 'http://interface.bilibili.cn/playurl?cid=' + cid;
	GM_xmlhttpRequest({
		method: 'GET',
		url: url,
		synchronous: false,
		onload: function(responseDetails) {
			if (responseDetails.status == 200) {
				var pattern = /<\/(?:(?:chunk)?size|length)>[\s\n]*?<url><\!\[CDATA\[(.*?)\]\]><\/url>/ig;
				var c = 1;
				//console.log(responseDetails.responseText);
				while (content = pattern.exec(responseDetails.responseText)) {
					var url = content ? (content[1]) : 'http://interface.bilibili.cn/playurl?cid=' + cid;
					insert_download_button(url, c);
					c++;
				}
			}
		}
	});
}