Greasy Fork

Greasy Fork is available in English.

掘金日常一键操作

后台判断自动签到+抽奖

当前为 2021-10-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         掘金日常一键操作
// @version      0.2
// @description  后台判断自动签到+抽奖
// @match        https://juejin.cn/**
// @run-at       document-end
// @namespace http://greasyfork.icu/users/823922
// ==/UserScript==
{
    // 注:现在本人还在研究如何自动获取到 aid 和 uuid,所以脚本里都是直接写死的,如果使用有问题,建议开控制台看下自己的请求里携带的 aid 及 uuid,然后替换掉第一行的 userInfo 变量
    let userInfo = 'aid=2608&uuid=7016487434494658063';
    let jqExist = () => {
        return typeof jQuery != 'undefined';
    }
    // 用于加载 jq 
    if (!jqExist()) {
        var script = document.createElement('script'),
            head = document.getElementsByTagName('head')[0];
        script.type = 'text/javascript';
        script.charset = 'UTF-8';
        script.src = 'https://code.jquery.com/jquery-3.1.1.min.js';
        head.appendChild(script);
    }
    let loadTime;
    loadTime = setInterval(() => { //  轮询检查 jq 载入,载入成功后去掉轮询
        if (jqExist()) {
            unsafeWindow.$ = $;
            clearInterval(loadTime);
            // 当页面点击时,延迟检测页面 url 变化
            run.setLastUrl(unsafeWindow.location.href);
            run.once();
            $(document).on('click', '*', function() {
                run.once();
            })
        }
    }, 1000);

    unsafeWindow.choujiang = async function choujiang() {
        console.log('调用抽奖函数');
        // return false;
        let obj = {};
        let flag = true;
        simpleDialog.open();
        for (; flag;) {
            await fetch(`https://api.juejin.cn/growth_api/v1/lottery/draw?${userInfo}`, httpConfig)
                .then(function(response) {
                    return response.json();
                }).then(data => {
                    if (data.err_msg == 'success') {
                        let name = data.data.lottery_name;
                        console.log(name);
                        simpleDialog.addRecord(name)
                        obj[name] = obj[name] || 0;
                        obj[name] = obj[name] + 1;
                    } else {
                        flag = false;
                    }
                })
            console.log(obj)
        }
        let keys = Object.keys(obj);
        simpleDialog.empty();
        simpleDialog.addRecord(keys.length > 0 ? keys.map(k => k + ': ' + obj[k]).join('<br>') : '抽奖次数不足!')
        simpleDialog.close(1200);
        return false;
    }

    async function dayTask() {
        console.log('调用日活函数')
        let msgs = [];
        let freeLottery = true;
        // 签到
        await fetch(
                `https://api.juejin.cn/growth_api/v1/check_in?${userInfo}&_signature=_02B4Z6wo00101q966EAAAIDCL3gSAGCq5SKvfuzAAMrJs3JkiZSLejXKJy5lR-3Rot9hYdZVnmHKrdQPh0MmwDCQsjT9tEIN0G3uIK8RsU7pcEznZ9.oqqfseed8PMV.rul6lxG-dkqFtwsq61`,
                httpConfig)
            .then(function(response) {
                return response.json();
            }).then(data => {
                if (data.err_msg == 'success') {
                    msgs.push('签到成功!');
                } else {
                    freeLottery = false;
                    msgs.push(data.err_msg);
                    unsafeWindow.localStorage.juejinDayTask = currDate();
                }
                console.log(data);
            });
        if (!freeLottery) return false; // 没有免费抽奖就退出
        await fetch(`https://api.juejin.cn/growth_api/v1/lottery/draw?${userInfo}`, httpConfig)
            .then(function(response) {
                return response.json();
            }).then(data => {
                if (data.err_msg == 'success') {
                    msgs.push(`获得奖品 ${data.data.lottery_name}`);
                }
                console.log(data);
            });
        simpleDialog.open();
        simpleDialog.addRecord(msgs.join('<br>'));
        simpleDialog.close(1200);
    }

    function currDate() {
        let date = new Date();
        return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-');
    }
    var httpConfig = {
        "headers": {
            "accept": "*/*",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site"
        },
        "referrer": "https://juejin.cn/",
        "referrerPolicy": "strict-origin-when-cross-origin",
        "body": "{}",
        "method": "POST",
        "mode": "cors",
        "credentials": "include"
    };
    var run = (function() { // 用于处理,页面内点击时,异步刷新页面
        let lastUrl;
        let clickTime;
        return {
            once: () => {
                clearTimeout(clickTime);
                clickTime = setTimeout(() => {
                    let $menu = $('.nav-list>.main-nav-list>ul');
                    $menu.find('li:gt(1)').remove();
                    if ($('#ou-chi-mao').length == 0) {
                        $menu.append(`<li class="nav-item link-item" data-v-e29b3e60=""><a href="javascript:void(0)" data-v-e29b3e60="" onclick="choujiang()"
                            style="color: #f009;font-family: serif;font-weight: bolder;" id="ou-chi-mao">血统鉴定</a></li>`);
                    }
                    let sysDate = unsafeWindow.localStorage.juejinDayTask;
                    if (sysDate == null || sysDate != currDate()) {
                        lastUrl = unsafeWindow.location.href;
                        console.log('reload by setInterval', lastUrl);
                        dayTask();
                    }
                }, 500)
            },
            setLastUrl: (url) => {
                lastUrl = url;
            }
        }
    })();
    var simpleDialog = (function() { // 简易的弹窗效果
        return {
            open: () => {
                let html =
                    `<div id="lotteryResultDialog" style="position: absolute;z-index: 9999;width: 200px;height: 200px;left: calc(50vw - 100px);top: calc(30vh - 100px);
                        border-radius: 5px;padding: 10px;overflow: hidden;box-shadow:  0 0 7px 1px #f00a;background: #fffa;">
                        <div style="overflow: auto;width: 240px;height: 200px;"></div>
                     </div>
                     `;
                $('body').append(html);
            },
            addRecord: (msg) => {
                let html = `<div style="padding: 5px;font-size: 16px;display:none">${msg}</div>`;
                $('#lotteryResultDialog>div').prepend(html);
                $('#lotteryResultDialog>div>div:first').show(300).css({
                        'color': 'red'
                    })
                    .siblings().css({
                        'color': '#000'
                    })
            },
            close: (time = 1200) => {
                setTimeout(() => $('#lotteryResultDialog').remove(), time)
            },
            empty: () => {
                $('#lotteryResultDialog').empty();
            }
        }
    })();
}