Skip to content

Commit f0238bf

Browse files
committed
feat: 增加bili-live相关脚本
1 parent 1f52ac0 commit f0238bf

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| [bilixs](/packages/bilixs) | 优化 bilixs 体验 | [GitHub](https://github.com/IronKinoko/userscripts/raw/dist/bilixs.user.js) |
1919
| [novel-speech-synthesis](/packages/novel-speech-synthesis) | 小说语音阅读功能 | [GitHub](https://github.com/IronKinoko/userscripts/raw/dist/novel-speech-synthesis.user.js) |
2020
| [steam-multisell](/packages/steam-multisell) | Steam 批量出售 | [GitHub](https://github.com/IronKinoko/userscripts/raw/dist/steam-multisell.user.js) |
21+
| [bili-live](/packages/bili-live) | bilibili 直播间优化 | [GitHub](https://github.com/IronKinoko/userscripts/raw/dist/bili-live.user.js) |
2122

2223
## iOS & Mac safari install
2324

packages/bili-live/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# bili-live
2+
3+
## Install/安装
4+
5+
[Click here/点此](https://github.com/IronKinoko/userscripts/raw/dist/bili-live.user.js)

packages/bili-live/meta.template

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ==UserScript==
2+
// @name bili-live
3+
// @namespace https://github.com/IronKinoko/userscripts/tree/master/packages/bili-live
4+
// @version #version#
5+
// @license MIT
6+
// @description #description#
7+
// @author IronKinoko
8+
// @match https://link.bilibili.com/*
9+
// @icon https://www.bilibili.com/favicon.ico
10+
// @grant none
11+
// @downloadURL https://github.com/IronKinoko/userscripts/raw/dist/bili-live.user.js
12+
// @updateURL https://github.com/IronKinoko/userscripts/raw/dist/bili-live.user.js
13+
// ==/UserScript==

packages/bili-live/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "bili-live",
3+
"version": "1.0.0",
4+
"description": "优化直播间开播设置",
5+
"main": "index.js",
6+
"scripts": {
7+
"dev": "bocchi dev",
8+
"build": "bocchi build"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/IronKinoko/userscripts.git"
13+
},
14+
"keywords": [],
15+
"author": "IronKinoko <[email protected]>",
16+
"license": "MIT",
17+
"bugs": {
18+
"url": "https://github.com/IronKinoko/userscripts/issues"
19+
},
20+
"homepage": "https://github.com/IronKinoko/userscripts/tree/master/packages/bili-live#readme",
21+
"devDependencies": {
22+
"shared": "workspace:*"
23+
}
24+
}

packages/bili-live/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { router } from 'shared'
2+
import * as startLive from './modules/startLive'
3+
4+
router({
5+
domain: 'link.bilibili.com',
6+
routes: [
7+
{ path: '/p/center/index', setup: startLive.setup, run: startLive.run },
8+
],
9+
})
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export function setup() {
2+
hookXHR()
3+
}
4+
export function run() {}
5+
6+
declare global {
7+
interface XMLHttpRequest {
8+
_requestURL: string
9+
_requestMethod: string
10+
}
11+
}
12+
13+
function hookXHR() {
14+
const originalOpen = XMLHttpRequest.prototype.open
15+
const originalSend = XMLHttpRequest.prototype.send
16+
17+
// @ts-ignore
18+
XMLHttpRequest.prototype.open = function (method, url: string, ...rest) {
19+
this._requestURL = url
20+
this._requestMethod = method
21+
// @ts-ignore
22+
return originalOpen.call(this, method, url, ...rest)
23+
}
24+
25+
XMLHttpRequest.prototype.send = function (body) {
26+
const xhr = this
27+
const url = this._requestURL
28+
const method = this._requestMethod
29+
if (url && url.includes('GetWebLivePermission')) {
30+
const originalOnReadyStateChange = xhr.onreadystatechange
31+
xhr.onreadystatechange = function () {
32+
if (xhr.readyState === 4) {
33+
try {
34+
if (xhr.status >= 200 && xhr.status < 300) {
35+
let responseData = JSON.parse(xhr.responseText)
36+
if (responseData.data) {
37+
responseData.data.allow_live = true
38+
}
39+
40+
Object.defineProperty(xhr, 'responseText', {
41+
get: function () {
42+
return JSON.stringify(responseData)
43+
},
44+
})
45+
}
46+
} catch (e) {
47+
console.error('修改GetWebLivePermission返回值出错:', e)
48+
}
49+
}
50+
51+
if (originalOnReadyStateChange) {
52+
// @ts-ignore
53+
originalOnReadyStateChange.apply(this, arguments)
54+
}
55+
}
56+
}
57+
58+
if (url && url.includes('/room/v1/Room/startLive')) {
59+
const isString = typeof body === 'string'
60+
const params = isString
61+
? new URLSearchParams(body)
62+
: (body as URLSearchParams)
63+
params.set('platform', 'mobile')
64+
arguments[0] = isString ? params.toString() : params
65+
}
66+
// @ts-ignore
67+
return originalSend.apply(this, arguments)
68+
}
69+
}

packages/bili-live/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src", "../../env.d.ts"]
4+
}

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)