科研小助手
// ==UserScript==
// @name 科研小助手
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 科研通签到
// @author You
// @crontab * * once * *
// @grant GM_xmlhttpRequest
// @grant GM_log
// @grant GM_notification
// ==/UserScript==
return new Promise((resolve, reject) => {
'use strict';
const tools = {
sleep: t => new Promise(res => setTimeout(res, t)),// 箭头函数体只有一句,可以省略return
net: {
change: (str, withCookie) => {
str = str.trim(); //去除文本首位\s
let method = str.match(/(.*?)\s/)[1];
let url = str.match(/\s(.*?)\s/)[1];
str = str.replace(/.*?\n/, ''); //去除第一行
let data = /\n\s*\n\s*(.*)$/m.test(str) ? str.slice(str.match(/\n\s*\n\s*(.*)$/m).index).trim() : ''; //获取请求体,没有则返回'', 用slice截取防止请求体含有\n时出错
str = str.replace(new RegExp(data), '').trim(); //去除data
str = str.replace(/^(\s*?)(\S.*)/gm, `$2`); //去除每行行首的\s
str = str.replace(/\sContent-Length:.*/im, ''); //去除Content-Length所在行,不用/^\sContent-Length:./im, 这样做会多一个空白行避免一些问题
str = withCookie ? str : str.replace(/\s*cookie:.*/im, '') //去除cookie所在行,不用/^\s*cookie.*/im, 这样做会多一个空白行
let headers = {}, h = str.match(/(\S*?):\s*(\S.*)/mg);
// GM_log(h)
h.forEach(e => {
let t = e.match(/(\S*?):\s*(.*)/);
headers[t[1]] = t[2].replace(/\s*$/, '');//去除行尾\s,避免一下总没错吧
});
//去除浏览器默认携带的请求头
if (!/origin:/i.test(str)) headers.origin = '';//脚本猫默认有一个拓展origin,这里去掉
if (!/dnt/i.test(str)) headers.dnt = '';
if (!/referer/i.test(str)) headers.referer = '';
//if (!/accept/i.test(str)) headers.accept='';
if (!/user-agent/i.test(str)) headers['user-agent'] = '';
if (!/sec-ch-ua/i.test(str)) headers['sec-ch-ua'] = '';
if (!/sec-ch-ua-mobile/i.test(str)) headers['sec-ch-ua-mobile'] = '';
if (!/sec-ch-ua-platform/i.test(str)) headers['sec-ch-ua-platform'] = '';
if (!/sec-fetch-dest/i.test(str)) headers['sec-fetch-dest'] = '';
if (!/sec-fetch-mode/i.test(str)) headers['sec-fetch-mode'] = '';
if (!/sec-fetch-site/i.test(str)) headers['sec-fetch-site'] = '';
//if (!/accept-language/i.test(str)) headers['accept-language']='';
if (!/accept-encoding/i.test(str)) headers['accept-encoding'] = '';
return { url, method, data, headers }
},
}
};
tools.net.send = (str, onload = (xhr => xhr), anonymous = false, withCookie = false) => {
let t = tools.net.change(str, withCookie);
return new Promise((resolve, reject) => {
t.anonymous = anonymous;
t.onload = (xhr) => {
resolve(onload(xhr))
};
t.onerror = _ => { GM_log('net error') };
GM_xmlhttpRequest(t)
})
};
(async _ => {
await tools.sleep(1000 * 60 * 2);
let msg = await tools.net.send(`
GET https://www.ablesci.com/user/sign HTTP/1.1
Host: www.ablesci.com
Connection: keep-alive
sec-ch-ua: "Not_A Brand";v="99", "Microsoft Edge";v="109", "Chromium";v="109"
Accept: application/json, text/javascript, */*; q=0.01
DNT: 1
X-Requested-With: XMLHttpRequest
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.69
sec-ch-ua-platform: "Windows"
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://www.ablesci.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ja;q=0.5
sec-gpc: 1
`, xhr => JSON.parse(xhr.response).msg);
GM_log(msg)
GM_notification('科研通自动签到:\n' + msg)
})();
resolve();
});