-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjd_cookie_sync_ql.user.js
More file actions
250 lines (240 loc) · 6.73 KB
/
jd_cookie_sync_ql.user.js
File metadata and controls
250 lines (240 loc) · 6.73 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
// ==UserScript==
// @name 京东Cookie手动同步青龙
// @namespace jd_cookie_sync_ql
// @storageName jd_cookie_sync_ql
// @version 1.0.0
// @description 手动同步京东Cookie到青龙面板
// @author Leslie
// @run-at document-end
// @match https://home.m.jd.com/myJd/newhome.action*
// @match https://m.jd.com/*
// @grant unsafeWindow
// @grant GM_cookie
// @grant GM_addValueChangeListener
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_log
// @connect jd.com
// ==/UserScript==
/* ==UserConfig==
青龙配置:
url:
title: 青龙面板地址
description: '青龙面板地址,例如: http://127.0.0.1:1080'
clientId:
title: clientId
description: 请在 系统设置->应用设置 添加应用,然后复制client id值
clientSecret:
title: clientSecret
description: 请在 系统设置->应用设置 添加应用,然后复制client secret值
==/UserConfig== */
const sync = document.createElement("div");
sync.id = "sync-ck";
sync.style.position = "absolute";
sync.style.zIndex = "1000";
sync.style.padding = "10px";
sync.style.width = "100px";
sync.style.left = "calc(50% - 50px)";
sync.style.top = "200px";
sync.style.background = "aliceblue";
sync.style.color = "#000";
sync.style["text-align"] = "center";
sync.style["border-radius"] = "8px";
sync.style["font-weight"] = "bold";
sync.innerHTML = "同步cookie";
function getJDCookie() {
return new Promise((resolve, reject) => {
let jdCookie = "";
GM_cookie(
"list",
{
domain: ".jd.com",
name: "pt_key",
},
(cookies) => {
if (cookies.length == 0) {
return reject();
}
jdCookie = "pt_key=" + cookies[0].value;
GM_cookie(
"list",
{
domain: ".jd.com",
name: "pt_pin",
},
(cookies) => {
if (cookies.length == 0) {
return reject();
}
jdCookie += ";pt_pin=" + cookies[0].value + ";";
resolve({
jdCookie: jdCookie,
pt_pin: decodeURI(cookies[0].value),
});
},
);
},
);
});
}
function ajax(method, url, token, data) {
return new Promise((resolve, reject) => {
let headers = {};
if (method != "get") {
headers["Content-Type"] = "application/json";
}
if (token) {
headers["Authorization"] = "Bearer " + token;
}
GM_xmlhttpRequest({
url: url,
method: method,
data: JSON.stringify(data),
headers: headers,
responseType: "json",
onload(resp) {
let code = resp.response.code || "unknow";
let msg = resp.response.message || "unkonw";
if (resp.status == 200) {
if (code == 200) {
resolve(resp.response);
} else {
reject(msg);
}
} else {
reject(msg);
}
},
onerror(resp) {
reject(resp);
},
});
});
}
function getQlToken() {
GM_log("getQlToken-开始获取青龙Token");
return new Promise(async (resolve, reject) => {
let url = GM_getValue("青龙配置.url");
let clientId = GM_getValue("青龙配置.clientId");
let clientSecret = GM_getValue("青龙配置.clientSecret");
try {
let resp = await ajax(
"get",
url + "/open/auth/token?client_id=" + clientId + "&client_secret=" + clientSecret,
);
token = resp.data.token;
GM_setValue("ql.token", token);
GM_setValue("ql.expiration", resp.data.expiration);
GM_log("getQlToken-获取青龙Token成功");
resolve(token);
} catch (e) {
GM_log("getQlToken-获取青龙Token失败" + e);
return reject(e);
}
});
}
function updateCk(ckName, ck, pt_pin, token) {
return new Promise(async (resolve, reject) => {
try {
let url = GM_getValue("青龙配置.url");
let resp = await ajax("get", url + "/open/envs?searchValue=" + ckName, token);
let id = (resp.data || []).find((item) => item.name === ckName).id || "";
if (id) {
// 使用 Token 更新环境变量
await ajax("put", url + "/open/envs", token, {
name: ckName,
value: ck,
remarks: pt_pin + " by ScriptCat",
id: id,
});
// 使用 Token 启用环境变量
await ajax("put", url + "/open/envs/enable", token, [id]);
} else {
// 使用 Token 添加环境变量
await ajax("post", url + "/open/envs", token, [
{ name: ckName, value: ck, remarks: pt_pin + " by ScriptCat" },
]);
}
resolve();
} catch (e) {
reject(e);
}
});
}
function setJDCookieToQingLong(ck, pt_pin) {
return new Promise(async (resolve, reject) => {
//获取青龙token
let url = GM_getValue("青龙配置.url");
if (!url) {
return reject("请配置青龙地址");
}
let ckName = "JD_COOKIE";
let token = GM_getValue("ql.token");
let expiration = GM_getValue("ql.expiration");
try {
// token过期重新获取
if (!token || expiration < new Date().getTime() / 1000 - 1000) {
token = await getQlToken();
}
await updateCk(ckName, ck, pt_pin, token);
resolve();
} catch (e) {
if (e == "UnauthorizedError") {
try {
token = await getQlToken();
await updateCk(ckName, ck, pt_pin, token);
resolve();
} catch (e) {
reject(e);
}
}
return reject(e);
}
});
}
function startSync() {
getJDCookie()
.then((resp) => {
let ck = resp.jdCookie;
let pt_pin = resp.pt_pin;
GM_setClipboard(ck);
GM_setValue("jdck", ck);
setJDCookieToQingLong(ck, pt_pin)
.then((resp) => {
GM_log("ck设置成功");
GM_notification({
title: "京东ck同步青龙",
text: "以将ck复制到了剪辑版,青龙面板推送成功",
});
})
.catch((e) => {
GM_log("ck设置失败" + JSON.stringify(e), "error");
GM_notification({
title: "京东ck同步青龙",
text: "以将ck复制到了剪辑版,青龙面板推送失败:" + e,
});
});
})
.catch((err) => {
GM_log(err);
GM_notification({
title: "京东ck同步青龙",
text: "京东ck获取失败,请确定是否登录",
});
});
}
sync.addEventListener("click", (ev) => {
switch (ev.target.id) {
case "sync-ck": {
startSync();
break;
}
default: {
console.log("点击同步cookie:");
}
}
});
document.body.appendChild(sync);