forked from Puqns67/IHateLivehime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathihatelivehime.user.js
More file actions
312 lines (261 loc) · 10.5 KB
/
ihatelivehime.user.js
File metadata and controls
312 lines (261 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// ==UserScript==
// @name IHateLivehime
// @name:zh-CN 我讨厌直播姬
// @description 在个人直播间添加“开始直播”与“结束直播”按钮,让低粉丝数的用户也能绕开强制要求的直播姬开播。
// @match https://live.bilibili.com/*
// @icon https://i0.hdslb.com/bfs/static/jinkela/long/images/favicon.ico
// @version 0.2.0
// @author Puqns67 (with enhancements)
// @namespace https://github.com/Puqns67
// @grant GM_addStyle
// @updateURL https://github.com/Puqns67/IHateLivehime/raw/refs/heads/master/ihatelivehime.user.js
// @downloadURL https://github.com/Puqns67/IHateLivehime/raw/refs/heads/master/ihatelivehime.user.js
// @homepageURL https://github.com/Puqns67/IHateLivehime
// @supportURL https://github.com/Puqns67/IHateLivehime/issues
// ==/UserScript==
(async function () {
'use strict';
// --- 新增:自定义弹窗的CSS样式 ---
// 使用 @grant GM_addStyle 来注入CSS,这是油猴脚本的标准做法
GM_addStyle(`
.ihlh-modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
}
.ihlh-modal-content {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
width: 90%;
max-width: 500px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.ihlh-modal-title {
margin-top: 0;
margin-bottom: 15px;
font-size: 1.25rem;
color: #333;
}
.ihlh-modal-body {
margin-bottom: 20px;
}
.ihlh-modal-body textarea {
width: 100%;
min-height: 80px;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
font-family: "Consolas", "Monaco", monospace;
font-size: 14px;
resize: vertical;
}
.ihlh-modal-footer {
text-align: right;
}
.ihlh-modal-button {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-left: 10px;
}
.ihlh-button-copy {
background-color: #23ade5;
color: white;
}
.ihlh-button-close {
background-color: #f0f2f5;
color: #333;
}
`);
// --- 新增:显示可复制信息的自定义弹窗函数 ---
function showModal(title, textContent, showCopyButton = false) {
// 移除已存在的弹窗
const existingModal = document.querySelector('.ihlh-modal-overlay');
if (existingModal) {
existingModal.remove();
}
const overlay = document.createElement('div');
overlay.className = 'ihlh-modal-overlay';
const modalContent = document.createElement('div');
modalContent.className = 'ihlh-modal-content';
const modalTitle = document.createElement('h3');
modalTitle.className = 'ihlh-modal-title';
modalTitle.textContent = title;
const modalBody = document.createElement('div');
modalBody.className = 'ihlh-modal-body';
const textArea = document.createElement('textarea');
textArea.readOnly = true;
textArea.value = textContent;
modalBody.appendChild(textArea);
const modalFooter = document.createElement('div');
modalFooter.className = 'ihlh-modal-footer';
const closeButton = document.createElement('button');
closeButton.className = 'ihlh-modal-button ihlh-button-close';
closeButton.textContent = '关闭';
closeButton.onclick = () => overlay.remove();
modalFooter.appendChild(closeButton);
if (showCopyButton) {
const copyButton = document.createElement('button');
copyButton.className = 'ihlh-modal-button ihlh-button-copy';
copyButton.textContent = '复制信息';
copyButton.onclick = () => {
navigator.clipboard.writeText(textContent).then(() => {
copyButton.textContent = '已复制!';
setTimeout(() => {
copyButton.textContent = '复制信息';
}, 2000);
}).catch(err => {
console.error('复制失败: ', err);
copyButton.textContent = '复制失败';
});
};
modalFooter.insertBefore(copyButton, closeButton);
}
modalContent.appendChild(modalTitle);
modalContent.appendChild(modalBody);
modalContent.appendChild(modalFooter);
overlay.appendChild(modalContent);
document.body.appendChild(overlay);
// 点击遮罩层也可以关闭
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
overlay.remove();
}
});
}
function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function get_element_with_wait(selectors, timeout = 3200, retry_count = 32) {
let timeout_once = timeout / 32;
let retry = 1;
while (retry <= retry_count) {
let result = document.querySelector(selectors);
if (result !== null) return result;
await sleep(timeout_once);
retry++;
}
return null;
}
function get_cookie(name) {
let re = new RegExp(`(?:^|; *)${name}=([^=]+?)(?:;|$)`).exec(document.cookie);
if (re === null) {
return null;
}
return re[1];
}
async function get_current_user_info() {
return await fetch("https://api.bilibili.com/x/space/myinfo", { "credentials": "include" }).then(r => r.json());
}
async function get_room_info_by_room_id(id) {
return await fetch(`https://api.live.bilibili.com/room/v1/Room/get_info?room_id=${id}`, { "credentials": "include" }).then(r => r.json());
}
async function get_room_info_by_user_id(id) {
return await fetch(`https://api.live.bilibili.com/live_user/v1/Master/info?uid=${id}`, { "credentials": "include" }).then(r => r.json());
}
async function start_live(room_id) {
let room_info = await get_room_info_by_room_id(room_id);
if (room_info.code !== 0) {
// 修改:使用自定义弹窗
showModal(`获取房间信息失败`, `错误信息: ${room_info.message || room_info.msg}\n错误代码: ${room_info.code}\n\n${JSON.stringify(room_info)}`);
return;
}
if (room_info.data.live_status === 1) {
// 修改:使用自定义弹窗
showModal("提示", "房间已经处于开播状态!");
return;
}
let bili_jct = get_cookie("bili_jct");
if (bili_jct === null) {
// 修改:使用自定义弹窗
showModal("错误", "获取身份验证信息(bili_jct)失败!请确保您已登录B站。");
return;
}
let params = new URLSearchParams({
"room_id": room_id,
"area_v2": room_info.data.area_id,
"platform": "pc_link",
"csrf": bili_jct
}).toString();
let response = await fetch("https://api.live.bilibili.com/room/v1/Room/startLive?" + params, { "method": "POST", "credentials": "include" }).then(r => r.json());
if (response.code !== 0) {
// 修改:使用自定义弹窗
showModal(`开播失败`, `错误信息: ${response.message || response.msg}\n错误代码: ${response.code}\n\n${JSON.stringify(response)}`);
return;
}
// 修改:使用自定义弹窗,并显示复制按钮
const streamInfo = `推流地址:${response.data.rtmp.addr}\n推流密钥:${response.data.rtmp.code}`;
showModal("开播成功!", streamInfo, true);
}
async function stop_live(room_id) {
let room_info = await get_room_info_by_room_id(room_id);
if (room_info.code !== 0) {
// 修改:使用自定义弹窗
showModal(`获取房间信息失败`, `错误信息: ${room_info.message || room_info.msg}\n错误代码: ${room_info.code}\n\n${JSON.stringify(room_info)}`);
return;
}
if ([0, 2].includes(room_info.data.live_status)) {
// 修改:使用自定义弹窗
showModal("提示", "房间当前未开播!");
return;
}
let bili_jct = get_cookie("bili_jct");
if (bili_jct === null) {
// 修改:使用自定义弹窗
showModal("错误", "获取身份验证信息(bili_jct)失败!请确保您已登录B站。");
return;
}
let params = new URLSearchParams({
"room_id": room_id,
"csrf": bili_jct
}).toString();
let response = await fetch("https://api.live.bilibili.com/room/v1/Room/stopLive?" + params, { "method": "POST", "credentials": "include" }).then(r => r.json());
if (response.code !== 0) {
// 修改:使用自定义弹窗
showModal(`下播失败`, `错误信息: ${response.message || response.msg}\n错误代码: ${response.code}\n\n${JSON.stringify(response)}`);
return;
}
// 修改:使用自定义弹窗
showModal("操作成功", "已成功下播!");
}
let path_room_id = /^\/(\d+)$/.exec(document.location.pathname)
if (path_room_id === null) {
console.log("[IHateLivehime] 当前页面并非直播间!");
return;
}
let room_id = Number(path_room_id[1])
let current_user_info = await get_current_user_info()
let current_room_info = await get_room_info_by_room_id(room_id)
if (current_user_info.data.mid !== current_room_info.data.uid) {
console.log("[IHateLivehime] 当前直播间不为自己的直播间!");
return;
}
let button_area = await get_element_with_wait(".left-header-area");
if (button_area === null) {
console.log("[IHateLivehime] 页面元素不存在!");
return;
}
let start_live_button = document.createElement("button");
start_live_button.appendChild(document.createTextNode("开始直播"));
start_live_button.style.marginLeft = "10px"; // 加点间距
start_live_button.addEventListener("click", async () => start_live(room_id));
let stop_live_button = document.createElement("button");
stop_live_button.appendChild(document.createTextNode("结束直播"));
stop_live_button.style.marginLeft = "5px"; // 加点间距
stop_live_button.addEventListener("click", async () => stop_live(room_id));
button_area.appendChild(start_live_button);
button_area.appendChild(stop_live_button);
console.log("[IHateLivehime] 开/下播按钮已添加!");
}());