bilibili toothbrush

牙刷科技,还原哔哩哔哩播放器

当前为 2014-05-17 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        bilibili toothbrush
// @namespace   http://www.icycat.com
// @description 牙刷科技,还原哔哩哔哩播放器
// @include     *www.bilibili.tv/video/av*
// @include     *bilibili.kankanews.com/video/av*
// @version     2.0
// @grant       GM_xmlhttpRequest
// @run-at      document-start
// ==/UserScript==

var aid, cid, bofqi, player;

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+)/);
    aid = aid_array == null ? 'error' : aid_array[1];
    console.log('aid:' + aid);
    if (aid == 'error') {
        return;
    }  
    bofqi = document.getElementById('bofqi');
    if(bofqi){
        var embed_array = bofqi.getElementsByTagName('embed');
        player = embed_array.length > 0 ? embed_array[0] : false;
        if (player) {
            console.log('存在版权播放器');
            var flashvars = player.getAttribute('flashvars');
            var cid_array = flashvars.match(/cid=(\d+)/);
            cid = cid_array == null ? 'error' : cid_array[1];
            console.log('cid:' + cid);
            if (cid == 'error') {
                return;
            } else {
                checkCid();
            }
        } else {
            console.log('播放器无需替换!');
            return;
        }
    } else {
        if (title == '出错啦! - bilibili.tv') {
        console.log('404啦');
        } else {
            console.log('页面不存在视频内容');
            return;
        }
    }
}

function checkCid() {
    console.log('检查是否存在视频源');
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://interface.bilibili.tv/playurl?cid=' + 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;
                    switch (from[1]) {
                        case 'sina':
                            source = '新浪';
                            break;
                        case 'youku':
                            source = '优酷';
                            break;
                        case 'qq':
                            source = 'QQ';
                            break;
                        case 'local':
                            source = 'B站';
                            break;
                        case 'letv':
                            source = '乐视';
                            break;
                        default:
                            source = '不明';
                    }
                    console.log('存在' + source + '视频源,开始替换播放器');
                    replacePlayer(source);
                } else {
                    addCheckTip('暂无其他视频源啦!<br/>停止替换播放器!');
                    console.log('不存在视频源,停止替换');
                }
            }
        }
    });
}

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 "微软雅黑";height: 35px;right: -200px;margin-left: -250px;overflow: hidden;padding: 5px;position: fixed;text-align: center;bottom: 100px;width: 132px;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;text-align: center;top: 8px;width: 30px;');
}

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) {
    bofqi.innerHTML = '<embed id="bofqi_embed" width="950" height="482" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowFullScreenInteractive="true" flashvars="cid=' + cid + '&aid=' + aid + '" src="https://static-s.bilibili.tv/play.swf" type="application/x-shockwave-flash" AllowScriptAccess="always" allowfullscreen="true" quality="high" />';
    var fix_fullwin = unsafeWindow.player_fullwin;
    unsafeWindow.player_fullwin = function(status) {
        fix_fullwin(status);
        var bofqi_embed = document.getElementById('bofqi_embed');
        console.log('网页全屏' + status);
        if (status) {
            bofqi_embed.style.width = '100%';
            bofqi_embed.style.height = '100%';
        } else {
            bofqi_embed.removeAttribute('style');
        }
    }
    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 = '重新替换';
        bofqi.removeChild(document.getElementById('bofqi_embed'));
        bofqi.appendChild(player);
    } else {
        player_btn.onclick = function() {
            playerBackControl(true);
        };
        player_btn.innerHTML = '取消替换';
        bofqi.innerHTML = '<embed id="bofqi_embed" width="950" height="482" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowFullScreenInteractive="true" flashvars="cid=' + cid + '&aid=' + aid + '" src="https://static-s.bilibili.tv/play.swf" type="application/x-shockwave-flash" AllowScriptAccess="always" allowfullscreen="true" quality="high" />';
    }
}