User:Jimmy Xu/scripts/cactions.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

        importStylesheet('User:Jimmy_Xu/scripts/cactions.css');
// "Fail gracefully" if skin not supported
switch (mw.config.get('skin')) {
    case 'modern':
    case 'monobook':
    case 'vector':

        // Global variables
        var _cactions = {
            admin: mw.config.get('wgUserGroups').join().indexOf('sysop') > -1 ? true : false,
            areqs: [],
            hovms: mw.config.get('skin') == 'vector' ? 50 : 400,
            menus: [],
            mouse: null,
            pname: encodeURIComponent(mw.config.get('wgPageName')),
            timer: [],
            vectr: mw.config.get('skin') == 'vector' ? true : false
        };

        function xhr(request, url, orsc) {
            with(request) {
                open('GET', url, true);
                onreadystatechange = orsc;
                send(null);
            }
        }

        // Find absolute position of element
        function findPos(eid, offset) {
            var obj = document.getElementById(eid),
                pos = [0, 0];
            do with(obj) {
                pos[0] += offsetLeft;
                pos[1] += offsetTop;
            }
            while (obj = obj.offsetParent);
            pos[0] += offset[0];
            pos[1] += offset[1];
            return pos;
        }

        // Create menu div element
        function createMenu(mid, mtitle, vectorise, html) {
            var menu = document.createElement('div');
            with(menu) {
                id = 'opt-' + mid;
                className = 'ca-menu';
                style.display = 'none';
            }
            menu.onmouseover = function() {
                showMenu('opt-' + mid)
            };
            menu.onmouseout = function() {
                hideMenu('opt-' + mid)
            };

            var elements = {
                ul: document.createElement('ul'),
                li: null,
                a: null,
                txt: null
            };
            with(elements) {
                for (var i = 0; i < html.length; i++)
                    if (html[i].length) {
                        li = document.createElement('li');
                        li.id = html[i][0];
                        a = document.createElement('a');
                        a.href = html[i][2];
                        txt = document.createTextNode(html[i][1]);
                        a.appendChild(txt);
                        li.appendChild(a);
                        ul.appendChild(li);
                    }
                menu.appendChild(ul);
            }

            document.body.appendChild(menu);
            if (vectorise) createTab(mid, mtitle);

            return 'opt-' + mid;
        }

        // Create cactions LI tab
        function createTab(mid, mtitle) {
            if (_cactions.vectr) {
                var cid = 'p-' + mid;
                var elements = {
                    div: document.createElement('div'),
                    h3: document.createElement('h3'),
                    span: document.createElement('span'),
                    a: document.createElement('a'),
                    txt: null
                };
                with(elements) {
                    div.id = cid;
                    div.className = 'vectorMenu extraMenu';

                    txt = document.createTextNode(mtitle);
                    span.appendChild(txt);
                    h3.appendChild(span);

                    a.href = '#';
                    h3.onmouseover = function() {
                        showMenu('opt-' + mid, findPos(cid, [0, 40]))
                    };
                    h3.onmouseout = function() {
                        hideMenu('opt-' + mid)
                    };

                    span = document.createElement('span');
                    a.appendChild(span);
                    h3.appendChild(a);

                    div.appendChild(h3);
                    document.getElementById('right-navigation').insertBefore(div, document.getElementById('p-search'));
                }
            } else {
                var cid = 'ca-' + mid;
                var elements = {
                    li: document.createElement('li'),
                    a: document.createElement('a'),
                    txt: document.createTextNode(mtitle)
                };
                with(elements) {
                    li.id = cid;
                    a.href = '#';
                    a.onmouseover = function() {
                        showMenu('opt-' + mid, findPos(cid, [-10, 20]))
                    };
                    a.onmouseout = function() {
                        hideMenu('opt-' + mid)
                    };
                    a.appendChild(txt);
                    li.appendChild(a);

                    document.getElementById('p-cactions').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].appendChild(li);
                }
            }
        }

        // CSS hide elements
        function hideElements(elements, conditionals) {
            var i;
            if (typeof(conditionals) == 'undefined') {
                for (i = 0; i < elements.length; i++)
                    if (document.getElementById(elements[i])) document.getElementById(elements[i]).style.display = 'none';
            } else
                for (i = 0; i < elements.length; i++)
                    if (document.getElementById(elements[i])) {
                        document.getElementById(elements[i]).style.display = 'none';
                        if (conditionals[i]) document.getElementById(conditionals[i]).style.display = 'none';
                    }
        }

        // Show/hide menu functions
        function showMenu(mid, pos) {
            with(_cactions) {
                mouse = mid;
                if (pos)
                    for (var i = 0; i < menus.length; i++) {
                        if (timer[menus[i]]) {
                            clearTimeout(timer[menus[i]]);
                            timer[menus[i]] = null;
                        }
                        if (mid.replace(/-[^-]+$/, '') == menus[i]) continue;
                        document.getElementById(menus[i]).style.display = 'none';
                    }
                if (!timer[mid]) with(document.getElementById(mid).style) {
                    display = '';
                    if (pos) {
                        left = pos[0] + 'px';
                        top = pos[1] + 'px';
                    }
                } else {
                    clearTimeout(timer[mid]);
                    timer[mid] = null;
                }
            }
        }

        function hideMenu(mid) {
            with(_cactions) {
                if (mid == mouse.replace(/-[^-]+$/, '')) timer[mid] = null;

                if (timer[mid]) {
                    timer[mid] = null;
                    document.getElementById(mid).style.display = 'none';
                    if (mid == mouse && mid.search(/opt-.*-/) != -1) document.getElementById(mid.replace(/-[^-]+$/, '')).style.display = 'none';
                } else timer[mid] = setTimeout(function() {
                    hideMenu(mid);
                }, hovms);
            }
        }

        // Delink element
        function removeLink(eid) {
            var element = document.getElementById(eid);
            if (!element.getElementsByTagName('a').length) return false;

            var a = element.getElementsByTagName('a')[0];
            element.appendChild(a.removeChild(a.firstChild));
            element.removeChild(a);

            element.className = 'ca-disabled';
        }

        // User options hook
        jQuery(function() {
            switch (mw.config.get('wgNamespaceNumber')) {
                case 2:
                case 3:
                    _cactions.uname = encodeURIComponent(mw.config.get('wgTitle').split('/')[0].replace(/ /g, '_'));
            }
            if (mw.config.get('wgCanonicalSpecialPageName') == 'Contributions') {
                for (var i = 0, hl; hl = document.getElementById('contentSub').getElementsByTagName('a')[i]; i++) {
                    if (hl.href.indexOf('user=') > -1) {
                        _cactions.uname = hl.href.split('user=')[1].split('&amp;')[0];
                        break;
                    }
                }
            }

            if (_cactions.uname) {
                with(_cactions) {
                    var script = mw.config.get('wgScript');
                    menus[menus.length] = createMenu('user', '用户', true, Array(
                        ['c-u-logs', '用户日志 >', '#'],
                        ['c-u-rfx', '链接到RfX >', '#'],
                        ['c-u-blocks', '封禁 >', '#'],
                        ['c-u-contribs', '用户贡献', script + '?title=Special:Contributions/' + uname + '&action=view'],
                        ['c-u-editcount', '编辑次数', '//tools.wmflabs.org/supercount/index.php?project=zh.wikipedia&user=' + uname.replace(/_/g, '+')],
                        ['c-u-editsum', '编辑摘要', '//tools.wmflabs.org/xtools/editsummary/index.php?lang=zh&wiki=wikipedia&name=' + uname.replace(/_/g, '+')],
                        //['c-u-wcuser', 'Edit analysis', 'http://en.wikichecker.com/user/?l=all&t=' + uname],
                        ['c-u-sul', '全域帐户', script + '?title=Special:CentralAuth/' + uname + '&action=view'],
                        ['c-u-subpages', '用户空间', script + '?title=Special:PrefixIndex/User:' + uname + '/&action=view'],
                        ['c-u-email', '电邮用户', script + '?title=Special:EmailUser/' + uname + '&action=view'],
                        ['c-u-groups', '用户群组', script + '?title=Special:ListUsers&action=view&limit=1&username=' + uname],
                        //['c-u-rightslog', '权限更改', script + '?title=Special:Log&action=view&type=rights&page=User:' + uname]
                        ['c-u-rightslog', '更改权限', script + '?title=Special:UserRights&user=' + uname]
                    ));

                    menus[menus.length] = createMenu('user-logs', '', false, Array(
                        ['c-ul-logs', '所有公共日志', script + '?title=Special:Log&action=view&user=' + uname],
                        ['c-ul-blocks', '查封日志', script + '?title=Special:Log&action=view&type=block&user=' + uname],
                        ['c-ul-deletes', '删除日志', script + '?title=Special:Log&action=view&type=delete&user=' + uname],
                        ['c-ul-moves', '移动日志', script + '?title=Special:Log&action=view&type=move&user=' + uname],
                        ['c-ul-patrols', '巡查日志', script + '?title=Special:Log&action=view&type=patrol&user=' + uname],
                        ['c-ul-protects', '保护日志', script + '?title=Special:Log&action=view&type=protect&user=' + uname],
                        ['c-ul-uploads', '上传日志', script + '?title=Special:Log&action=view&type=upload&user=' + uname],
                        ['c-ul-rights', '用户权限日志', script + '?title=Special:Log&action=view&type=rights&user=' + uname]
                    ));

                    menus[menus.length] = createMenu('user-rfx', '', false, Array(
                        ['c-ux-rfa', 'RfAs', script + '?title=Special:PrefixIndex/Wikipedia:申请成为管理员/' + uname + '&action=view'],
                        ['c-ux-rfb', 'RfBs', script + '?title=Special:PrefixIndex/Wikipedia:申请成为行政员/' + uname + '&action=view'],
                        ['c-ux-rfcu', 'RfCU', script + '?title=Special:PrefixIndex/Wikipedia:申请成为用户查核员/' + uname + '&action=view'],
                        ['c-ux-rfo', 'RfO', script + '?title=Special:PrefixIndex/Wikipedia:申请成为监督员/' + uname + '&action=view']
                    ));

                    menus[menus.length] = createMenu('user-blocks', '', false, Array(
                        admin ? ['c-ub-block', '查封用户', script + '?title=Special:BlockIP/' + uname + '&action=view'] : [],
                        admin ? ['c-ub-unblock', '解封用户', script + '?title=Special:IPBlockList&action=unblock&ip=' + uname] : [],
                        ['c-ub-ipblock', '查看封禁', script + '?title=Special:IPBlockList&action=view&ip=' + uname],
                        ['c-ub-blocklog', '查封日志', script + '?title=Special:Log&action=view&type=block&page=User:' + uname]
                    ));

                    if (uname.search(/(?:\d{1,3}\.){3}\d{1,3}/) == 0) {
                        areqs['ip'] = new XMLHttpRequest();
                        xhr(areqs['ip'], mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=blocks&bkusers=' + uname + '&bkprop=id&xhr=' + Math.random(), function() {
                            with(areqs['ip']) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (api.query.blocks.length) {
                                    hideElements(['c-ub-block']);
                                    document.getElementById('c-ub-ipblock').getElementsByTagName('a')[0].style.color = '#EE1111';
                                } else {
                                    hideElements(['c-ub-unblock']);
                                    removeLink('c-ub-ipblock');
                                }
                            }
                        });
                    } else {
                        areqs.user = new XMLHttpRequest();
                        xhr(areqs.user, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=users&ususers=' + uname + '&usprop=blockinfo|groups&xhr=' + Math.random(), function() {
                            with(areqs.user) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                with(api.query.users[0]) {
                                    if (typeof(missing) != 'undefined') hideElements(['ca-user']);
                                    else {
                                        if (typeof(blockedby) != 'undefined') {
                                            hideElements(['c-ub-block']);
                                            document.getElementById('c-ub-ipblock').getElementsByTagName('a')[0].style.color = '#EE1111';
                                        } else {
                                            hideElements(['c-ub-unblock']);
                                            removeLink('c-ub-ipblock');
                                        }

                                        if (typeof(groups) == 'undefined' || groups.join().indexOf('sysop') == -1) hideElements(['c-ul-blocks', 'c-ul-deletes', 'c-ul-protects', 'c-ul-rights']);
                                    }
                                }
                            }
                        });

                        areqs.rfa = new XMLHttpRequest();
                        xhr(areqs.rfa, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=allpages&apprefix=申请成为管理员%2F' + uname + '&apnamespace=4&aplimit=1&xhr=' + Math.random(), function() {
                            with(areqs.rfa) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (!api.query.allpages.length) removeLink('c-ux-rfa');
                            };
                        });

                        areqs.rfb = new XMLHttpRequest();
                        xhr(areqs.rfb, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=allpages&apprefix=申请成为行政员%2F' + uname + '&apnamespace=4&aplimit=1&xhr=' + Math.random(), function() {
                            with(areqs.rfb) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (!api.query.allpages.length) removeLink('c-ux-rfb');
                            }
                        });

                        areqs.rfcu = new XMLHttpRequest();
                        xhr(areqs.rfcu, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=allpages&apprefix=申请成为用户查核员%2F' + uname + '&apnamespace=4&aplimit=1&xhr=' + Math.random(), function() {
                            with(areqs.rfcu) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (!api.query.allpages.length) removeLink('c-ux-rfcu');
                            }
                        });

                        areqs.rfo = new XMLHttpRequest();
                        xhr(areqs.rfo, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=allpages&apprefix=申请成为监督员%2F' + uname + '&apnamespace=4&aplimit=1&xhr=' + Math.random(), function() {
                            with(areqs.rfo) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (!api.query.allpages.length) removeLink('c-ux-rfo');
                            }
                        });

                        areqs.uspace = new XMLHttpRequest();
                        xhr(areqs.uspace, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=allpages&apprefix=' + uname + '%2F&apnamespace=2&aplimit=1&xhr=' + Math.random(), function() {
                            with(areqs.uspace) if (readyState == 4 && status == 200) {
                                var api = eval('(' + responseText + ')');
                                if (!api.query.allpages.length) removeLink('c-u-subpages');
                            }
                        });
                    }

                    areqs.ublocks = new XMLHttpRequest();
                    xhr(areqs.ublocks, mw.config.get('wgScriptPath') + '/api.php?format=json&action=query&list=logevents&letype=block&letitle=User:' + uname + '&lelimit=1&xhr=' + Math.random(), function() {
                        with(areqs.ublocks) if (readyState == 4 && status == 200) {
                            var api = eval('(' + responseText + ')');
                            if (!api.query.logevents.length) removeLink('c-ub-blocklog');
                        }
                    });
                }

                // FIXME hideElements(['t-contributions', 't-log', 't-emailuser']);

                document.getElementById('c-u-logs').onmouseover = function() {
                    showMenu('opt-user-logs', findPos('c-u-logs', [40, 0]))
                };
                document.getElementById('c-u-logs').onmouseout = function() {
                    hideMenu('opt-user-logs')
                };
                document.getElementById('c-u-logs').style.fontWeight = 'bold';

                document.getElementById('c-u-rfx').onmouseover = function() {
                    showMenu('opt-user-rfx', findPos('c-u-rfx', [40, 0]))
                };
                document.getElementById('c-u-rfx').onmouseout = function() {
                    hideMenu('opt-user-rfx')
                };
                document.getElementById('c-u-rfx').style.fontWeight = 'bold';
                document.getElementById('opt-user-rfx').style.width = '50px';

                document.getElementById('c-u-blocks').onmouseover = function() {
                    showMenu('opt-user-blocks', findPos('c-u-blocks', [40, 0]))
                };
                document.getElementById('c-u-blocks').onmouseout = function() {
                    hideMenu('opt-user-blocks')
                };
                document.getElementById('c-u-blocks').style.fontWeight = 'bold';

                if (_cactions.uname.search(/(?:\d{1,3}\.){3}\d{1,3}/) == 0) hideElements(['c-u-logs', 'c-ux-rfa', 'c-ux-rfb', 'c-u-editcount', 'c-u-editsum', 'c-u-wcuser', 'c-u-subpages', 'c-u-email', 'c-u-groups', 'c-u-rightslog']);
            }
        });

        // Page options hook
        jQuery(function() {
            if (!mw.config.get('wgCanonicalSpecialPageName')) {
                with(_cactions) {
                    var script = mw.config.get('wgScript'),
                        articleId = mw.config.get('wgArticleId')
                    menus[menus.length] = createMenu('page', '页面', true, Array(
                        ['c-p-logs', '页面日志 >', '#'],
                        articleId ? ['c-p-history', '历史', script + '?title=' + pname + '&action=history'] : [],
                        articleId ? ['c-p-move', '移动', script + '?title=Special:Movepage/' + pname + '&action=view'] : [],
                        !vectr ? ['c-p-watch', '监视', script + '?title=' + pname + '&action=watch'] : [],
                        !vectr ? ['c-p-unwatch', '停止监视', script + '?title=' + pname + '&action=unwatch'] : [],
                        admin ? ['c-p-protect', '保护', script + '?title=' + pname + '&action=protect'] : [],
                        admin ? ['c-p-unprotect', '取消保护', script + '?title=' + pname + '&action=unprotect'] : [],
                        admin ? ['c-p-delete', '删除', script + '?title=' + pname + '&action=delete'] : [],
                        admin ? ['c-p-undelete', '恢复', script + '?title=Special:Undelete/' + pname + '&action=view'] : [],
                        articleId ? ['c-p-diff', '最近更改', script + '?title=' + pname + '&action=view&diff=' + mw.config.get('wgCurRevisionId')] : [],
                        articleId ? ['c-p-editzero', '编辑首段', script + '?title=' + pname + '&action=edit&section=0'] : [],
                        articleId ? ['c-p-newsection', '新评论', script + '?title=' + pname + '&action=edit&section=new'] : [],
                        //articleId ? ['c-p-wcpage', 'Page analysis', 'http://en.wikichecker.com/article/?a=' + pname] : [],
                        ['c-p-purge', '更新缓存', script + '?title=' + pname + '&action=purge']
                    ));

                    menus[menus.length] = createMenu('page-logs', '', false, Array(
                        ['c-pl-logs', '所有公共日志', script + '?title=Special:Log&action=view&page=' + pname],
                        ['c-pl-deletes', '删除日志', script + '?title=Special:Log&type=delete&page=' + pname],
                        ['c-pl-moves', '移动日志', script + '?title=Special:Log&action=view&type=move&page=' + pname],
                        articleId ? ['c-pl-patrols', '巡查日志', script + '?title=Special:Log&action=view&type=patrol&page=' + pname] : [],
                        ['c-pl-protects', '保护日志', script + '?title=Special:Log&action=view&type=protect&page=' + pname],
                        mw.config.get('wgNamespaceNumber') == 6 ? ['c-pl-uploads', '上传日志', script + '?title=Special:Log&action=view&type=upload&page=' + pname] : []
                    ));
                }

                // FIXME hideElements(['ca-protect', 'ca-unprotect', 'ca-delete', 'ca-undelete', 'ca-history', 'ca-move'], ['c-p-unprotect', 'c-p-protect', 'c-p-undelete', 'c-p-delete']);
                // if (!_cactions.vectr) hideElements(['ca-watch', 'ca-unwatch'], ['c-p-unwatch', 'c-p-watch']);
                hideElements(['c-p-history', 'c-p-move', 'c-p-unprotect', 'c-p-protect', 'c-p-undelete', 'c-p-delete', 'c-p-unwatch', 'c-p-watch']);

                document.getElementById('c-p-logs').onmouseover = function() {
                    showMenu('opt-page-logs', findPos('c-p-logs', [40, 0]))
                };
                document.getElementById('c-p-logs').onmouseout = function() {
                    hideMenu('opt-page-logs')
                };
                document.getElementById('c-p-logs').style.fontWeight = 'bold';
            }
        });
}