Skip to content

Commit

Permalink
Bypass region restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
zacyu committed Oct 12, 2014
1 parent a58ed70 commit 18e2592
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 68 deletions.
63 changes: 44 additions & 19 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var notification = false,
playerTabs = {};
playerTabs = {},
cidHackType = {};

function getFileData(url, callback) {
xmlhttp = new XMLHttpRequest();
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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({
Expand All @@ -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,
Expand Down Expand Up @@ -278,20 +291,30 @@ 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 = {};
if (getOption("replace") == "on" && details.url.indexOf("cid=" + playerTabs[details.tabId]) > 0) {
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,
Expand All @@ -314,4 +337,6 @@ chrome.webRequest.onHeadersReceived.addListener(function(details) {
blockingResponse.responseHeaders = headers;
}
return blockingResponse;
}, {urls: ["http://www.bilibili.com/video/av*"]}, ["responseHeaders", "blocking"]);
}, {
urls: ["http://www.bilibili.com/video/av*"]
}, ["responseHeaders", "blocking"]);
129 changes: 81 additions & 48 deletions bilibili_injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand All @@ -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');
}
Expand All @@ -315,17 +376,17 @@
var redirectSection = $('<div class="section redirect"><h3>生成页选项</h3><p><a class="b-btn w" href="' + biliHelper.redirectUrl + '">前往原始跳转页</a></p></div>');
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 = $('<div class="section switcher"><h3>播放器切换</h3><p></p></div>');
switcherSection.find('p').append($('<a class="b-btn w" type="original">原始播放器</a><a class="b-btn w" type="iframe">Iframe 播放器</a><a class="b-btn w" type="swf">SWF 播放器</a><a class="b-btn w" type="html5">HTML5 播放器</a>').click(function() {
switcherSection.find('p').append($('<a class="b-btn w" type="original">原始播放器</a><a class="b-btn w" type="swf">SWF 播放器</a><a class="b-btn w" type="iframe">Iframe 播放器</a><a class="b-btn w" type="html5">HTML5 播放器</a>').click(function() {
$('.arc-tool-bar .helper .section.switcher a.b-btn').addClass('w');
biliHelper.switcher[$(this).attr('type')]();
$(this).removeClass('w');
}));
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) {
Expand All @@ -336,7 +397,8 @@
}
if (typeof biliHelper.downloadUrls !== "undefined" || biliHelper.error) {
if (typeof biliHelper.downloadUrls !== "object" || !biliHelper.downloadUrls.length) {
var downloaderSection = $('<div class="section downloder"><h3>视频下载</h3><p>视频地址获取失败</p></div>');
var errorMessage = biliHelper.error || "视频地址获取失败",
downloaderSection = $('<div class="section downloder"><h3>视频下载</h3><p><span></span>' + errorMessage + '</p></div>');
} else {
var downloaderSection = $('<div class="section downloder"><h3>视频下载</h3><p></p></div>');
for (i in biliHelper.downloadUrls) {
Expand All @@ -345,14 +407,13 @@
}
}
} else {
var downloaderSection = $('<div class="section downloder"><h3>视频下载</h3><p>视频地址获取中,请稍等…</p></div>');
var downloaderSection = $('<div class="section downloder"><h3>视频下载</h3><p><span></span>视频地址获取中,请稍等…</p></div>');
}
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') ||
Expand Down Expand Up @@ -388,7 +449,7 @@
swf: function() {
this.current = "swf";
notifyCidHack(function() {
$('#bofqi').html('<embed id="bofqi_embed" class="player" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" allowscriptaccess="always" rel="noreferrer" flashvars="cid=' + biliHelper.cid + '&aid=' + biliHelper.avid + '" src="https://static-s.bilibili.com/play.swf" type="application/x-shockwave-flash" allowfullscreen="true" quality="high">');
$('#bofqi').html('<object type="application/x-shockwave-flash" class="player" data="http://static.hdslb.com/play.swf" id="player_placeholder" style="visibility: visible;"><param name="allowfullscreeninteractive" value="true"><param name="allowfullscreen" value="true"><param name="quality" value="high"><param name="allowscriptaccess" value="always"><param name="wmode" value="opaque"><param name="flashvars" value="cid=' + biliHelper.cid + '&aid=' + biliHelper.avid + '"></object>');
});
},
iframe: function() {
Expand Down Expand Up @@ -424,7 +485,7 @@
work();
});

function work() {
var work = function() {
chrome.extension.sendMessage({
command: "getVideoInfo",
avid: biliHelper.avid,
Expand Down Expand Up @@ -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<br /><a target="_blank" href="http://www.bilibili.com/search?orderby=default&keyword=' + encodeURIComponent($(this).attr("previous")) + '">' + $(this).attr("previous") + '</a><br /><a target="_blank" href="http://www.bilibili.com/search?orderby=ranklevel&keyword=' + encodeURIComponent($(this).attr("next")) + '">' + $(this).attr("next") + '</a>', 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<br /><a target="_blank" href="http://www.bilibili.com/search?orderby=default&keyword=' + encodeURIComponent($(this).attr("previous")) + '">' + $(this).attr("previous") + '</a><br /><a target="_blank" href="http://www.bilibili.com/search?orderby=ranklevel&keyword=' + encodeURIComponent($(this).attr("next")) + '">' + $(this).attr("next") + '</a>', 1e3);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}

0 comments on commit 18e2592

Please sign in to comment.