Greasy Fork

Greasy Fork is available in English.

京东养猪

京东养猪: 签到、捡饲料、喂食

当前为 2020-03-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         京东养猪
// @namespace    http://tampermonkey.net/
// @version      0.4.2
// @description  京东养猪: 签到、捡饲料、喂食
// @author       bob
// @match        https://uua.jr.jd.com/*
// @grant        none
// @note         http://greasyfork.icu/zh-CN/scripts/398000-%E4%BA%AC%E4%B8%9C%E5%85%BB%E7%8C%AA
// ==/UserScript==

// 养猪连接
const jdyzUrl = 'https://uua.jr.jd.com/uc-fe-wxgrowing/cloudpig/index/home/20191030/158432123100870350/?channelLV=qdy&shareId=vZoq-O6FkuUp62HuKheuTcAdoUJQ3Dik&sourceID=240&actflag=1AEE706F1D&isPay=N&utm_source=Android%2aurl%2a1584321232229&utm_medium=jrappshare&utm_term=qqfriends';
// 点击后等待时间
const timeForClick = 1000;
// 定时捡饲料
var pickFoodTask = null;

(function() {
    'use strict';
    // Your code here...
    // 根据class名称,点击对应节点
    function clickByClassName (className, index = 0) {
        var btn = document.getElementsByClassName(className)
        if (btn && btn[index]) {
            btn[index].click();
            return true;
        }
        return false;
    }

    function sleep(timeout) {
        return new Promise(resolve => {
            setTimeout(resolve, timeout);
        });
    }

    // 京东养猪
    if (/https:\/\/uua.jr.jd.com\/uc-fe-wxgrowing\/cloudpig\/index\/home\/20191030\/158432123100870350\/*/.test(window.location.href)) {
        // 签到
        async function signIn () {
            console.log('准备签到')
            // 打开签到
            clickByClassName("main-icon main-icon-sign");
            await sleep(timeForClick);
            // 签到
            clickByClassName("sign-btn");
            await sleep(timeForClick);
            // 关闭签到
            clickByClassName("sign-close");
            // 关闭奖励 TODO
            clickByClassName("precious-btn");
            console.log('签到完成');
        }

        // 捡猪饲料
        function pickFood () {
            console.log('开始捡猪饲料');
            var disableFood = document.getElementsByClassName("main-precious-disable")[0];
            if (disableFood) {
                console.log('待补给饲料,停止捡饲料');
                stopPickFood();
            }
            if (pickFoodTask) {
                return;
            }
            pickFoodTask = setInterval(() => {
                // 点击左边礼物盒
                var res = clickByClassName("precious precious-one precious-pos-1 springback");
                if (!res) {
                    // 点击右边礼物盒
                    clickByClassName("precious precious-one precious-pos-2 springback");
                }
            }, 3000);
        }

        // 停止捡饲料
        function stopPickFood () {
            console.log('停止捡饲料')
            if (pickFoodTask) {
                clearInterval(pickFoodTask);
            }
        }

        // 添加食物
        async function addFood () {
            console.log('开始喂食')
            // 打开饲料仓库
            clickByClassName("draw-trough-img");
            await sleep(timeForClick);

            // 如果是没有饲料,直接关闭
            var empytBtn = document.querySelector('.empty-btn');
            if (empytBtn) {
                empytBtn.click();
                console.log('没有饲料,退出喂食');
                await sleep(timeForClick);
                return false;
            }
            // 饲料详情
            var foodItems = document.getElementsByClassName("feed-item-desc");
            var foodCounts = new Array();
            for (var i=0; i<foodItems.length; i++) {
                var count = foodItems[i].childNodes[1].innerText;
                foodCounts.push({"index": i, "count": count});
            }
            // 饲料按余量降序
            foodCounts.sort((a, b) => {
                return b.count - a.count;
            });
            // 取最大余量喂食
            var index = foodCounts[0].index;
            clickByClassName("feed-item-btn", index);
            // 关闭饲料仓库
            clickByClassName("feed-close");
            await sleep(timeForClick);
        }

        // 是否需要喂食
        function isNeedFood () {
            var needBtn = document.getElementsByClassName("draw-feed-btn")
            if (needBtn && needBtn[0]) {
                return true;
            }
            // 剩余时间 mm:hh:ss 格式
            var residue = document.getElementsByClassName("draw-feed-time");
            if (residue && residue[0]) {
                return !/\d{2}:\d{2}:\d{2}/.test(residue[0].innerText);
            }
            return false;
        }

        // 抽奖
        async function getLucky() {
            var btn = document.querySelector(".foot-icon");
            btn.click();
            console.log('打开抽奖');
            await sleep(2000);
            console.log('已打开抽奖');
            btn = document.querySelector("a[class='luck-btn KinerLotteryBtn start']");
            if (btn) {
                var txt = btn.innerText;
                if (txt.indexOf('免费') == 0) {
                    console.log('免费抽奖');
                    btn.click();
                    await sleep(8000);
                    btn = document.querySelectorAll('.precious-btn');
                    if (btn && btn[0] && btn[0].innerText == '好的') {
                        btn[0].click();
                        await sleep(timeForClick);
                        getLucky();
                    } else {
                        console.log('没有中奖');
                        btn = document.querySelector('.luck-close');
                        btn.click();
                        await sleep(timeForClick);
                    }
                } else {
                    console.log('免费次数用完,退出抽奖');
                    btn = document.querySelector('.luck-close');
                    btn.click();
                    await sleep(timeForClick);
                }
            } else {
                console.log('没有签到抽奖入口');
                btn = document.querySelector('.luck-close');
                btn.click();
                await sleep(timeForClick);
            }
        }

        // 协调喂食和捡饲料
        function checkAndAddFood () {
            setInterval(() => {
                if (isNeedFood()) {
                    stopPickFood();
                    addFood();
                    pickFood();
                } else {
                    console.log('不需要喂食')
                }
            }, 3000);
        }

        // 检查是否可以执行
        function checkCanExecute() {
            var war = document.querySelector('.modal-btn.confirm');
            if (war && war.innerText == '在京东金融App领福利') {
                console.log("被限制只能在京东金融App领福利");
                alert('无法自动养猪,请设置浏览器代理为手机模式、清除浏览器缓存后,刷新页面重试');
                return false;
            }
            return true;
        }

        // 清除浏览器缓存
        function clearCache() {
            var ca = document.cookie.split(';');
            for(var i=0; i<ca.length; i++) {
                var c = ca[i].trim();
                console.log(`document.cookie = "${c}; expires=Thu, 01 Jan 1970 00:00:00 GMT";`);
                document.cookie = `${c}; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
            }
            return "";
        }

        window.addEventListener("load", async () => {
            await sleep(3000);
            if (!checkCanExecute()) {
                console.log('无法执行自动任务');
                return false;
            }
            console.log('开始搞事情');
            signIn();
            await sleep(5000);
            getLucky();
            checkAndAddFood();

            // 下一天凌晨刷新页面,进行新一轮任务
            var date = new Date();
            date.setDate(date.getDate()+1);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(1);
            setTimeout(() => {
                window.location.reload();
            }, (date.getTime() - new Date().getTime()));
            console.log(`计划【${date}】 刷新页面`);
        });
    }

})();