User:ZeScript/daily.js

来自滚动的天空Wiki

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
"use strict";
// 本脚本用于折磨API(划掉)用于签到。
(function (mw, $) {
    // try {
    const USER = mw.config.get("wgUserName");
    var api = new mw.Api();
    var today = new Date();
    if ($.cookie("last-time-" + USER.replace(/ /g, "-")) == today.toDateString()) {
        mw.notify("今天签到过了");
        return;
    } else {
        $.cookie("last-time-" + USER.replace(/ /g, "-"), today.toDateString());
    }
    class Daily {
        constructor() {
            this.json = null;
        }
        fetchJson() {
            api.get({
                action: "query",
                prop: "revisions",
                titles: `User:${USER}/daily.json`,
                formatversion: 2,
                rvprop: "content"
            }).done(responce => {
                if (responce.query) {
                	console.log(responce)

                    if (responce.query.pages[0].missing) {
                        this.json = { main: {} };
                    } else {
                        this.json = JSON.parse(responce.query.pages[0].revisions[0].content);
                    }
                    this.addToday()
                    this.commitEdit()
                }
            })
        }
        addToday() {
            
            const year = "" + today.getFullYear();

            const month = today.getMonth();
            const date = "" + today.getDate();
            if (!this.json.main) {
                this.json.main = {};
            }
            if (!this.json.main[year]) {
                this.json.main[year] = [];
            }
            if (!this.json.main[year][month]) {
                for (let m = month; m >= 0; m--) {
                    if (!this.json.main[year][m]) {
                        this.json.main[year][m] = {};
                        var firstDate = new Date(year + "-" + (m < 9 ? "0" + (m + 1) : m + 1) + "-01");
                        this.json.main[year][m].firstDay = firstDate.getDay();
                    }
                }
            }
            if (!this.json.main[year][month][date]) {
                this.json.main[year][month][date] = {};
                this.json.main[year][month][date].online = true;
                mw.notify("今日已签到!");
                this.json.main[year][month][date].time = today.toLocaleTimeString();
                console.log(this);
            }
        }
        commitEdit() {
            api.postWithEditToken({
                action: "edit",
                format: "json",
                title: "User:" + USER + "/daily.json",
                text: JSON.stringify(this.json)
            }).done((responce) => {
            	console.log("签到完成");
            })
        }

    }
    new Daily().fetchJson()
})(mw, jQuery);