Skip to content

Instantly share code, notes, and snippets.

@yige233
Last active February 28, 2024 08:59
Show Gist options
  • Save yige233/9e2eb148df88a3c4692e9abdff8839d6 to your computer and use it in GitHub Desktop.
Save yige233/9e2eb148df88a3c4692e9abdff8839d6 to your computer and use it in GitHub Desktop.
其乐论坛:蒸汽换体力油猴脚本
// ==UserScript==
// @name 其乐论坛:蒸汽换体力脚本
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 其乐论坛:蒸汽换体力脚本。打开https://keylol.com/home.php?mod=spacecp&ac=credit&op=exchange,然后打开油猴插件扩展菜单,即可使用
// @author 破损的鞘翅
// @match https://keylol.com/home.php?mod=spacecp&ac=credit&op=exchange
// @icon https://www.google.com/s2/favicons?domain=keylol.com
// @grant GM.registerMenuCommand
// @grant GM.getValue
// @grant GM.setValue
// ==/UserScript==
(function () {
"use strict";
class Exchange {
echangedCredits = 0;
events = new Map([
["积分操作成功", [true, "任务成功完成"]],
["兑换后蒸汽不足", [false, "兑换后蒸汽不足50点"]],
["您输入的密码错误", [false, "您输入的密码错误"]],
["请求来路不正确", [false, "请求来路不正确或提交的表单数据有误"]],
]);
constructor() {}
async post() {
const result = await fetch("https://keylol.com/home.php?mod=spacecp&ac=credit&op=exchange&handlekey=credit&inajax=1", {
headers: {
"content-type": "application/x-www-form-urlencoded",
},
body: [
`formhash=${document.forms[1].formhash.value}`,
"operation=exchange",
"exchangesubmit=true",
"outi=0",
"exchangeamount=3",
"tocredits=1",
"fromcredits=3",
`password=${await GM.getValue("password")}`,
].join("&"),
method: "POST",
credentials: "include",
}).then((res) => res.text());
for (let event of this.events) {
if (result.indexOf(event[0]) != -1) {
return event[1];
}
}
return [false, "未知原因"];
}
async run(times = 1) {
const [success, reason] = await this.post();
if (success) {
this.echangedCredits = this.echangedCredits + 3;
times--;
console.log(success, reason);
if (times > 0) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return this.run(times);
}
}
const msg = `本次体力兑换由于 ${reason} 而结束!一共兑换了${this.echangedCredits}体力,消耗了${this.echangedCredits}蒸汽。`;
console.log(msg);
showDialog(msg, "alert", null, null, 0, null, null, null, null, 5);
}
}
GM.registerMenuCommand("设置密码", async () => {
let pwd = prompt("填入密码。");
if (!pwd) return alert("未设置密码!");
await GM.setValue("password", pwd);
alert("密码设置为:" + pwd);
});
GM.registerMenuCommand("清除密码", async () => {
await GM.setValue("password", "");
alert("密码已清除!");
});
GM.registerMenuCommand("兑换体力(单次)", () => {
new Exchange().run();
});
GM.registerMenuCommand("兑换体力(全部)", () => {
if (confirm("确定消耗全部蒸汽兑换体力吗??")) {
new Exchange().run(99999);
}
});
GM.registerMenuCommand("兑换体力(自定次数)", () => {
let times = Number(prompt("希望运行几次?"));
if (!times) return alert("没有输入有效次数!");
if (confirm("预计兑换" + times * 3 + "体力。")) {
new Exchange().run(times);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment