油猴脚本之掘金签到

585 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情”

注意: 目前禁止脚本签到,本文章仅仅学习使用。

需求:本篇文章设置需要打开掘金后才会自动签到。

油猴插件先配置后使用,配置在==UserScript==内。关键的几个配置 @grant 用到的油猴的一些方法。 GM_getValue GM_setValue : 数据持续化。保存数据获取数据 GM_addStyle 添加样式代码 GM_xmlhttpRequest 接口请求 window.onurlchange url变化的回调

F12签到查看掘金的相关接口,在油猴里面判断是否处于掘金网站,自己调用即可。有封号风险,仅限学习使用。

 function juejinSign() {
        const signKey = "juejin_sign";
        // 获取上一次签到的日子
        const lastSignDate = GM_getValue(signKey, 0);
        // 计算现在所在的日子
        const currentDate = new Date().getDate();

       /** GM_xmlhttpRequest({
            url: "https://api.juejin.cn/growth_api/v1/lottery_history/global_big",
            method: "POST",
            data: '{"page_no":1,"page_size":5}',
            headers: {
                "content-type": "application/json",
                "user-agent": navigator.userAgent,
            },
            responseType: "json",
            onload(response) {
                if (response.status === 200) {
                    const itemData = response.response.data.lotteries[0];
                    console.log( itemData)
                    GM_xmlhttpRequest({
                        url: "https://api.juejin.cn/growth_api/v1/lottery_lucky/dip_lucky?aid=2608&spider=0&uuid=" + itemData.history_id,
                        method: "GET",
                        headers: {
                            "content-type": "application/json",
                            "user-agent": navigator.userAgent,
                        },
                        responseType: "json",
                        onload(response) {
                            if (response.status === 200) {
                                console.log( response)
                            }
                        },
                    });
                }
            },
        });
**/
        // 今天已经请求过,不再请求
        if (currentNumberOfDays !== lastSignNumberOfDays) {
            GM_xmlhttpRequest({
                url: "https://api.juejin.cn/growth_api/v1/check_in",
                method: "POST",
                headers: {
                    "content-type": "application/json",
                    "user-agent": navigator.userAgent,
                },
                responseType: "json",
                onload(response) {
                    if (response.status === 200) {
                        const data = response.response;
                        if (data.data === "success") {
                            alert("签到成功");
                        } else {
                            alert(data.err_msg);
                        }
                        // 存储签到成功日期
                        GM_setValue(signKey, currentDate);
                    }
                },
            });
            
            
            // 每天免费抽奖
            GM_xmlhttpRequest({
                url: "https://api.juejin.cn/growth_api/v1/lottery/draw",
                method: "POST",
                headers: {
                    "content-type": "application/json",
                    "user-agent": navigator.userAgent,
                },
                responseType: "json",
                onload(response) {
                    if (response.status === 200) {
                        const data = response.response;
                        if (data.data === "success") {
                            alert("签到成功");
                        } else {
                            alert(data.err_msg);
                        }
                        // 存储签到成功日期
                        GM_setValue(signKey, currentDate);
                    }
                },
            });

        }

    }
if (window.onurlchange === null) {
        window.addEventListener('urlchange', (info) => {
            // 仅仅打开掘金才请求签到的接口。去掉有风险,慎用。
            if(info.url.indexOf('juejin.cn') !== -1) {
                juejinSign()
            }
        });
    }

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 2 天,点击查看活动详情”