From b9551bb808f9f02075b85c26b137b7efcd2ceeb9 Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Thu, 4 Jan 2024 02:11:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(balh):=20=E4=BF=AE=E5=A4=8D=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E5=9C=B0=E5=8C=BA=E6=97=A0=E6=B3=95=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E4=BB=85=E9=99=90=E5=A4=A7=E9=99=86=E7=95=AA=E5=89=A7=20(#1300?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(balh): 修复其他地区无法播放仅限大陆番剧 * wip: 整理代码 --------- Co-authored-by: ipcjs --- .../src/feature/bili/area_limit_for_vue.ts | 8 ++-- .../src/feature/bili/area_limit_xhr_.ts | 42 ++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts b/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts index 6a4e5d1d..a6263db0 100644 --- a/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts +++ b/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts @@ -1,4 +1,4 @@ -import { AppSeasonInfo, BiliBiliApi } from "../../api/bilibili" +import { BiliBiliApi } from "../../api/bilibili" import { BiliPlusApi } from "../../api/biliplus" import { Converters } from "../../util/converters" import { cookieStorage } from "../../util/cookie" @@ -413,10 +413,10 @@ export function removeEpAreaLimit(ep: StringAnyObject) { ep.epRights.allow_dm = 1 } if (ep.rights) { - ep.rights.area_limit = false + ep.rights.area_limit = 0 ep.rights.allow_dm = 1 } - if (ep.badge === '受限') { + if (ep.badge === '受限' || ep.badge_info.text === '受限') { ep.badge = '' ep.badge_info = { "bg_color": "#FB7299", "bg_color_night": "#BB5B76", "text": "" } ep.badge_type = 0 @@ -505,6 +505,8 @@ export function area_limit_for_vue() { // data.initEpList.forEach(removeEpAreaLimit) // data.rights.area_limit = false // data.rights.allow_dm = 1 + } else if (data.seasonInfo.mediaInfo.episodes.length > 0) { + data.seasonInfo.mediaInfo.episodes.forEach(removeEpAreaLimit) } else if (data.seasonInfo && !data.seasonInfo.mediaInfo.rights.can_watch) { // 新版没有Playable的是预告 PV,不能直接跳过,can_watch=false 才替换 return; diff --git a/packages/unblock-area-limit/src/feature/bili/area_limit_xhr_.ts b/packages/unblock-area-limit/src/feature/bili/area_limit_xhr_.ts index d9c4af3f..99bf0c4e 100644 --- a/packages/unblock-area-limit/src/feature/bili/area_limit_xhr_.ts +++ b/packages/unblock-area-limit/src/feature/bili/area_limit_xhr_.ts @@ -4,13 +4,12 @@ import { Converters, uposMap } from '../../util/converters'; import { _ } from '../../util/react' import { Async, Promise } from '../../util/async'; import { r, _t } from '../../feature/r' -import { util_error, util_info, util_log, util_warn, util_debug, logHub } from '../../util/log' +import { util_error, util_warn, log } from '../../util/log' import { cookieStorage } from '../../util/cookie' import { balh_config, isClosed } from '../../feature/config' -import { Func } from '../../util/utils'; import { util_page } from '../../feature/page' import { access_key_param_if_exist, platform_android_param_if_app_only } from '../../api/bilibili-utils' -import { BiliPlusApi, generateMobiPlayUrlParams, getMobiPlayUrl, fixMobiPlayUrlJson, fixThailandPlayUrlJson } from '../../api/biliplus' +import { generateMobiPlayUrlParams, getMobiPlayUrl, fixMobiPlayUrlJson, fixThailandPlayUrlJson } from '../../api/biliplus' import { ui } from '../../util/ui' import { Strings } from '../../util/strings' import { util_init } from '../../util/initiator' @@ -43,23 +42,23 @@ export const area_limit_xhr = (() => { /// {@endtemplate} transformResponse: ({ url, response, xhr, container }) => { if (url.match(RegExps.url('api.bilibili.com/pgc/view/web/season?'))) { - log('/pgc/view/web/season:', xhr.responseText) let json = JSON.parse(xhr.responseText) if (json.code === 0 && json.result) { // processSeasonInfo(json.result) json.result.episodes.forEach(removeEpAreaLimit) - json.result.rights.area_limit = false + json.result.rights.area_limit = 0 + json.result.rights.ban_area_show = 0 return json } } else if (url.match(RegExps.url('bangumi.bilibili.com/view/web_api/season/user/status')) || url.match(RegExps.url('api.bilibili.com/pgc/view/web/season/user/status'))) { - log('/season/user/status:', xhr.responseText) let json = JSON.parse(xhr.responseText) let rewriteResult = false if (json.code === 0 && json.result) { areaLimit(json.result.area_limit !== 0) if (json.result.area_limit !== 0) { json.result.area_limit = 0 // 取消区域限制 + json.result.ban_area_show = 0 rewriteResult = true } if (balh_config.blocked_vip) { @@ -211,24 +210,26 @@ export const area_limit_xhr = (() => { log('/x/player/playurl', 'origin', `block: ${container.__block_response}`, xhr.response) // todo : 当前只实现了r.const.mode.REPLACE, 需要支持其他模式 // 2018-10-14: 等B站全面启用新版再说(;¬_¬) - } else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl')) + } else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl')) || url.match(RegExps.url('api.bilibili.com/pgc/player/web/v2/playurl')) && !Strings.getSearchParam(url, 'balh_ajax')) { - log('/pgc/player/web/playurl', 'origin', `block: ${container.__block_response}`, xhr.response) - if (!container.__redirect) { // 请求没有被重定向, 则需要检测结果是否有区域限制 - let json = typeof xhr.response === 'object' ? xhr.response : JSON.parse(xhr.responseText) + const reqUrl = new URL(url, document.location.href) + const isV1 = reqUrl.pathname === '/pgc/player/web/playurl' + let json = typeof xhr.response === 'object' ? xhr.response : JSON.parse(xhr.responseText) + if (!container.__redirect || (!isV1 && isAreaLimitForPlayUrl(json.result))) { // 请求没有被重定向, 则需要检测结果是否有区域限制 if (balh_config.blocked_vip || json.code || isAreaLimitForPlayUrl(json.result)) { areaLimit(true) // 2022-09-17 ipcjs: 为什么这里用的是请求url, 而不是响应url?... - let requestUrl = container.__url + let requestUrl = isV1 ? container.__url : `//api.bilibili.com/pgc/player/web/playurl${reqUrl.search}` if (isBangumiPage()) { requestUrl += `&module=bangumi` } return bilibiliApis._playurl.asyncAjax(requestUrl) .then(data => { if (!data.code) { - data = { code: 0, result: data, message: "0" } + data = isV1 + ? { code: 0, result: data, message: "0" } + : { code: 0, message: "success", result: { video_info: data } } } - log('/pgc/player/web/playurl', 'proxy', data) return data }) } else { @@ -288,7 +289,6 @@ export const area_limit_xhr = (() => { } else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl')) && !Strings.getSearchParam(url, 'balh_ajax') && needRedirect()) { - log('/pgc/player/web/playurl') // debugger container.__redirect = true // 标记该请求被重定向 if (isBangumiPage()) { @@ -297,13 +297,8 @@ export const area_limit_xhr = (() => { return bilibiliApis._playurl.asyncAjax(url) .then(data => { if (!data.code) { - data = { - code: 0, - result: data, - message: "0", - } + data = { code: 0, result: data, message: "0" } } - log('/pgc/player/web/playurl', 'proxy(redirect)', data) return data }) } @@ -337,7 +332,6 @@ export const area_limit_xhr = (() => { let oriResultTransformer let oriResultTransformerWhenProxyError let one_api; - // log(param) if (param.url.match(RegExps.urlPath('/web_api/get_source'))) { one_api = bilibiliApis._get_source; oriResultTransformer = p => p @@ -372,7 +366,6 @@ export const area_limit_xhr = (() => { param.data = undefined } if (isBangumiPage()) { - log(`playurl add 'module=bangumi' param`) param.url += `&module=bangumi` } // 加上这个参数, 防止重复拦截这个url @@ -385,7 +378,6 @@ export const area_limit_xhr = (() => { } oriResultTransformer = p => p .then(json => { - log(json) if (isNewPlayurl && !json.code) { json = json.result } @@ -536,7 +528,7 @@ export const area_limit_xhr = (() => { } function isBangumiPage() { - const mediaInfo = window.__INITIAL_STATE__?.mediaInfo + const mediaInfo = window.__INITIAL_STATE__?.mediaInfo || window.__NEXT_DATA__?.props.pageProps.dehydratedState?.queries[0]?.state.data.seasonInfo?.mediaInfo return isBangumi(mediaInfo?.season_type || mediaInfo?.ssType) } @@ -606,7 +598,7 @@ export const area_limit_xhr = (() => { } function isAreaLimitForPlayUrl(json) { - return (json.errorcid && json.errorcid == '8986943') || (json.durl && json.durl.length === 1 && json.durl[0].length === 15126 && json.durl[0].size === 124627); + return (json.errorcid && json.errorcid == '8986943') || (json.durl && json.durl.length === 1 && json.durl[0].length === 15126 && json.durl[0].size === 124627) || !json.video_info; } var bilibiliApis = (function () {