// ==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 // @downloadURL none // ==/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 = '脚本(`・ω・´)\ \ +10086'; $('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('分段【' + count + '】'); } //设置参数 //修复按钮事件 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('已更改,刷新生效_(:3」∠)_'); } //播放器大小按钮事件 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('已更改,刷新生效_(:3」∠)_'); } /** -------------------------------控制 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 = ' '; }else{ document.getElementById('bofqi').outerHTML = ''; } } } //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]*?<\!\[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++; } } } }); }