Greasy Fork

Greasy Fork is available in English.

mooc/优课 查题小组手

进入做题界面自动查询答案并且填充内容

目前为 2022-05-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         mooc/优课 查题小组手
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  进入做题界面自动查询答案并且填充内容
// @author       shulan
// @match        http://www.uooc.net.cn/exam/*
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        window.onload
// @grant        window.console
// @license      MIT
// ==/UserScript==

(() => {
    'use strict';
    var t,
        n,
        e = (function () {
            function n(n, e, o) {
                void 0 === n && (n = ''),
                    void 0 === e && (e = []),
                    void 0 === o && (o = t.Radio),
                    (this.question = n),
                    (this.options = e),
                    (this.type = o);
            }
            return (
                (n.prototype.set_answer = function (t) {
                    this.answer = t;
                }),
                (n.prototype.match_answer = function (t) {
                    return this.options
                        .map(function (t, n) {
                            return [t, n];
                        })
                        .filter(function (n) {
                            var e = n[0];
                            return t.some(function (t) {
                                return new RegExp(e.body).test(t);
                            });
                        })
                        .map(function (t) {
                            return t[0], t[1];
                        });
                }),
                n
            );
        })();
    !(function (t) {
        (t[(t.Radio = 0)] = 'Radio'), (t[(t.Checkbox = 1)] = 'Checkbox'), (t[(t.Judge = 3)] = 'Judge'), (t[(t.InBlank = 2)] = 'InBlank');
    })(t || (t = {})),
        (function (t) {
            (t[(t.NOTFOUND = 0)] = 'NOTFOUND'), (t[(t.NOTMATCH = 1)] = 'NOTMATCH'), (t[(t.MATCHED = 2)] = 'MATCHED');
        })(n || (n = {}));
    var o,
        i =
            ((o = function (t, n) {
                return (
                    (o =
                        Object.setPrototypeOf ||
                        ({ __proto__: [] } instanceof Array &&
                            function (t, n) {
                                t.__proto__ = n;
                            }) ||
                        function (t, n) {
                            for (var e in n) Object.prototype.hasOwnProperty.call(n, e) && (t[e] = n[e]);
                        }),
                    o(t, n)
                );
            }),
            function (t, n) {
                if ('function' != typeof n && null !== n) throw new TypeError('Class extends value ' + String(n) + ' is not a constructor or null');
                function e() {
                    this.constructor = t;
                }
                o(t, n), (t.prototype = null === n ? Object.create(n) : ((e.prototype = n.prototype), new e()));
            }),
        r = (function () {
            function n() {}
            return (
                (n.prototype.parse = function () {
                    return $('.queBox')
                        .map(function (n, e) {
                            return {
                                type: t.Radio,
                                question: $(e).find('.ti-q-c').text(),
                                options: $(e)
                                    .find('.ti-alist .ti-a')
                                    .map(function (t, n) {
                                        var e = $(n),
                                            o = e.find('.ti-a-i').text().trim(),
                                            i = e.find('.ti-a-c').text().trim();
                                        return { prefix: o.slice(0, o.indexOf('.')), body: i };
                                    })
                                    .toArray(),
                            };
                        })
                        .toArray()
                        .map(function (t, n) {
                            return new a(n, { question: t.question, options: t.options, type: t.type });
                        });
                }),
                n
            );
        })(),
        a = (function (t) {
            function n(n, e) {
                var o = t.call(this, e.question, e.options) || this;
                return (o.position = n), o;
            }
            return (
                i(n, t),
                (n.prototype.select = function () {
                    var t,
                        n = this;
                    'number' == typeof this.position &&
                        (null === (t = this.answer) ||
                            void 0 === t ||
                            t.map(function (t) {
                                $('.queBox .ti-alist:eq('.concat(n.position, ') .ti-a .ti-a-i [type=radio]:eq(').concat(t, ')')).click();
                            }));
                }),
                n
            );
        })(e);
    function c(t) {
        return (
            (n = this),
            (e = void 0),
            (i = function () {
                return (function (t, n) {
                    var e,
                        o,
                        i,
                        r,
                        a = {
                            label: 0,
                            sent: function () {
                                if (1 & i[0]) throw i[1];
                                return i[1];
                            },
                            trys: [],
                            ops: [],
                        };
                    return (
                        (r = { next: c(0), throw: c(1), return: c(2) }),
                        'function' == typeof Symbol &&
                            (r[Symbol.iterator] = function () {
                                return this;
                            }),
                        r
                    );
                    function c(r) {
                        return function (c) {
                            return (function (r) {
                                if (e) throw new TypeError('Generator is already executing.');
                                for (; a; )
                                    try {
                                        if (
                                            ((e = 1),
                                            o &&
                                                (i = 2 & r[0] ? o.return : r[0] ? o.throw || ((i = o.return) && i.call(o), 0) : o.next) &&
                                                !(i = i.call(o, r[1])).done)
                                        )
                                            return i;
                                        switch (((o = 0), i && (r = [2 & r[0], i.value]), r[0])) {
                                            case 0:
                                            case 1:
                                                i = r;
                                                break;
                                            case 4:
                                                return a.label++, { value: r[1], done: !1 };
                                            case 5:
                                                a.label++, (o = r[1]), (r = [0]);
                                                continue;
                                            case 7:
                                                (r = a.ops.pop()), a.trys.pop();
                                                continue;
                                            default:
                                                if (!((i = (i = a.trys).length > 0 && i[i.length - 1]) || (6 !== r[0] && 2 !== r[0]))) {
                                                    a = 0;
                                                    continue;
                                                }
                                                if (3 === r[0] && (!i || (r[1] > i[0] && r[1] < i[3]))) {
                                                    a.label = r[1];
                                                    break;
                                                }
                                                if (6 === r[0] && a.label < i[1]) {
                                                    (a.label = i[1]), (i = r);
                                                    break;
                                                }
                                                if (i && a.label < i[2]) {
                                                    (a.label = i[2]), a.ops.push(r);
                                                    break;
                                                }
                                                i[2] && a.ops.pop(), a.trys.pop();
                                                continue;
                                        }
                                        r = n.call(t, a);
                                    } catch (t) {
                                        (r = [6, t]), (o = 0);
                                    } finally {
                                        e = i = 0;
                                    }
                                if (5 & r[0]) throw r[1];
                                return { value: r[0] ? r[1] : void 0, done: !0 };
                            })([r, c]);
                        };
                    }
                })(this, function (n) {
                    return [
                        2,
                        new Promise(function (n, e) {
                            GM_xmlhttpRequest({
                                method: 'POST',
                                url: 'http://cx.icodef.com/wyn-nb',
                                headers: { 'Content-Type': 'application/x-www-form-urlencoded', Authorization: '' },
                                data: 'question='.concat(encodeURIComponent(t.question)),
                                onload: function (t) {
                                    n($.parseJSON(t.responseText) || {});
                                },
                                onerror: function (t) {
                                    e(t);
                                },
                            });
                        }),
                    ];
                });
            }),
            new ((o = void 0) || (o = Promise))(function (t, r) {
                function a(t) {
                    try {
                        s(i.next(t));
                    } catch (t) {
                        r(t);
                    }
                }
                function c(t) {
                    try {
                        s(i.throw(t));
                    } catch (t) {
                        r(t);
                    }
                }
                function s(n) {
                    var e;
                    n.done
                        ? t(n.value)
                        : ((e = n.value),
                          e instanceof o
                              ? e
                              : new o(function (t) {
                                    t(e);
                                })).then(a, c);
                }
                s((i = i.apply(n, e || [])).next());
            })
        );
        var n, e, o, i;
    }
    var s = (function () {
            function t(t) {
                this.quetions = t;
            }
            return (
                (t.from = function (n) {
                    return new t(n.parse());
                }),
                t
            );
        })(),
        u = (function () {
            function t() {
                (this.container = $(
                    '<div class="container" style="position: fixed;right: 0;top: 0;width: 500px;max-height: 400px;background: #fff;overflow: hidden auto;">\n        <div class="controller"></div>\n        <div class="list" style="position: relative;">\n            <table class="header-fixed" style="height: 20px; width: 100%;background: #fff;">\n                <tr>\n                    <td width="50px">序号</td>\n                    <td width="300px" style="padding: 5px 10px" >问题</td>\n                    <td width="150px">答案</td>\n                </tr>\n            </table>\n            <div style="overflow: hidden auto; max-height: 300px;">\n                <table class="listarea"></table>\n            </div>\n        </div>\n    </div>'
                )),
                    (this.controller = this.container.find('.controller')),
                    (this.listarea = this.container.find('.list table.listarea')),
                    this.init();
            }
            return (
                (t.prototype.init = function () {
                    this.windowController();
                }),
                (t.prototype.appendItem = function (t, e) {
                    var o,
                        i =
                            (((o = {})[n.NOTFOUND] = 'rgba(255, 0, 0, 0.3)'),
                            (o[n.NOTMATCH] = 'rgba(0, 255, 0, 0.3)'),
                            (o[n.MATCHED] = 'rgba(0, 0, 255, 0.3)'),
                            o),
                        r = $(
                            '<tr style="background: '
                                .concat(i[e], '; color: rgba(0,0,0, 0.71);">\n            <td width="50px">')
                                .concat(t.position + 1, '</td>\n            <td width="300px" style="padding: 5px 10px">')
                                .concat(t.question, '</td>\n            <td width="150px">')
                                .concat(t.rawAnswer ? t.rawAnswer : '未找到答案', '</td>\n        </tr>')
                        );
                    this.listarea.append(r);
                }),
                (t.prototype.windowController = function () {
                    var t = this,
                        n = $(
                            '<div style="display: flex; justify-content: flex-end; width: 100%; align-items: center; align-content: center; font-size: 24px;">\n            <div style="width: 20px; height: 20px; padding-left: 5px; line-height: 20px;" class="windowToMin">-</div>\n            <div style="width: 20px; height: 20px; padding-left: 5px; line-height: 20px;" class="windowClose">x</div>\n        </div>'
                        );
                    this.controller.append(n);
                    var e = $(
                        '<div class="openIcon" style="width: 20px; height: 20px; position: fixed; right: 50px; top: 50px; background: red;"></div>'
                    );
                    $(document.body).append(e),
                        e.hide(),
                        e.on('click', function () {
                            t.container.show(), e.hide();
                        }),
                        n.find('.windowClose').on('click', function () {
                            t.container.hide();
                        }),
                        n.find('.windowToMin').on('click', function () {
                            t.container.hide(), e.show();
                        });
                }),
                t
            );
        })();
    window.addEventListener('load', function () {
        setTimeout(function () {
            !(function () {
                var t, e, o, i;
                (t = this),
                    (e = void 0),
                    (i = function () {
                        var t, e, o, i, a, l, p, d, f, h;
                        return (function (t, n) {
                            var e,
                                o,
                                i,
                                r,
                                a = {
                                    label: 0,
                                    sent: function () {
                                        if (1 & i[0]) throw i[1];
                                        return i[1];
                                    },
                                    trys: [],
                                    ops: [],
                                };
                            return (
                                (r = { next: c(0), throw: c(1), return: c(2) }),
                                'function' == typeof Symbol &&
                                    (r[Symbol.iterator] = function () {
                                        return this;
                                    }),
                                r
                            );
                            function c(r) {
                                return function (c) {
                                    return (function (r) {
                                        if (e) throw new TypeError('Generator is already executing.');
                                        for (; a; )
                                            try {
                                                if (
                                                    ((e = 1),
                                                    o &&
                                                        (i = 2 & r[0] ? o.return : r[0] ? o.throw || ((i = o.return) && i.call(o), 0) : o.next) &&
                                                        !(i = i.call(o, r[1])).done)
                                                )
                                                    return i;
                                                switch (((o = 0), i && (r = [2 & r[0], i.value]), r[0])) {
                                                    case 0:
                                                    case 1:
                                                        i = r;
                                                        break;
                                                    case 4:
                                                        return a.label++, { value: r[1], done: !1 };
                                                    case 5:
                                                        a.label++, (o = r[1]), (r = [0]);
                                                        continue;
                                                    case 7:
                                                        (r = a.ops.pop()), a.trys.pop();
                                                        continue;
                                                    default:
                                                        if (!((i = (i = a.trys).length > 0 && i[i.length - 1]) || (6 !== r[0] && 2 !== r[0]))) {
                                                            a = 0;
                                                            continue;
                                                        }
                                                        if (3 === r[0] && (!i || (r[1] > i[0] && r[1] < i[3]))) {
                                                            a.label = r[1];
                                                            break;
                                                        }
                                                        if (6 === r[0] && a.label < i[1]) {
                                                            (a.label = i[1]), (i = r);
                                                            break;
                                                        }
                                                        if (i && a.label < i[2]) {
                                                            (a.label = i[2]), a.ops.push(r);
                                                            break;
                                                        }
                                                        i[2] && a.ops.pop(), a.trys.pop();
                                                        continue;
                                                }
                                                r = n.call(t, a);
                                            } catch (t) {
                                                (r = [6, t]), (o = 0);
                                            } finally {
                                                e = i = 0;
                                            }
                                        if (5 & r[0]) throw r[1];
                                        return { value: r[0] ? r[1] : void 0, done: !0 };
                                    })([r, c]);
                                };
                            }
                        })(this, function (w) {
                            switch (w.label) {
                                case 0:
                                    for (i in ((t = s.from(new r())), (e = new u()), $(document.body).append(e.container), (o = []), t.quetions))
                                        o.push(i);
                                    (a = 0), (w.label = 1);
                                case 1:
                                    if (!(a < o.length)) return [3, 7];
                                    (l = o[a]), (p = t.quetions[l]), (d = void 0), (w.label = 2);
                                case 2:
                                    return w.trys.push([2, , 4, 6]), console.group(''.concat(Number(l) + 1, ': ').concat(p.question)), [4, c(p)];
                                case 3:
                                    return (
                                        (f = w.sent()),
                                        console.log(f),
                                        (d = n.NOTFOUND),
                                        1 !== f.code
                                            ? (0 === f.code ? console.log('发生错误') : -1 === f.code && console.log('未找到答案'), [3, 6])
                                            : (console.log(f.data),
                                              (p.rawAnswer = f.data),
                                              (h = p.match_answer([f.data])),
                                              (d = n.NOTMATCH),
                                              h.length
                                                  ? ((d = n.MATCHED), p.set_answer(h), console.log(h), p.select(), [3, 6])
                                                  : (console.log('没匹配到答案'), [3, 6]))
                                    );
                                case 4:
                                    return (
                                        console.groupEnd(),
                                        e.appendItem(p, d),
                                        [
                                            4,
                                            ((y = 3e3),
                                            new Promise(function (t) {
                                                return setTimeout(function () {
                                                    return t(void 0);
                                                }, y);
                                            })),
                                        ]
                                    );
                                case 5:
                                    return w.sent(), [7];
                                case 6:
                                    return a++, [3, 1];
                                case 7:
                                    return [2];
                            }
                            var y;
                        });
                    }),
                    new ((o = void 0) || (o = Promise))(function (n, r) {
                        function a(t) {
                            try {
                                s(i.next(t));
                            } catch (t) {
                                r(t);
                            }
                        }
                        function c(t) {
                            try {
                                s(i.throw(t));
                            } catch (t) {
                                r(t);
                            }
                        }
                        function s(t) {
                            var e;
                            t.done
                                ? n(t.value)
                                : ((e = t.value),
                                  e instanceof o
                                      ? e
                                      : new o(function (t) {
                                            t(e);
                                        })).then(a, c);
                        }
                        s((i = i.apply(t, e || [])).next());
                    });
            })();
        }, 1e3);
    });
})();