本帖最后由 zip11 于 2023-4-13 21:19 编辑
本帖最后由 zip11 于 2023-4-13 21:16 编辑
编写论坛 -定时签到脚本,可以签到,但是日志报错,想知道如何解决
报错信息
2023-04-13 12:00:00 backscript return not promise {"env":"sandbox","component":"sandbox","scriptId":10,"name":"油猴中文网_sign"}
报错的代码
// ==UserScript==
// @name 油猴中文网_sign
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 油猴中文网论坛-定时签到脚本,每日1次,需要先登陆
// @author zip11
// @grant GM_xmlhttpRequest
// @grant GM_notification
// @grant GM_log
// @connect bbs.tampermonkey.net.cn
// @crontab 1-59 * once * *
// ==/UserScript==
(function () {
'use strict';
function hash(wz1) {
// bbs 获取 mhash
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "get",
url: wz1,
headers: {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
onload: function (xhr) {
console.log("请求成功");
let nr = xhr.responseText
//响应 文本
console.log(nr);
// 提取 网页的mhash -start
let parser = new DOMParser();
let doc = parser.parseFromString(nr, "text/html")
// string类型的 html文本转换成 dom结构
let formhash = doc.querySelector(`input[name="formhash"]`).value;
// css 选择器
console.log(formhash)
//GM_notification(formhash)
resolve(formhash);
},
onerror: function (response) {
//console.log("请求失败");
reject("请求失败" + response);
}
});
/*
*/
});
}
function tmcn_sign(hash1) {
// bbs 签到 post
if (typeof hash1 == 'string' && hash1.length > 0) {
//判断字符串不为 空
let body1 = "formhash=" + hash1 + "&qdxq=fd&qdmode=3&todaysay=&fastreply=0"
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "POST",
url: "https://bbs.tampermonkey.net.cn/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
'Referer':'https://bbs.tampermonkey.net.cn/dsu_paulsign-sign.html'
},
data: body1,
// 签到 执行 完成
onload: function (xhr) {
let text = '';
console.log("请求成功");
//console.log(xhr.responseText);
if (xhr.responseText.includes("签到成功")) {
text = "油猴中文网bbs定时签到成功"
//GM_notification(text)
resolve(text)
} else if (xhr.responseText.includes("已经签到")) {
text = "油猴中文网bbs定时签到失败 - 重复签到"
//GM_notification(text)
resolve(text)
} else {
text = "油猴中文网bbs定时签到失败-未知错误-详见调试-mhash:" + hash1 + "\nbody1:" + body1
//GM_notification(text)
reject(text)
}
// resolve(xhr.responseText);
},
// 签到 错误
onerror: function (response) {
console.log("请求失败" + response);
reject("bbs请求失败" + response);
}
});
});
}
else {
return "mhash为空无法签到"
}
}
async function run1() {
const hh = await hash("https://bbs.tampermonkey.net.cn/thread-184-1-1.html");
//GM_notification(hh);
// mhash 获取
const sign_msg = await tmcn_sign(hh);
GM_notification(sign_msg);
// bbs 签到
if(sign_msg!=="油猴中文网定时签到成功") {
// 错误-日志保存
GM_log(sign_msg,"error");
}
else {
//签到 成功
GM_log(sign_msg,"info");
}
}
run1();
// 签到
})();
正确 不报错的代码
感谢player的解答
(必须严格安装脚本猫的后台脚本模板写,不按规范写签到完成后脚本却没有自动结束,总算没有烦人的报错了)
// ==UserScript==
// @name 油猴中文网_sign
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 油猴中文网论坛-定时签到脚本,每日1次,需要先登陆
// @author zip11
// @grant GM_xmlhttpRequest
// @grant GM_notification
// @grant GM_log
// @connect bbs.tampermonkey.net.cn
// @crontab 1-59 * once * *
// ==/UserScript==
'use strict';
function hash(wz1) {
// bbs 获取 mhash
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: "get",
url: wz1,
headers: {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
onload: function (xhr) {
console.log("请求成功");
let nr = xhr.responseText
//响应 文本
console.log(nr);
// 提取 网页的mhash -start
let parser = new DOMParser();
let doc = parser.parseFromString(nr, "text/html")
// string类型的 html文本转换成 dom结构
let formhash = doc.querySelector(`input[name="formhash"]`).value;
// css 选择器
console.log(formhash)
//GM_notification(formhash)
resolve(formhash);
},
onerror: function (response) {
//console.log("请求失败");
reject("请求失败" + response);
}
});
});
}
function tmcn_sign(hash1) {
// bbs 签到 post
return new Promise((resolve, reject) => {
if (typeof hash1 == 'string' && hash1.length > 0) {
//判断字符串不为 空
let body1 = "formhash=" + hash1 + "&qdxq=fd&qdmode=3&todaysay=&fastreply=0"
GM_xmlhttpRequest({
method: "POST",
url: "https://bbs.tampermonkey.net.cn/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
'Referer':'https://bbs.tampermonkey.net.cn/dsu_paulsign-sign.html'
},
data: body1,
// 签到 执行 完成
onload: function (xhr) {
let text = '';
console.log("请求成功");
//console.log(xhr.responseText);
if (xhr.responseText.includes("签到成功")) {
text = "油猴中文网bbs定时签到成功"
//GM_notification(text)
resolve(text)
} else if (xhr.responseText.includes("已经签到")) {
text = "油猴中文网bbs定时签到失败 - 重复签到"
//GM_notification(text)
resolve(text)
} else {
text = "油猴中文网bbs定时签到失败-未知错误-详见调试-mhash:" + hash1 + "\nbody1:" + body1
//GM_notification(text)
reject(text)
}
// resolve(xhr.responseText);
},
// 签到 错误
onerror: function (response) {
console.log("请求失败" + response);
reject("bbs请求失败" + response);
}
});
} else {
reject("mhash为空无法签到");
//return
}
});
}
async function run1() {
const hh = await hash("https://bbs.tampermonkey.net.cn/thread-184-1-1.html");
//GM_notification(hh);
// mhash 获取
const sign_msg = await tmcn_sign(hh);
GM_notification(sign_msg);
if(sign_msg!=="油猴中文网定时签到成功") {
// 错误-日志保存
GM_log(sign_msg,"error");
return(sign_msg);
} else {
//签到 成功
GM_log(sign_msg,"info");
return(sign_msg);
}
}
return new Promise(async (resolve, reject) => {
try {
let jg8 = await run1();
if (jg8 == "油猴中文网定时签到成功") {
resolve("ok"+jg8);// 执行成功
} else {
reject("error-exit"+jg8);// 执行失败,并返回错误原因
}
} catch (error) {
//GM_log(error);
reject(error);
}
});