// ==UserScript== // @name 百度贴吧自动签到 // @namespace Sency // @author Sency // @version 0.4.1 // @crontab * 1-23 once * * // @grant GM_xmlhttpRequest // @grant GM_notification // @grant GM_getValue // @connect tieba.baidu.com // @homepage https://bbs.tampermonkey.net.cn/forum.php?mod=viewthread&tid=926 // @supportURL https://bbs.tampermonkey.net.cn/forum.php?mod=viewthread&tid=926 // ==/UserScript== /* ==UserConfig== 设置: DelayMode: title: 延迟签到模式 default: false Notice: title: 通知窗口 default: 总是 values: [总是, 仅失败时, 关闭] ==/UserConfig== */ function ajax(url, method = 'get', data = null, referer = null) { let headers = null; if (method.toLowerCase() === 'post') { headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Origin': 'https://tieba.baidu.com' }; if (referer) headers['Referer'] = referer; } return new Promise((resolve, reject) => { GM_xmlhttpRequest({ url: url, method: method, data: data, headers: headers, timeout: 20000, onload: res => res.status == 200 ? resolve(res.response) : reject(`${res.status}: ${url}`), onerror: () => reject(`error: ${url}`), ontimeout: () => reject(`timeout: ${url}`) }); }); } function parseJson(obj) { let json = obj; if (typeof obj !== 'object') { try { json = JSON.parse(obj); } catch(e) { json = {}; } } return json; } return new Promise(async (resolve, reject) => { const delayMode = GM_getValue('设置.DelayMode', false); const notice = GM_getValue('设置.Notice', '总是'); try { const getTbs = parseJson(await ajax('https://tieba.baidu.com/dc/common/tbs', 'get')); const tbs = getTbs.tbs; await ajax('https://tieba.baidu.com/tbmall/onekeySignin1', 'post', `ie=utf-8&tbs=${tbs}`, 'https://tieba.baidu.com/index.html'); //const html = await ajax('https://tieba.baidu.com/index.html', 'get'); //const forums = JSON.parse(html.match(/(?<="forums":)\[.*?\]/)[0]); const getForums = parseJson(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 = parseJson(await ajax('https://tieba.baidu.com/sign/add', 'post', `ie=utf-8&kw=${forum.forum_name}&tbs=${tbs}`, `https://tieba.baidu.com/f?ie=utf-8&kw=${encodeURIComponent(forum.forum_name)}`)); if (doSign.no == 0) success++; if (delayMode) await new Promise(resolve => setTimeout(resolve, 1000)); } 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); } });