上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

签到脚本-日志报错

[复制链接]
  • TA的每日心情
    奋斗
    13 小时前
  • 签到天数: 162 天

    [LV.7]常住居民III

    22

    主题

    28

    回帖

    197

    积分

    荣誉开发者

    积分
    197

    新人报道油中2周年荣誉开发者

    发表于 2023-4-13 16:25:25 | 显示全部楼层 | 阅读模式
    悬赏16油猫币已解决

    本帖最后由 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);
        }
    
    });
    

    最佳答案

    查看完整内容

    [md]backscript return not promise ![image.png](data/attachment/forum/202304/13/165311kbet9ab0aemssttz.png)[/md]
  • TA的每日心情
    无聊
    2023-11-24 10:55
  • 签到天数: 61 天

    [LV.6]常住居民II

    14

    主题

    30

    回帖

    111

    积分

    中级工程师

    积分
    111
    发表于 2023-4-13 16:25:26 | 显示全部楼层

    backscript return not promise

    image.png

    已有1人评分好评 油猫币 理由
    zip11 + 1 + 7 很给力!

    查看全部评分 总评分:好评 +1  油猫币 +7 

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    294

    主题

    3906

    回帖

    3826

    积分

    管理员

    积分
    3826

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-4-13 17:52:11 | 显示全部楼层
    player 发表于 2023-4-13 16:53
    [md]backscript return not promise

    !(data/attachment/forum/202304/13/165311kbet9ab0aemssttz.png)[/m ...

    正解,否则会有一个warn
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表