// ==UserScript== // @name 百度贴吧自动签到 // @namespace Sency // @author Sency // @version 0.3 // @crontab * 1-23 once * * // @grant GM_xmlhttpRequest // @grant GM_notification // @grant GM_getValue // @connect tieba.baidu.com // ==/UserScript== /* ==UserConfig== 设置: Notice: title: 通知选项 default: 总是 values: [总是, 仅失败时, 关闭] ==/UserConfig== */ const defaultHeaders = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}; function ajax(url, method = 'get', data = null) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ url: url, method: method, data: data, headers: data ? defaultHeaders : null, timeout: 20000, onload: res => res.status == 200 ? resolve(res.response) : reject(`${res.status}: ${url}`), onerror: () => reject(`error: ${url}`), ontimeout: () => reject(`timeout: ${url}`) }); }); } return new Promise(async (resolve, reject) => { const notice = GM_getValue('设置.Notice', '总是'); try { const getTbs = await ajax('https://tieba.baidu.com/dc/common/tbs', 'get'); const tbs = JSON.parse(getTbs).tbs; await ajax('https://tieba.baidu.com/tbmall/onekeySignin1', 'post', `ie=utf-8&tbs=${tbs}`); //const html = await ajax('https://tieba.baidu.com/index.html', 'get'); //const forums = JSON.parse(html.match(/(?<="forums":)\[.*?\]/)[0]); const getForums = await ajax('https://tieba.baidu.com/mo/q/newmoindex', 'get'); const forums = getForums.data.like_forum; const unsigns = forums.filter(forum => forum.is_sign != 1); let success = forums.length - unsigns.length; for (let forum of unsigns) { //贴吧会检测是否签到过快,所以这里不用Promise.all const doSign = await ajax('https://tieba.baidu.com/sign/add', 'post', `ie=utf-8&kw=${forum.forum_name}&tbs=${tbs}`); JSON.parse(doSign).no == 0 && success++; } const fail = forums.length - success; notice == '总是' && GM_notification({ title: '贴吧签到', text: `签到完成,已签到${success}个吧,失败${fail}个吧。` }); resolve(); } catch(e) { if (notice != '关闭') { if (e.toString().slice(0, 7) == 'timeout') { GM_notification({ title: '贴吧签到', text: '签到失败,请检查你的网络状态。' }); } else { GM_notification({ title: '贴吧签到', text: '签到失败,请检查你的登录状态。' }); } } reject(e); } });