LLWiki正在建设中,欢迎加入我们

MediaWiki:Gadget-mobileBlock.js

来自LLWiki
跳转到导航 跳转到搜索

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
// 由ResourceLoader直接调用,不可使用ES6语法
// 出于安全考虑,请尽可能使用jQuery代替htmlString
"use strict";
/*global mw, $, OO, wgULS*/
const action = mw.config.get('wgAction'),
    userGroups = mw.config.get( 'wgUserGroups' ),
    specialPage = mw.config.get( 'wgCanonicalSpecialPageName' ),
    script = mw.config.get('wgScript'),
    pagename = mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ),
    relpage = mw.util.wikiUrlencode( mw.config.get( 'wgRelevantPageName' ) ),
    isSysop = userGroups.includes( 'sysop' );
$(function() {
    if (mw.config.get('skin') == 'vector') { return; }
    
    // 页顶图标
    if (action == 'view' && mw.config.get( 'wgIsArticle' )) {
        const restrictionEdit = mw.config.get( 'wgRestrictionEdit' ) || [],
            restrictionMove = mw.config.get( 'wgRestrictionMove' ) || [],
            isProtected = ['autoconfirmed', 'sysop'].includes( restrictionEdit[0] ) ||
                ['autoconfirmed', 'sysop'].includes( restrictionMove[0] ),
            isExist = mw.config.get( 'wgArticleId' ) > 0,
            url = script + '?title=' + pagename,
            options = [];
        var classes = 'page-actions-menus';
        if (isExist) {
            options.push( $('<a>', {href: script + '/special:历史/' + pagename, text: wgULS("历史", "歷史")})
                .prepend('<i class="fa fa-clock"></i>') );
        }
        if (isExist && !mw.config.get( 'wgIsRedirect' )) {
            classes += ' page-actions-menus-l';
            const api = new mw.Api(),
                pageExisting = [wgULS('页面', '頁面'),
                $('<a>', {href: '#'}).click(function(e) { e.preventDefault(); }), '已存在!'];
            options.push( $('<a>', {text: "重定向至此"}).prepend('<i class="fa fa-file-import"></i>')
                .on('click', function() {
                OO.ui.prompt( wgULS('请输入重定向页名称', '請輸入重定向頁名稱'), {actions: [{label: '取消'},
                    {label: wgULS("确定", "確認"), flags: ['primary', 'progressive'], action: 'accept'}]} )
                    .then(function(result) {
                    if (!result) { return; }
                    api.get({action: 'query', titles: result, converttitles: 1, formatversion: 2}).then(function(x) {
                        const converted = x.query.pages[0];
                        if (!converted.missing) {
                            pageExisting[1].text( converted.title ).attr('href', script + '?redirect=no&title=' +
                                mw.util.wikiUrlencode( converted.title ));
                            mw.notify(pageExisting, {type: 'error'});
                            return;
                        }
                        api.create(result, {summary: '使用mobileBlock小工具创建', formatversion: 2},
                            '#重定向 [[' + mw.config.get( 'wgPageName' ) + ']]').then(function() {
                            mw.notify( wgULS('创建成功!', '創建成功!') );
                        }, function(reason) {
                            mw.notify(wgULS('创建失败!错误原因:', '創建失敗!錯誤原因:') + reason, {type: 'error'});
                        });
                    }, function(reason) { mw.apiFailure(reason, wgULS('该页面是否存在', '該頁面是否存在')); });
                });
            }) );
        }
        if (userGroups.includes( restrictionMove[0] || 'user' ) && isExist) {
            options.push( $('<a>', {href: script + '/special:移动页面/' + pagename, text: wgULS("移动", "移動")})
                .prepend('<i class="fa fa-truck"></i>') );
        }
        if (isSysop && isExist) {
            options.push( $('<a>', {href: url + '&action=delete', text: wgULS("删除", "刪除")})
                .prepend('<i class="fa fa-cut"></i>') );
        }
        if (isSysop) {
            options.push( $('<a>', {href: url + '&action=protect', text: wgULS("保护", "保護")})
                .prepend('<i class="fa fa-' + (isProtected ? 'unlock' : 'lock') + '"></i>') );
        }
        if (options.length) {
            const dropdown = $('<div>', {class: classes})
                .append( options.map(function(ele) { return $('<div>', {html: ele}); }) ),
                listMore = $('<li>', {class: "page-actions-menu__list-item"})
                .appendTo( '#page-actions' ).append( dropdown );
            $('<a>', {role: "button", tabIndex: -1, title: "更多功能"}).prependTo( listMore )
                .append('<i class="fa fa-ellipsis-v"></i>')
                .on('focus', function() { dropdown.show(); }).on('blur', function() { dropdown.hide(); });
        }
    }

    /* 添加撤销链接 */
    const textUndo = wgULS("撤销", "復原");
    if (specialPage == "MobileDiff" && $( '.revision-history-prev' ).length && !/\d+\.{3}\d+$/.test(pagename)) {
        const undo = parseInt( pagename.match(/\d+$/) ),
            undoafter = parseInt( $('.revision-history-prev a').attr('href').match(/\d+$/) );
        if (undo > undoafter) {
            $('.mw-mf-diff-info__link-latest a:last-child').after(
                $('<a>', {text: '(' + textUndo + ')',
                    href: script + '?title=' + relpage + '&action=edit&undo=' + undo + '&undoafter=' + undoafter
                })
            );
        }
    }
    else if (specialPage == "History") {
        var list = $('.page-summary > a'),
            oldid = list.map(function() { return parseInt( this.href.match(/\d+$/) ); }).toArray();
        list.filter(function(i) { return i < list.length - 1; }).append(function(i) {
            return $('<span>').css('font-size', '0.9em')
                .append( $('<a>', {text: textUndo,
                    href: script + '?title=' + relpage + '&action=edit&undo=' + oldid[i] + '&undoafter=' + oldid[i + 1]
                }) );
        });
    }
    else if (specialPage == 'Recentchanges') {
        mw.hook( 'wikipage.content' ).add(function($content) {
            console.log('Hook: wikipage.content, 开始添加撤销链接');
            $content.find( '.mw-changeslist-line-inner' ).append(function() {
                const self = $(this);
                if (self.find( '.newpage' ).length) { return; }
                const href = self.find('.mw-changeslist-diff').attr('href'),
                    undo = mw.util.getParamValue('diff', href),
                    undoafter = mw.util.getParamValue('oldid', href),
                    title = mw.util.getParamValue('title', href);
                return $('<span>').append( $('<a>', {text: textUndo,
                    href: script + '?title=' + title + '&action=edit&undo=' + undo + '&undoafter=' + undoafter }) );
            });
        });
    }

    // 添加封禁链接
    const textBlock = wgULS("封禁", "封鎖");
    if (specialPage == "MobileDiff" && isSysop) {
        $( '.mw-mf-user-link' ).after(function() {
            return $('<a>', {href: script + 'special:block/' + $(this).text(), text: '(' + textBlock + ')'});
        });
    }

    // 用户页封禁链接
    if (mw.config.get( 'wgNamespaceNumber' ) == 2 && action == 'view' && isSysop) {
        $('<a>', {text: textBlock, href: '/zh/Special:block/' + mw.config.get( 'wgRelevantUserName' )})
            .appendTo( '.user-links' ).before( ' ' ).wrap( '<li>' ).before( ' ' );
    }
});
// [[category:作为模块的小工具]] [[category:默认开启的小工具]] [[category:需要用户权限的小工具]] [[category:维护工具]] [[category:手机版小工具]]
// {{DEFAULTSORT:mobileBlock.js}}