-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
229 lines (209 loc) · 8.59 KB
/
user.js
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
// 插入侧栏与页面
const userPage = ExtensionFunctions.insertNavigationItem({
pageId: "userPage",
icon: "EE08",
text: "我的",
appendBefore: "searchPage",
});
ExtensionFunctions.insertStyle(`
#userPage #qrcodeContainer {
min-width: 40%;
}
#userPage #userPageInfo {
display: none;
}
#userPage .userPageMain {
display: grid;
place-items: center;
height: 100vh;
margin-left: auto;
margin-right: auto;
position: relative;
width: calc(100% - 40px);
max-width: 520px;
}
#userPage .userPageMod {
display:flex;
border-radius: 5px;
box-sizing: border-box;
background-color: rgba(255, 255, 255, .6);
backdrop-filter: blur(30px);
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
max-height: calc(100vh - 25px);
overflow: auto;
height: auto;
width: 100%;
padding: 30px;
}
`)
userPage.pageDiv.classList.add("page");
userPage.pageDiv.innerHTML = `
<div class="userPageMain">
<div class="userPageMod">
<div id="loginForm" style="margin-right: 20px;">
<h2>登录云音乐</h2>
<input type="email" id="email" placeholder="邮箱">
<input type="password" id="password" placeholder="密码">
<button id="loginButton" style="margin-top: 15px;">登录</button>
</div>
<div id="qrcodeContainer">
<div id="qrcode" style="width: 100%;"></div>
<p style="
margin: 0;
padding-left: 20px;
">
手机扫描二维码登录
</p>
</div>
<div id="userInfo" style="text-align: center;">
<h2><i></i> 网易云音乐</h2>
<img id="avatar" src="" alt="用户头像" width="30%" style="border-radius: 10em;">
<p><span style="font-size: 24px;font-weight: bold;" id="username"></span></p>
<button id="myPlaylistsButton" class="sub" style="margin-top: 15px;">我的歌单</button>
<button id="myMusicsButton" class="sub" style="margin-top: 15px;">我的收藏</button>
<button id="logoutButton" class="sub" style="margin-top: 15px;">退出登录</button>
</div>
</div>
</div>
`
userPage.navbarDiv.addEventListener("click", (event) => {
ncm_loadUserPage();
});
// 页面代码
function ncm_getHeaders() {
let headers = {};
const headersConf = config.getItem("ext.ncm.apiHeaders");
if (headersConf) {
headersConf.split('&').forEach(pair => {
const [key, value] = pair.split('=');
key && value && (headers[decodeURIComponent(key)] = decodeURIComponent(value));
});
}
return headers;
}
async function ncm_loadUserPage() {
var cookieValue = "";
config.getItem("ext.ncm.apiHeaders").split("&").map((it) => it.split("=")).forEach((it) => {
if (decodeURIComponent(decodeURI(it[0])) == "cookie") {
cookieValue = decodeURIComponent(decodeURI(it[1]));
}
});
if (cookieValue) {
const response = await fetch(config.getItem("ext.ncm.apiEndpoint") + `/login/status?cookie=` + cookieValue, { headers: ncm_getHeaders() });
const data = await response.json();
if (data.data.profile) {
ncm_handleLoginSuccess(data.data);
} else {
ncm_generateQRCode();
}
} else {
ncm_generateQRCode();
}
}
async function ncm_generateQRCode() {
try {
const response = await fetch(config.getItem("ext.ncm.apiEndpoint") + '/login/qr/key?timestamp=${Date.now()}', { headers: ncm_getHeaders() });
const data = await response.json();
const key = data.data.unikey;
const qrResponse = await fetch(config.getItem("ext.ncm.apiEndpoint") + `/login/qr/create?key=${key}&qrimg=true×tamp=${Date.now()}`, { headers: ncm_getHeaders() });
const qrData = await qrResponse.json();
const qrcodeElement = document.getElementById('qrcode');
qrcodeElement.innerHTML = `<img style="width: 100%;" src="${qrData.data.qrimg}" alt="二维码">`;
ncm_checkQRCodeStatus(key);
document.getElementById('loginForm').style.display = 'block';
document.getElementById('qrcodeContainer').style.display = 'block';
document.getElementById('userInfo').style.display = 'none';
} catch (error) {
console.error('生成二维码失败:', error);
}
}
async function ncm_checkQRCodeStatus(key) {
const interval = setInterval(async () => {
try {
const response = await fetch(config.getItem("ext.ncm.apiEndpoint") + `/login/qr/check?key=${key}×tamp=${Date.now()}`, { headers: ncm_getHeaders() });
const data = await response.json();
if (data.code === 803) {
clearInterval(interval);
ncm_handleLoginSuccess(data);
} else if (data.code === 801 || data.code === 802) {
// 继续轮询
} else if (data.code === 800) {
clearInterval(interval);
}
} catch (error) {
console.error('检查二维码状态失败:', error);
clearInterval(interval);
}
}, 2000);
}
async function ncm_loginWithEmail() {
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
try {
const response = await fetch(config.getItem("ext.ncm.apiEndpoint") + '/login?email=' + email + '&password=' + password, { headers: ncm_getHeaders() })
const data = await response.json();
if (data.code === 200) {
ncm_handleLoginSuccess(data);
} else {
alert('登录失败,请检查您的用户名和密码!');
}
} catch (error) {
console.error('登录失败:', error);
}
}
function ncm_logOut() {
var cookieValue = "";
config.getItem("ext.ncm.apiHeaders").split("&").map((it) => it.split("=")).forEach((it) => {
if (decodeURIComponent(it[0]) == "cookie") {
cookieValue = decodeURIComponent(it[1]);
fetch(config.getItem("ext.ncm.apiEndpoint") + `/logout?cookie=` + cookieValue, { headers: ncm_getHeaders() });
config.setItem('ext.ncm.apiHeaders', config.getItem("ext.ncm.apiHeaders").replace('cookie=' + it[1], ''));
}
});
document.getElementById('loginForm').style.display = 'block';
document.getElementById('qrcodeContainer').style.display = 'block';
document.getElementById('userInfo').style.display = 'none';
}
function ncm_handleLoginSuccess(data) {
if (data.cookie) {
const MUSIC_U_REGEX = /MUSIC_U=(.*?)(?:;|$)/;
const match = MUSIC_U_REGEX.exec(data.cookie);
if (match) {
var currentConfig = config.getItem('ext.ncm.apiHeaders');
var targetConfig = encodeURI(encodeURIComponent('cookie')) + '=' + encodeURI(encodeURIComponent('MUSIC_U=' + match[1]));
if (currentConfig && currentConfig !== "" && currentConfig !== null) {
currentConfig = currentConfig + '&' + targetConfig
} else {
currentConfig = targetConfig
}
config.setItem('ext.ncm.apiHeaders', currentConfig);
}
}
if (data.profile) {
document.getElementById('username').innerText = data.profile.nickname;
document.getElementById('avatar').src = data.profile.avatarUrl;
} else if (data.account && data.account.userName) {
document.getElementById('username').innerText = data.account.userName;
document.getElementById('avatar').src = 'https://s1.music.126.net/style/favicon.ico';
} else {
console.error('数据格式不正确');
}
document.getElementById('loginForm').style.display = 'none';
document.getElementById('qrcodeContainer').style.display = 'none';
document.getElementById('userInfo').style.display = 'block';
}
function ncm_openMyPlaylists() {
switchRightPage('recommendPage');
document.getElementById('listSearchTab').click();
document.getElementById('ncm_search_input').value = `${document.getElementById('username').innerText}`;
document.getElementById('ncm_search_btn').click();
}
function ncm_openMyMusics() {
ncm_loadLikeList();
}
// 链接事件
document.getElementById('loginButton').onclick = ncm_loginWithEmail;
document.getElementById('qrcode').onclick = ncm_generateQRCode;
document.getElementById('logoutButton').onclick = ncm_logOut;
document.getElementById('myPlaylistsButton').onclick = ncm_openMyPlaylists;
document.getElementById('myMusicsButton').onclick = ncm_openMyMusics;