Skip to content

Commit 771a95c

Browse files
committed
♻️ refactor: switch only
1 parent babfd9e commit 771a95c

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

lib/index.user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
// @connect api.live.bilibili.com
1818
// @connect *
1919
// ==/UserScript==
20-
(()=>{let o=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(i,e,p=!0,a=null,l=null){let{host:n,pathname:r}=new URL(e.startsWith("//")?"https:"+e:e);switch(i){case"GET":if(n==="api.live.bilibili.com"&&r==="/xlive/app-blink/v1/live/GetWebLivePermission"){let s=Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype,"responseText")?.get;Object.defineProperty(this,"responseText",{get(){let t=JSON.parse(s?.call(this));return t.data.allow_live=!0,JSON.stringify(t)}})}break;case"POST":n==="api.live.bilibili.com"&&r==="/room/v1/Room/startLive"&&(this.send=s=>{let t=new URLSearchParams(s);t.set("platform","pc_link"),XMLHttpRequest.prototype.send.call(this,t.toString())});break}o.call(this,i,e,p,a,l)}})();
20+
(()=>{let n=XMLHttpRequest.prototype.open;XMLHttpRequest.prototype.open=function(i,t,r=!0,o=null,p=null){let{host:a,pathname:l}=new URL(t.startsWith("//")?"https:"+t:t);switch([i,a,l].toString()){case["GET","api.live.bilibili.com","/xlive/app-blink/v1/live/GetWebLivePermission"].toString():let c=Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype,"responseText")?.get;Object.defineProperty(this,"responseText",{get(){let e=JSON.parse(c?.call(this));return e.data.allow_live=!0,JSON.stringify(e)}});break;case["POST","api.live.bilibili.com","/room/v1/Room/startLive"].toString():this.send=e=>{let s=new URLSearchParams(e);s.set("platform","pc_link"),XMLHttpRequest.prototype.send.call(this,s.toString())};break}n.call(this,i,t,r,o,p)}})();

src/index.user.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,37 @@
1313
url.startsWith("//") ? "https:" + url : url,
1414
);
1515

16-
switch (method) {
17-
case "GET":
18-
if (
19-
host === "api.live.bilibili.com" &&
20-
pathname === "/xlive/app-blink/v1/live/GetWebLivePermission"
21-
) {
22-
const getter = Object.getOwnPropertyDescriptor(
23-
XMLHttpRequest.prototype,
24-
"responseText",
25-
)?.get;
16+
switch ([method, host, pathname].toString()) {
17+
case [
18+
"GET",
19+
"api.live.bilibili.com",
20+
"/xlive/app-blink/v1/live/GetWebLivePermission",
21+
].toString():
22+
const getter = Object.getOwnPropertyDescriptor(
23+
XMLHttpRequest.prototype,
24+
"responseText",
25+
)?.get;
2626

27-
Object.defineProperty(this, "responseText", {
28-
get() {
29-
const resp = JSON.parse(getter?.call(this));
30-
resp.data.allow_live = true;
31-
return JSON.stringify(resp);
32-
},
33-
});
34-
}
27+
Object.defineProperty(this, "responseText", {
28+
get() {
29+
const resp = JSON.parse(getter?.call(this));
30+
resp.data.allow_live = true;
31+
return JSON.stringify(resp);
32+
},
33+
});
3534

3635
break;
37-
case "POST":
38-
if (
39-
host === "api.live.bilibili.com" &&
40-
pathname === "/room/v1/Room/startLive"
41-
) {
42-
this.send = (body: string) => {
43-
const searchParams = new URLSearchParams(body);
36+
case [
37+
"POST",
38+
"api.live.bilibili.com",
39+
"/room/v1/Room/startLive",
40+
].toString():
41+
this.send = (body: string) => {
42+
const searchParams = new URLSearchParams(body);
4443

45-
searchParams.set("platform", "pc_link");
46-
XMLHttpRequest.prototype.send.call(this, searchParams.toString());
47-
};
48-
}
44+
searchParams.set("platform", "pc_link");
45+
XMLHttpRequest.prototype.send.call(this, searchParams.toString());
46+
};
4947

5048
break;
5149
}

0 commit comments

Comments
 (0)