MediaWiki:Pagefill.js

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

  • 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
document.getElementById("BatchCreateByTemplate").innerHTML='<textarea id="BCBTexcel" style="width:100%;height:300px"></textarea><div id="BCBTdisplay" class="row"></div><button id="BCBTsubmit" style="padding:0.5em 1em">确认创建</button>'
BCBTMeta = {
    template: "",
    key: [],
    page: {},
    state: "ready",
    titles: [],
    index: 0
};
BCBTFun = {
    initialize: function () {
        if (mw.config.get("wgUserGroups").indexOf("sysop") == -1) {
            document.getElementById("BatchCreateByTemplate").innerHTML="你无权使用这个功能。"
            return;
        }
        document.getElementById("BCBTexcel").oninput = function () {
            let row = this.value.split("\n");
            let title = row[0].split("\t");
            title = title.map(function (e) {
                return e.trim();
            });
            BCBTMeta.template = title[0];
            BCBTMeta.key = [];
            BCBTMeta.page = {};
            for (let i = 1, ilen = title.length; i < ilen; i++) {
                BCBTMeta.key.push(title[i]);
            }
            for (let i = 1, ilen = row.length; i < ilen; i++) {
                let cell = row[i].split("\t");
                cell = cell.map(function (e) {
                    return e.trim();
                });
                if (cell[0]) {
                    BCBTMeta.page[cell[0]] = [];
                    for (let j = 1, jlen = title.length; j < jlen; j++) {
                        BCBTMeta.page[cell[0]].push(cell[j] || "");
                    }
                }
            }
            let BCBTdisplay = document.getElementById("BCBTdisplay");
            BCBTdisplay.innerHTML = "";
            for (let key in BCBTMeta.page) {
                let text =
                    "[[<a href=" +
                    mw.config.values.wgScriptPath +
                    "/" +
                    encodeURIComponent(key) +
                    " title=" +
                    key +
                    " target=_blank>" +
                    key +
                    "</a>]]";
                text +=
                    "\n\n{{" +
                    (BCBTMeta.template
                        ? "<a href=" +
                            mw.config.values.wgScriptPath +
                            "/" +
                            encodeURIComponent("模板:" + BCBTMeta.template) +
                            " title=" +
                            "模板:" +
                            BCBTMeta.template +
                            "  target=_blank>" +
                            BCBTMeta.template +
                            "</a>"
                        : "<span style='color:red'>请确认模板</span>");
                for (let i = 0, ilen = BCBTMeta.key.length; i < ilen; i++) {
                    text += "\n|" + (BCBTMeta.key[i] ? BCBTMeta.key[i] + "=" : "") + BCBTMeta.page[key][i];
                }
                text += "\n}}";
                let pre = document.createElement("pre");
                pre.classList.add("col-md-3");
                pre.dataset.title = key;
                pre.innerHTML = text;
                BCBTdisplay.appendChild(pre);
            }
        };
        document.getElementById("BCBTsubmit").onclick = function () {
            if (BCBTMeta.state == "ready" && confirm("真的确认了吗?")) {
                document.getElementById("BCBTexcel").readOnly = true;
                BCBTMeta.state = "busy";
                BCBTMeta.titles = Object.keys(BCBTMeta.page);
                create();
            }
        };
    }
};
function create() {
    var title = BCBTMeta.titles[BCBTMeta.index++];
    if (!title) {
        console.log("finished!");
        return false;
    }
    let text = "{" + "{" + BCBTMeta.template;
    for (let i = 0, ilen = BCBTMeta.key.length; i < ilen; i++) {
        text += "\n|" + (BCBTMeta.key[i] ? BCBTMeta.key[i] + "=" : "") + BCBTMeta.page[title][i];
    }
    text += "\n}}";
    new mw.Api()
        .postWithToken("csrf", {
            action: "edit",
            title: title,
            text: text
        })
        .then(function (json) {
            // let title = json.edit.title;
            // $.get(mw.config.get("wgCookiePath") + title);
            document.querySelector('#BCBTdisplay pre[data-title="' + title + '"]').classList.add("created");
            return setTimeout(create, 1000);
        })
        .catch(function (e) {
            console.log(e); // "http", "editConfict"
            // BCBTMeta.index--;
            document.querySelector('#BCBTdisplay pre[data-title="' + title + '"]').classList.add("uncertain");
            return setTimeout(create, 5000);
        });
}
window.addEventListener("load", function () {
    BCBTFun.initialize();
});
function getApi(url, callback = false, callbackE = false) {
    let ajax = new XMLHttpRequest();
    if (!callback) {
        ajax.open("GET", url, false);
        ajax.send();
        let json = JSON.parse(ajax.responseText);
        return json;
    }
    ajax.open("GET", url);
    ajax.send();
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
            if (ajax.status == 200) {
                callback(ajax.responseText);
            } else if (ajax.status == 404 && callbackE) {
                callbackE(true);
            }
        }
    };
}
function postFdApi(url, fd, callback = false, callbackE = false) {
    let ajax = new XMLHttpRequest();
    if (!callback) {
        ajax.open("POST", url, false);
        ajax.send(fd);
        let json = JSON.parse(ajax.responseText);
        return json;
    }
    ajax.open("POST", url);
    ajax.send(fd);
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
            if (ajax.status == 200) {
                callback(ajax.responseText);
            } else if (ajax.status == 404 && callbackE) {
                callbackE(true);
            }
        }
    };
}