Greasy Fork

Greasy Fork is available in English.

dati

自动答题

当前为 2018-12-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                dati
// @name:zh-CN          大头答题
// @namespace           www.icycat.com
// @description         自动答题
// @description:zh-CN   检测答案自动答题
// @author              冻猫
// @include             https://www.douyu.com/9999
// @include             https://www.douyu.com/1209
// @include             https://www.douyu.com/196
// @version             1.6
// @run-at              document-end
// ==/UserScript==


function exec(fn) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.body.appendChild(script);
}

exec(function() {
    function checkPreview() {
        if (document.querySelector('div[class^="answerPreview"]')) {
            console.log('30秒后开始答题');
            setTimeout(function() {
                console.log('开始检测答案');
                findAnswer(1);
            }, 25000);
            return;
        }
        setTimeout(function() {
            checkPreview();
        }, 1000);
    }

    function findAnswer(t) {
        var json = {};
        json[1] = 0;
        json[2] = 0;
        json[3] = 0;
        var content = document.querySelectorAll('.Barrage-content');
        for (var i = 0; i < content.length; i++) {
            switch (content[i].innerText.substr(0, 1).toUpperCase()) {
                case 'A':
                    json[1]++;
                    break;
                case 'B':
                    json[2]++;
                    break;
                case 'C':
                    json[3]++;
                    break;
            }
        }
        var tempVal = 0,
            tempKey = '';
        for (var key in json) {
            if (json[key] > tempVal) {
                tempKey = key;
                tempVal = json[key];
            }
        }
        if (tempKey) {
            console.log('检测到最多答案为' + tempKey + '!');
            answer(tempKey-1);
            socketAnswer(tempKey-1);
        } else {
            t++;
            if (t < 40) {
                setTimeout(function() {
                    findAnswer(t);
                }, 100);
            } else {
                console.log('没有检测到答案,有C选C了');
                answer(2);
                socketAnswer(2);
            }
        }
    }

    function answer(ans) {
        console.log('准备答题');
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                var li = document.querySelectorAll('div[class^="answerProblem"] ul li');
                if (li.length > 0) {
                    li[ans].click();
                    console.log(new Date().getTime());
                    console.log('答题完成 -- observer click,19分钟后自动刷新网页');
                    setTimeout(function() {
                        window.location.reload();
                    }, 1140000);
                    observer.disconnect();
                }
            });
        });
        observer.observe(document.querySelector('div[class^="answerPreview"]').parentNode, {
            childList: true
        });
    }

    function socketAnswer(ans) {
        window.socketProxy.socketStream.subscribe('compqs', problemHandler);

        function problemHandler(e) {
            var {
                qid
            } = e;
            var uid = document.querySelector('.Avatar-img').getAttribute('uid');
            if (qid) {
                var {
                    room
                } = window.socketProxy.info;
                var msg = {
                    type: 'compqaq',
                    acid: 'act_comdt',
                    qid: qid,
                    aid: ans,
                    rid: room.roomId,
                    uid: uid,
                };
                window.socketProxy.sendMessage(msg);
                console.log(new Date().getTime());
                console.log('答题完成 -- socketStream,19分钟后自动刷新网页');
                setTimeout(function() {
                    window.location.reload();
                }, 1140000);
            }
        }
    }

    checkPreview();
    console.log('开始自动答题 -- 冻猫');

});