百度贴吧自动签到
// ==UserScript==
// @name 百度贴吧自动签到
// @description 定时脚本,请使用脚本猫安装,并确保你的百度账号处于登录状态。
// @namespace cxxjackie
// @author cxxjackie
// @version 0.5.0
// @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: true
Notice:
title: 通知窗口
default: 总是
values: [总是, 仅失败时, 关闭]
==/UserConfig== */
const defaultOption = {
method: 'get',
responseType: 'json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Origin': 'https://tieba.baidu.com'
},
timeout: 20000
};
function ajax(url, option = {}) {
const _option = {...defaultOption, ...option};
return new Promise((resolve, reject) => {
if (defaultOption.headers && option.headers) {
const keys = Object.keys(option.headers);
for (const header in defaultOption.headers) {
const lowerHeader = header.toLowerCase();
if (!keys.find(key => key.toLowerCase() === lowerHeader)) {
_option.headers[header] = defaultOption.headers[header];
}
}
}
if ('referer' in _option) {
_option.headers = _option.headers || {};
_option.headers.Referer = _option.referer;
delete _option.referer;
}
// 兼容脚本猫旧版本
const responseType = _option.responseType;
switch (responseType) {
case 'text':
case 'json':
delete _option.responseType;
break;
case 'arraybuffer':
case 'blob':
break;
default:
return reject(`Unsupported responseType: ${responseType}`);
}
GM_xmlhttpRequest({
..._option,
url: url,
onload: res => {
if (res.status == 200) {
// 兼容脚本猫旧版本
switch (responseType) {
case 'text':
resolve(res.responseText);
break;
case 'json':
try {
resolve(JSON.parse(res.responseText));
} catch(e) {
resolve({});
}
break;
default:
resolve(res.response);
}
} else {
reject(`${res.status}: ${url}`);
}
},
onerror: () => reject(`error: ${url}`),
ontimeout: () => reject(`timeout: ${url}`)
});
});
}
return new Promise(async (resolve, reject) => {
const delayMode = GM_getValue('设置.DelayMode', false);
const notice = GM_getValue('设置.Notice', '总是');
try {
const getTbs = await ajax('https://tieba.baidu.com/dc/common/tbs');
const tbs = getTbs.tbs;
await ajax('https://tieba.baidu.com/tbmall/onekeySignin1', {
method: 'post',
data: `ie=utf-8&tbs=${tbs}`,
referer: 'https://tieba.baidu.com/index.html'
});
//const html = await ajax('https://tieba.baidu.com/index.html', { responseType: 'text' });
//const forums = JSON.parse(html.match(/(?<="forums":)\[.*?\]/)[0]);
const getForums = await ajax('https://tieba.baidu.com/mo/q/newmoindex');
const forums = getForums.data.like_forum;
const unsigns = forums.filter(forum => forum.is_sign != 1);
let success = forums.length - unsigns.length;
for (const forum of unsigns) {
// 贴吧会检测是否签到过快,所以这里不用Promise.all
const doSign = await ajax('https://tieba.baidu.com/sign/add', {
method:'post',
data: `ie=utf-8&kw=${forum.forum_name}&tbs=${tbs}`,
referer: `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;
const message = `签到完成,已签到${success}个吧,失败${fail}个吧。`;
if (notice == '总是' || (notice == '仅失败时' && fail > 0)) {
GM_notification({
title: '贴吧签到',
text: message
});
}
resolve(message);
} catch(e) {
if (notice != '关闭') {
if (e.toString().slice(0, 7) == 'timeout') {
GM_notification({
title: '贴吧签到',
text: '签到失败,请检查你的网络状态。'
});
} else {
GM_notification({
title: '贴吧签到',
text: '签到失败,请检查你的登录状态。'
});
}
}
reject(e);
}
});