From 18e259200e5250f125f0666a79611715779fe35f Mon Sep 17 00:00:00 2001 From: Zhuogu Date: Sun, 12 Oct 2014 00:04:43 -0700 Subject: [PATCH] Bypass region restrictions --- background.js | 63 ++++++++++++++------- bilibili_injected.js | 129 +++++++++++++++++++++++++++---------------- manifest.json | 2 +- 3 files changed, 126 insertions(+), 68 deletions(-) diff --git a/background.js b/background.js index 36ed77a..3b0f532 100755 --- a/background.js +++ b/background.js @@ -1,5 +1,6 @@ var notification = false, - playerTabs = {}; + playerTabs = {}, + cidHackType = {}; function getFileData(url, callback) { xmlhttp = new XMLHttpRequest(); @@ -12,17 +13,16 @@ function getFileData(url, callback) { xmlhttp.send(); } -function getUrlVars(url) -{ - var vars = [], hash; - var hashes = url.slice(url.indexOf('?') + 1).split('&'); - for(var i = 0; i < hashes.length; i++) - { - hash = hashes[i].split('='); - vars.push(hash[0]); - vars[hash[0]] = hash[1]; - } - return vars; +function getUrlVars(url) { + var vars = [], + hash; + var hashes = url.slice(url.indexOf('?') + 1).split('&'); + for (var i = 0; i < hashes.length; i++) { + hash = hashes[i].split('='); + vars.push(hash[0]); + vars[hash[0]] = hash[1]; + } + return vars; } function searchBilibili(info) { @@ -140,6 +140,9 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { return true; case "cidHack": playerTabs[sender.tab.id] = request.cid; + cidHackType[request.cid] = request.type; + sendResponse(); + return true; case "getOption": sendResponse({ value: getOption(request.key) @@ -175,7 +178,17 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { }); return true; case "getDownloadLink": - getFileData("http://interface.bilibili.com/playurl?platform=bilihelper&otype=json&appkey=95acd7f6cc3392f3&cid=" + request.cid + "&quality=4&type=" + getOption("dlquality"), function(avDownloadLink) { + var url = { + download: "http://interface.bilibili.com/playurl?platform=bilihelper&otype=json&appkey=95acd7f6cc3392f3&cid=" + request.cid + "&quality=4&type=" + getOption("dlquality"), + playback: "http://interface.bilibili.com/playurl?platform=bilihelper&otype=json&appkey=95acd7f6cc3392f3&cid=" + request.cid + "&quality=4&type=mp4" + } + if (request.cidHack == 2) { + var url = { + download: "https://bilibili.guguke.net/playurl.json?cid=" + request.cid + "&type=" + getOption("dlquality"), + playback: "https://bilibili.guguke.net/playurl.json?cid=" + request.cid + "&type=mp4" + } + } + getFileData(url["download"], function(avDownloadLink) { avDownloadLink = JSON.parse(avDownloadLink); if (getOption("dlquality") == 'mp4' && avDownloadLink.from != 'qq') { sendResponse({ @@ -185,7 +198,7 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { rel_search: getOption("rel_search") }); } else { - getFileData("http://interface.bilibili.com/playurl?platform=bilihelper&otype=json&appkey=95acd7f6cc3392f3&cid=" + request.cid + "&quality=4&type=mp4", function(avPlaybackLink) { + getFileData(url["playback"], function(avPlaybackLink) { avPlaybackLink = JSON.parse(avPlaybackLink); sendResponse({ download: avDownloadLink.from == 'qq' ? avPlaybackLink : avDownloadLink, @@ -278,8 +291,12 @@ chrome.alarms.onAlarm.addListener(function(alarm) { }); chrome.webRequest.onBeforeRequest.addListener(function(details) { - chrome.tabs.sendMessage(details.tabId, {command: "error"}); -}, {urls: ["http://comment.bilibili.com/1272.xml"]}); + chrome.tabs.sendMessage(details.tabId, { + command: "error" + }); +}, { + urls: ["http://comment.bilibili.com/1272.xml"] +}); chrome.webRequest.onHeadersReceived.addListener(function(details) { var blockingResponse = {}; @@ -287,11 +304,17 @@ chrome.webRequest.onHeadersReceived.addListener(function(details) { playerTabs[details.tabId] = false; var params = getUrlVars(details.url); if (params['cid']) { - blockingResponse.redirectUrl = 'http://interface.bilibili.com/playurl?platform=bilihelper&cid=' + params['cid'] + '&appkey=95acd7f6cc3392f3'; + if (cidHackType[params['cid']] == 1) { + blockingResponse.redirectUrl = 'http://interface.bilibili.com/playurl?platform=bilihelper&cid=' + params['cid'] + '&appkey=95acd7f6cc3392f3'; + } else if (cidHackType[params['cid']] == 2) { + blockingResponse.redirectUrl = 'https://bilibili.guguke.net/playurl.xml?cid=' + params['cid']; + } } } return blockingResponse; -}, {urls: ["http://interface.bilibili.com/playurl?cid*", "http://interface.bilibili.com/playurl?accel=1&cid=*"]}, ["blocking"]); +}, { + urls: ["http://interface.bilibili.com/playurl?cid*", "http://interface.bilibili.com/playurl?accel=1&cid=*"] +}, ["blocking"]); chrome.webRequest.onHeadersReceived.addListener(function(details) { var headers = details.responseHeaders, @@ -314,4 +337,6 @@ chrome.webRequest.onHeadersReceived.addListener(function(details) { blockingResponse.responseHeaders = headers; } return blockingResponse; -}, {urls: ["http://www.bilibili.com/video/av*"]}, ["responseHeaders", "blocking"]); \ No newline at end of file +}, { + urls: ["http://www.bilibili.com/video/av*"] +}, ["responseHeaders", "blocking"]); \ No newline at end of file diff --git a/bilibili_injected.js b/bilibili_injected.js index abafe53..1ab95cf 100755 --- a/bilibili_injected.js +++ b/bilibili_injected.js @@ -58,7 +58,8 @@ if (biliHelper.cidHack) { chrome.extension.sendMessage({ command: "cidHack", - cid: biliHelper.cid + cid: biliHelper.cid, + type: biliHelper.cidHack }, function(response) { if (typeof callback === 'function') callback(); }); @@ -254,10 +255,17 @@ mode: adMode(request.css) }); return true; + case "copyright": + biliHelper.copyright = true; + return true; case "error": - if (!biliHelper.cidHack) { - biliHelper.cidHack = true; + if (biliHelper.cidHack == 0) { + biliHelper.cidHack = 1; + biliHelper.switcher[biliHelper.switcher.current](); + } else if (biliHelper.cidHack == 1 && biliHelper.copyright) { + biliHelper.cidHack = 2; biliHelper.switcher[biliHelper.switcher.current](); + finishUp(); } return true; default: @@ -268,14 +276,59 @@ } }); + var finishUp = function() { + chrome.extension.sendMessage({ + command: "getDownloadLink", + cid: biliHelper.cid, + cidHack: biliHelper.cidHack + }, function(response) { + var videoDownloadLink = response["download"], + videoPlaybackLink = response["playback"]; + biliHelper.downloadUrls = []; + biliHelper.playbackUrls = []; + if (videoDownloadLink.result == "error") { + if (typeof videoDownloadLink.message == "string") { + if (videoDownloadLink.message.indexOf("地区") > -1) { + biliHelper.copyright = true; + } + biliHelper.error = '错误: ' + videoDownloadLink.message; + } + } else { + if (typeof videoDownloadLink.durl["url"] === "undefined") { + biliHelper.downloadUrls = videoDownloadLink.durl; + } else { + biliHelper.downloadUrls.push(videoDownloadLink.durl); + } + if (typeof videoPlaybackLink.durl["url"] === "undefined") { + biliHelper.playbackUrls = videoPlaybackLink.durl; + } else { + biliHelper.playbackUrls.push(videoPlaybackLink.durl); + } + $('#loading-notice').fadeOut(300); + if (biliHelper.favorHTML5 && biliHelper.cid && biliHelper.playbackUrls && biliHelper.playbackUrls.length == 1 && biliHelper.playbackUrls[0].url.indexOf('m3u8') < 0) { + $('#loading-notice').fadeOut(300, function() { + biliHelper.switcher.html5(); + }); + } else if (biliHelper.replacePlayer) { + $('#loading-notice').fadeOut(300, function() { + biliHelper.switcher.swf(); + }); + } else { + $('#loading-notice').fadeOut(300); + } + } + }); + } + var biliHelperFunc = function() { intilize_style(); $("html").addClass("bilibili-helper"); var bili_reg = /\/video\/av([0-9]+)\/(?:index_([0-9]+)\.html)?$/, - urlResult = bili_reg.exec(document.URL); + urlResult = bili_reg.exec(document.URL.split('#')[0]); if (urlResult) { biliHelper.avid = urlResult[1]; biliHelper.page = urlResult[2]; + biliHelper.cidHack = 0; if (typeof biliHelper.page === "undefined") { biliHelper.page = 1; } else { @@ -286,10 +339,18 @@ command: "init" }, function(response) { biliHelper.genPage = false; + biliHelper.copyright = false; if (!$('.z').length) { biliHelper.genPage = true; biliHelper.redirectUrl = decodeURIComponent(__GetCookie('redirectUrl')); } + if ($('.z .z-msg').length > 0 && $('.z .z-msg').text().indexOf('版权') > -1) { + biliHelper.genPage = true; + biliHelper.copyright = true; + } + if ($('#bofqi div') > 0 && $('#bofqi div').text().indexOf('版权') > -1) { + biliHelper.copyright = true; + } if ($('meta[name="redirect"]').length) { biliHelper.redirectUrl = $('meta[name="redirect"]').attr('content'); } @@ -315,9 +376,9 @@ var redirectSection = $('

生成页选项

前往原始跳转页

'); main.append(redirectSection); } - if (biliHelper.cid && biliHelper.playbackUrls && biliHelper.playbackUrls.length == 1 && biliHelper.playbackUrls[0].url.indexOf('m3u8') < 0 || biliHelper.replacePlayer && typeof biliHelper.error === "undefined") { + if (biliHelper.cid && biliHelper.playbackUrls && biliHelper.playbackUrls.length == 1 && biliHelper.playbackUrls[0].url.indexOf('m3u8') < 0 || biliHelper.replacePlayer && typeof biliHelper.cid !== "undefined") { var switcherSection = $('

播放器切换

'); - switcherSection.find('p').append($('原始播放器Iframe 播放器SWF 播放器HTML5 播放器').click(function() { + switcherSection.find('p').append($('原始播放器SWF 播放器Iframe 播放器HTML5 播放器').click(function() { $('.arc-tool-bar .helper .section.switcher a.b-btn').addClass('w'); biliHelper.switcher[$(this).attr('type')](); $(this).removeClass('w'); @@ -325,7 +386,7 @@ if (biliHelper.redirectUrl) { switcherSection.find('a[type="original"]').remove(); } - if (!biliHelper.replacePlayer || biliHelper.error) { + if (!biliHelper.replacePlayer || !biliHelper.cid) { switcherSection.find('a[type="iframe"],a[type="swf"]').remove(); } if (!biliHelper.cid || !biliHelper.playbackUrls || biliHelper.playbackUrls.length != 1 || biliHelper.playbackUrls[0].url.indexOf('m3u8') >= 0) { @@ -336,7 +397,8 @@ } if (typeof biliHelper.downloadUrls !== "undefined" || biliHelper.error) { if (typeof biliHelper.downloadUrls !== "object" || !biliHelper.downloadUrls.length) { - var downloaderSection = $('

视频下载

视频地址获取失败

'); + var errorMessage = biliHelper.error || "视频地址获取失败", + downloaderSection = $('

视频下载

' + errorMessage + '

'); } else { var downloaderSection = $('

视频下载

'); for (i in biliHelper.downloadUrls) { @@ -345,14 +407,13 @@ } } } else { - var downloaderSection = $('

视频下载

视频地址获取中,请稍等…

'); + var downloaderSection = $('

视频下载

视频地址获取中,请稍等…

'); } main.append(downloaderSection); blockInfo.addClass('active'); } }); if (!biliHelper.genPage) $('.player-wrapper .arc-tool-bar').append(helperBlock); - biliHelper.originalPlayer = $('#bofqi').html(); if (response.replace == "on" && ($('#bofqi object').length > 0 && $('#bofqi object').attr('data') != 'http://static.hdslb.com/play.swf' && $('#bofqi object').attr('data') != 'https://static-s.bilibili.com/play.swf' && $('#bofqi object').attr('data') != 'http://static.hdslb.com/letv.swf' && $('#bofqi object').attr('data') != 'http://static.hdslb.com/play_old.swf') || ($('#bofqi embed').length > 0 && $('#bofqi embed').attr('src') != 'http://static.hdslb.com/play.swf' && $('#bofqi embed').attr('src') != 'https://static-s.bilibili.com/play.swf' && $('#bofqi embed').attr('src') != 'http://static.hdslb.com/letv.swf' && $('#bofqi embed').attr('src') != 'http://static.hdslb.com/play_old.swf') || @@ -388,7 +449,7 @@ swf: function() { this.current = "swf"; notifyCidHack(function() { - $('#bofqi').html(''); + $('#bofqi').html(''); }); }, iframe: function() { @@ -424,7 +485,7 @@ work(); }); - function work() { + var work = function() { chrome.extension.sendMessage({ command: "getVideoInfo", avid: biliHelper.avid, @@ -497,48 +558,20 @@ if (biliHelper.cid && !biliHelper.favorHTML5) { $('#loading-notice').fadeOut(300, function() { - biliHelper.switcher.iframe(); + biliHelper.switcher.swf(); }); } if (!biliHelper.cid) { biliHelper.error = '错误' + videoInfo.code + ': ' + videoInfo.error; return false; } - chrome.extension.sendMessage({ - command: "getDownloadLink", - cid: biliHelper.cid, - }, function(response) { - var videoDownloadLink = response["download"], - videoPlaybackLink = response["playback"]; - biliHelper.downloadUrls = []; - biliHelper.playbackUrls = []; - if (typeof videoDownloadLink.durl["url"] === "undefined") { - biliHelper.downloadUrls = videoDownloadLink.durl; - } else { - biliHelper.downloadUrls.push(videoDownloadLink.durl); - } - if (typeof videoPlaybackLink.durl["url"] === "undefined") { - biliHelper.playbackUrls = videoPlaybackLink.durl; - } else { - biliHelper.playbackUrls.push(videoPlaybackLink.durl); - } - $('#loading-notice').fadeOut(300); - if (biliHelper.favorHTML5 && biliHelper.cid && biliHelper.playbackUrls && biliHelper.playbackUrls.length == 1 && biliHelper.playbackUrls[0].url.indexOf('m3u8') < 0) { - $('#loading-notice').fadeOut(300, function() { - biliHelper.switcher.html5(); - }); - } else if (biliHelper.replacePlayer) { - $('#loading-notice').fadeOut(300, function() { - biliHelper.switcher.iframe(); - }); - } else { - $('#loading-notice').fadeOut(300); - } - $('.viewbox .info h2').html(addTitleLink($('.viewbox .info h2').attr('title'), response.rel_search)); - $(".titleNumber").click(function() { - var msgbox = new MessageBox; - msgbox.show(this, '\u70b9\u51fb\u641c\u7d22\u76f8\u5173\u89c6\u9891\uff1a
' + $(this).attr("previous") + '
' + $(this).attr("next") + '', 1e3); - }); + + finishUp(); + + $('.viewbox .info h2').html(addTitleLink($('.viewbox .info h2').attr('title'), response.rel_search)); + $(".titleNumber").click(function() { + var msgbox = new MessageBox; + msgbox.show(this, '\u70b9\u51fb\u641c\u7d22\u76f8\u5173\u89c6\u9891\uff1a
' + $(this).attr("previous") + '
' + $(this).attr("next") + '', 1e3); }); }); } diff --git a/manifest.json b/manifest.json index 84218e0..92cd255 100755 --- a/manifest.json +++ b/manifest.json @@ -27,6 +27,6 @@ "options_page": "options.html", "permissions": [ "alarms", "contextMenus", "cookies", "notifications", "storage", "tabs", "webRequest", "webRequestBlocking", "*://*.bilibili.com/*" ], "update_url": "https://clients2.google.com/service/update2/crx", - "version": "0.6.4", + "version": "0.6.5", "web_accessible_resources": [ "bilibili-helper.woff", "template.html", "imgs/icon-32.png", "imgs/loading.gif" ] }