京东ck获取并提交到青龙_后台
// ==UserScript==
// @name 京东ck获取并提交到青龙_后台
// @namespace jdck
// @storageName jdck
// @version 0.1.11
// @description 获取京东的ck并提交到青龙面板,需要提交时点击一次运行脚本,需要配置面板client
// @author 王一之
// @grant GM_cookie
// @grant GM_addValueChangeListener
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @grant GM_getCookieStore
// @grant GM_log
// @connect jd.com
// @background
// ==/UserScript==
/* ==UserConfig==
青龙配置:
push:
title: 是否推送
description: 开启后,不会通知直接推送到青龙面板
default: false
url:
title: 青龙面板地址
description: '青龙面板地址,例如: http://127.0.0.1:1080'
clientId:
title: clientId
description: 请在 系统设置->应用设置 添加应用,然后复制client id值
clientSecret:
title: clientSecret
description: 请在 系统设置->应用设置 添加应用,然后复制client secret值
==/UserConfig== */
if (!window.GM_getCookieStore) {
window.GM_getCookieStore = (_, cb) => {
cb(0, undefined)
}
}
function getCk(tabid) {
return new Promise((resolve, reject) => {
GM_getCookieStore(tabid, (storeId, err) => {
if (err) {
return reject(err);
}
let jdCookie = '';
GM_cookie('list', {
domain: '.jd.com',
name: "pt_key",
storeId: storeId
}, (item) => {
if (item.length == 0) {
return reject();
}
jdCookie = "pt_key=" + item[0].value;
GM_cookie('list', {
domain: '.jd.com',
name: "pt_pin",
storeId: storeId
}, (item) => {
if (item.length == 0) {
return reject();
}
jdCookie += ";pt_pin=" + item[0].value + ";";
resolve({ jdCookie: jdCookie, pt_pin: decodeURI(item[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,
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 updateToken() {
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);
resolve(token);
} catch (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 = '';
for (let i = 0; i < resp.data.length; i++) {
if (resp.data[i].remarks && resp.data[i].remarks.startsWith(pt_pin)) {
_id = resp.data[i]._id;
break;
}
}
if (_id) {
await ajax('put', url + '/open/envs', token, { "name": ckName, "value": ck, "remarks": pt_pin + ' by ScriptCat', "_id": _id });
await ajax('put', url + '/open/envs/enable', token, [_id]);
} else {
await ajax('post', url + '/open/envs', token, [{ "name": ckName, "value": ck, "remarks": pt_pin + ' by ScriptCat' }]);
}
resolve();
} catch (e) {
reject(e);
}
});
}
function setQlCk(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 {
if (!token || expiration < (new Date().getTime() / 1000) - 1000) {
token = await updateToken();
}
await updateCk(ckName, ck, pt_pin, token);
resolve();
} catch (e) {
if (e == 'UnauthorizedError') {
try {
token = await updateToken();
await updateCk(ckName, ck, pt_pin, token);
resolve();
} catch (e) {
reject(e);
}
}
return reject(e);
}
});
}
return new Promise(async (resolve, reject) => {
GM_addValueChangeListener('logout', (name, oldval, newval, remote, tabid) => {
GM_getCookieStore(tabid, (storeId, err) => {
if (err) {
return;
}
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'pt_key',
storeId: storeId,
}, () => {
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'pt_pin',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'pt_token',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'pwdt_id',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 's_key',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 's_pin',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'sfstoken',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'sid',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'thor1',
storeId: storeId,
});
GM_cookie('delete', {
url: 'https://home.m.jd.com/myJd/newhome.action',
name: 'wq_skey',
storeId: storeId,
});
});
});
});
GM_addValueChangeListener('push-ck', (name, oldval, newval, remote, tabid) => {
getCk(tabid).then(resp => {
let ck = resp.jdCookie;
let pt_pin = resp.pt_pin;
GM_setClipboard(ck);
GM_setValue('jdck', ck);
if (GM_getValue('青龙配置.push')) {
setQlCk(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,
});
});
} else {
GM_notification({
title: '京东ck获取并提交到青龙',
text: '以将ck复制到了剪辑版,请确定是否还要继续自动提交到青龙面板',
buttons: [{
title: '提交',
}, {
title: '取消'
}],
onclick(id, index) {
if (index === 0) {
//获取client并推送到青龙
setQlCk(ck, pt_pin).then(resp => {
GM_log('ck设置成功');
}).catch(e => {
GM_log('ck设置失败' + JSON.stringify(e), 'error');
GM_notification({
title: '京东ck获取并提交到青龙',
text: '以将ck复制到了剪辑版,青龙面板推送失败:' + e,
});
});
}
}
});
}
}).catch(res => {
GM_notification({
title: '京东ck获取并提交到青龙',
text: '京东ck获取失败,请确定是否登录',
});
});
});
});