「MediaWiki:Common.js」の版間の差分

提供: 萌えっ娘百科事典
移動先: 案内検索
(暂时还原 // Edit Via InPageEdit)
 
241行目: 241行目:
 
         });
 
         });
 
         // Avatar
 
         // Avatar
         if (mw.config.get("wgCanonicalSpecialPageName") === "UploadAvatar") {
+
         if (["ViewAvatar", "UploadAvatar"].includes(mw.config.get("wgCanonicalSpecialPageName"))) {
             location.href = "/cm:Special:UploadAvatar"
+
            var url = new mw.Uri();
        }
+
            url.host = url.host.replace(/^[^\.]+/g, "commons");
        if (mw.config.get("wgCanonicalSpecialPageName") === "ViewAvatar") {
+
             url.path = mw.config.get("wgScript");
             location.href = "/cm:Special:ViewAvatar"
+
            url.query.title = mw.config.get("wgCanonicalNamespace") + ":" + mw.config.get("wgCanonicalSpecialPageName");
 +
             location.replace(url);
 
         }
 
         }
 
         // workaround for template:heimu in safari
 
         // workaround for template:heimu in safari

2022年6月5日 (日) 16:26時点における最新版

(function($, mw) {
    //Extension:MultimediaViewer workaround for translucent
    function multimediaViewer() {
        $('a.image img[srcset][data-file-width][data-file-height], .mw-mmv-filepage-buttons a.mw-mmv-view-expanded').on('click', function() {
            if ($(this).closest('.TabLabelText')[0] || $(this).closest('a').closest('.mw-customtoggle')[0]) return;
            var scrollTop = document.body.scrollTop;
            $('html, body').add(document).on('scroll.multimediaViewer', function() {
                //console.debug(document.body.scrollTop, scrollTop);
                window.setTimeout(function() {
                    document.body.scrollTop = scrollTop;
                }, 0);
            }).scroll();
        });
        window.setInterval(function() {
            if ($('.mw-mmv-close')[0]) {
                if (mw.config.get('wgMultimediaViewer') !== 'on') {
                    mw.config.set('wgMultimediaViewer', 'on');
                    $('.mw-mmv-image img').off().on('click', function() {
                        window.open($(this).attr('src'), '_blank').focus();
                        return false;
                    });
                    $('.mw-mmv-image').off().on('click', function(e) {
                        if ($(e.target).is('img')) return false;
                        else if ($(e.target).closest('.error-box')[0]) return;
                        else $('.mw-mmv-close').click();
                    });
                }
            } else if (mw.config.get('wgMultimediaViewer') === 'on' && !$('.mw-mmv-close')[0]) $('html, body').add(document).off('scroll.multimediaViewer');
        }, 137);
    }
    //Tabs
    function tabs() {
        var defaultStyle = {
            purple: {
                labelColor: ' ', //anti check
                labelBackgroundColor: '#9070c0',
                labelBorderColor: '#b090e0 #7050a0 #9070c0 #b090e0',
                labelPadding: '.2em .3em .2em .3em',
                textBorderColor: '#9070c0',
                textBackgroundColor: '#f0edf5',
                textPadding: '1em'
            },
            green: {
                labelColor: ' ',
                labelBackgroundColor: '#75c045',
                labelBorderColor: '#90d060 #60b030 #75c045 #90d060',
                labelPadding: '.2em .3em .2em .3em',
                textBorderColor: '#75c045 #60b030 #60b030 #75c045',
                textBackgroundColor: '#f5fffa',
                textPadding: '1em'
            },
            red: {
                labelColor: ' ',
                labelBackgroundColor: '#FF0000',
                labelBorderColor: '#FF8888 #CC0000 #FF0000 #FF8888',
                labelPadding: '.2em .3em .2em .3em',
                textBorderColor: '#FF0000 #CC0000 #CC0000 #FF0000',
                textBackgroundColor: '#fffafa',
                textPadding: '1em'
            },
            blue: {
                labelColor: ' ',
                labelBackgroundColor: '#5b8dd6',
                labelBorderColor: '#88abde #3379de #5b8dd6 #88abde',
                labelPadding: '.2em .3em .2em .3em',
                textBackgroundColor: '#f0f8ff',
                textBorderColor: '#5b8dd6 #3379de #3379de #5b8dd6',
                textPadding: '1em'
            },
            yellow: {
                labelColor: ' ',
                labelBackgroundColor: '#ffe147',
                labelBorderColor: '#ffe977 #ffd813 #ffe147 #ffe977',
                labelPadding: '.2em .3em .2em .3em',
                textBackgroundColor: '#fffce8',
                textBorderColor: '#ffe147 #ffd813 #ffd813 #ffe147',
                textPadding: '1em'
            },
            orange: {
                labelColor: ' ',
                labelBackgroundColor: '#ff9d42',
                labelBorderColor: '#ffac5d #ff820e #ff9d42 #ffac5d',
                labelPadding: '.2em .3em .2em .3em',
                textBackgroundColor: '#ffeedd',
                textBorderColor: '#ff9d42 #ff820e #ff820e #ff9d42',
                textPadding: '1em'
            },
            black: {
                labelColor: ' ',
                labelBackgroundColor: '#7f7f7f',
                labelBorderColor: '#999999 #4c4c4c #7f7f7f #999999',
                labelPadding: '.2em .3em .2em .3em',
                textBackgroundColor: '#e5e5e5',
                textBorderColor: '#7f7f7f #4c4c4c #4c4c4c #7f7f7f',
                textPadding: '1em'
            }
        };
        $('body').addClass('tab');
        // A Class
        function StyleSheet() {}
        StyleSheet.prototype.getOwnPropertyNamesLength = function getOwnPropertyNamesLength() {
            return Object.getOwnPropertyNames(this).length;
        };
        String.prototype.toLowerFirstCase = function toLowerFirstCase() {
            return this[0].toLowerCase() + this.substring(1);
        };
        $('.Tabs').each(function(i) {
            if ($(this).children('.TabLabel')[0]) return true;
            var self = $(this),
                data = $.extend({
                    labelPadding: null,
                    labelBorderColor: null,
                    labelColor: null,
                    labelBackgroundColor: $('#content').css('background-color'),
                    textPadding: null,
                    textBorderColor: null,
                    textBackgroundColor: null,
                    defaultTab: 1,
                }, self.attr('class').length > 4 ? defaultStyle[self.attr('class').slice(5)] || {} : {}, this.dataset || {}),
                tabLabel = self.append('<div class="TabLabel"></div>').children('.TabLabel'),
                tabContent = self.append('<div class="TabContent"></div>').children('.TabContent'),
                labelPadding = data.labelPadding,
                labelColor = data.labelColor,
                styleSheet = {
                    label: new StyleSheet(),
                    text: new StyleSheet()
                },
                defaultTab = parseInt(data.defaultTab);
            self.children('.Tab').each(function() {
                $(this).children('.TabLabelText').appendTo(tabLabel);
                $(this).children('.TabContentText').appendTo(self.children('.TabContent'));
                $(this).remove();
            });
            if (isNaN(defaultTab) || defaultTab <= 0 || defaultTab > tabLabel.children('.TabLabelText').length) defaultTab = 1;
            tabLabel.children('.TabLabelText').on('click', function() {
                var label = $(this);
                label.addClass('selected').siblings().removeClass('selected').css({
                    'border-color': '#aaa',
                    'background-color': 'inherit'
                });
                tabContent.children('.TabContentText').eq(tabLabel.children('.TabLabelText').index(label)).addClass('selected').siblings().removeClass('selected').removeAttr('style');
                if (styleSheet.label.getOwnPropertyNamesLength()) label.css(styleSheet.label);
                if (label.is(':visible')) tabLabel.height(label.height() + parseFloat(label.css('padding-top')) + parseFloat(label.css('padding-bottom')) + 2);
                else tabLabel.removeAttr('style');
            }).eq(defaultTab - 1).click();
            if (labelPadding) tabLabel.children('.TabLabelText').css('padding', labelPadding);
            ['labelBorderColor', 'labelBackgroundColor', 'textPadding', 'textBorderColor', 'textBackgroundColor'].forEach(function(n) {
                var target = /^label/.test(n) ? 'label' : 'text',
                    key = n.replace(target, '').toLowerFirstCase();
                styleSheet[target][key] = data[n];
            });
            if (labelColor) styleSheet.label.borderTopColor = labelColor;
            else if (styleSheet.label.borderColor) styleSheet.label.borderTopColor = 'green';
            tabLabel.find('.selected').click();
            if (styleSheet.text.getOwnPropertyNamesLength()) tabContent.css(styleSheet.text);
            if (data.autoWidth === 'yes') self.css('display', 'inline-block');
        });
    }
    //Sub-pages link
    function subpage_links() {
        var links = mw.config.get('wgPageName').split('/'),
            link = '',
            subpages = $('.subpages')[0] ? $('.subpages').remove().empty().text('<') : $('<span class="subpages"/>').text('<'),
            length = links.length - 1,
            index;
        if (mw.config.get('wgPageName').indexOf('特別:移動') !== -1) links.splice(0, 1);
        for (index = 0; index < length; index++) {
            link += '/' + links[index];
            subpages.append($('<a/>', {
                text: ' ' + links[index],
                href: link
            }));
            if (length - index > 1) subpages.append(" |");
        }
        if (subpages.find('a')[0]) $('#contentSub').prepend(subpages);
    }
    //Copyright declaration
    function copyRights() {
        var div = $('<div>', {
                css: {
                    position: 'absolute',
                    left: '-99999px',
                    'z-index': '-99999'
                },
                html: '<pre></pre><br>Read more: ' + (/%/.test(mw.util.wikiUrlencode(mw.config.get('wgPageName'))) ? mw.config.get('wgPageName') + '(https://ja.moegirl.org.cn/' + encodeURIComponent(mw.config.get('wgPageName')) + ')' : 'https://jah.moegirl.org.cn/' + mw.config.get('wgPageName')) + '<br>This text block is from MoegirlPedia(https://ja.moegirl.org.cn/), The textual content follows the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 protocol.'
            }).appendTo('body'),
            valueNode = div.find('pre');
        $(document.body).on('copy', function() {
            var selection = window.getSelection(),
                value = selection.toString(),
                range = selection.getRangeAt(0);
            if (!value.length || value.length < 128
                ||
                $(selection.anchorNode).add(selection.basenode).add(selection.focusNode).closest('.Wikiplus-InterBox')[0]) 
                return;
            valueNode.text(value);
            selection.selectAllChildren(div[0]);
            window.setTimeout(function() {
                selection.removeAllRanges();
                selection.addRange(range);
                valueNode.empty();
            }, 0);
        });
    }
    //Edit request
    function editRequest() {
        var nsIds = mw.config.get('wgNamespaceIds'),
            nsId = mw.config.get('wgNamespaceNumber'),
            ns = [];
        for (var i in nsIds) {
            if (nsIds[i] === nsId) ns.push(i);
        }
        if (!ns[0]) return;
        var user = mw.config.get('wgUserName'),
            usergroup = mw.config.get('wgUserGroups'),
            page = mw.config.get('wgPageName');
        for (var j = 0, l = ns.length; j < l; j++) {
            if (/^[A-Za-z0-9]+[ _]talk$/i.test(ns[j])) return;
            if (/^[a-zA-Z0-9]+$/.test(ns[j])) var talkns = ns[j] + '_talk:';
            page = page.replace(new RegExp('^' + ns[j] + ':', 'i'), '');
        }
        var talkpage = talkns + page;
        if (!$('.permissions-errors')) return;
        var container = $('<div/>', {
            class: 'editRequest'
        });
        container.append('Sorry, you have no permission to edit this page. But you can click the button at right side, and send an edit request in the talk page, to ask somebody who have the permission to edit the page:');
        $('<span/>').addClass("newComment").text('Send an edit request').on('click', function() {
            window.open('https://zh.moegirl.org.cn/index.php?action=edit&preload=Template:编辑请求/comment&preloadtitle=编辑请求 - ' + mw.config.get("wgUserName") + '&section=new&title=' + talkpage, '_blank');
        }).appendTo(container);
        $('#mw-content-text').children('.wikiEditor-ui:first, textarea[readonly]:first').before('<hr>').before(container);
    }
    // Run-time
    $(function() {
        //workaround for redirect page which did not redirect correctly
        mw.loader.using('mw.Uri', function() {
            if (!new mw.Uri().redirect && $('#mw-content-text > .redirectMsg')[0] && !$('#mw-content-text > :not(.redirectMsg)')[0]) {
                var redirectLink = $('#mw-content-text > .redirectMsg > .redirectText > :first > a');
                if (redirectLink[0]) location.replace(redirectLink[0].href);
            }
        });
        // Avatar
        if (["ViewAvatar", "UploadAvatar"].includes(mw.config.get("wgCanonicalSpecialPageName"))) {
            var url = new mw.Uri();
            url.host = url.host.replace(/^[^\.]+/g, "commons");
            url.path = mw.config.get("wgScript");
            url.query.title = mw.config.get("wgCanonicalNamespace") + ":" + mw.config.get("wgCanonicalSpecialPageName");
            location.replace(url);
        }
        // workaround for template:heimu in safari
        if (!/chrome/i.test(navigator.userAgent) && /safari/i.test(navigator.userAgent)) $('.heimu').on('click', function() {
            $(this).toggleClass('off');
        });
        var copyRightsNameSpaces = [ // namespaces to insert Copyright declaration
            0, // (Main)
            4, // moegirlpedia
            8, // MediaWiki
            10, // Template
            12, // Help
            614 // R18
        ];
        //Extension:MultimediaViewer workaround for translucent
        if ($('img[srcset][data-file-width][data-file-height]')[0]) multimediaViewer();
        // Add "mainpage" class to the body element
        if (mw.config.get('wgMainPageTitle') === mw.config.get('wgPageName') && mw.config.get('wgAction') === 'view') $('body').addClass('mainpage');
        // Load script for edit-page
        if (mw.config.get('wgAction') === "edit" || mw.config.get('wgAction') === "submit" || mw.config.get('wgCanonicalSpecialPageName') === 'Search')
            mw.loader.load('/index.php?title=MediaWiki:Common.js/edit.js&action=raw&ctype=text/javascript');
        // Tabs apply
        if ($('.Tabs')[0]) tabs();
        //Sub-pages link
        if (mw.config.get('wgPageName').indexOf('/') !== -1 && (mw.config.get('wgNamespaceIds').special !== mw.config.get('wgNamespaceNumber') || mw.config.get('wgPageName').indexOf('Special:移动页面') !== -1) && !window._unsubpage)
            subpage_links();
        // Copyright declaration
        if (window.getSelection && mw.config.get('wgUserGroups').indexOf('sysop') + mw.config.get('wgUserGroups').indexOf('patroller') === -2 && ['edit', 'submit'].indexOf(mw.config.get('wgAction')) === -1 && copyRightsNameSpaces.indexOf(mw.config.get('wgNamespaceNumber')) !== -1)
            copyRights();
        // edit request
        //if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1 && !mw.config.get('wgIsProbablyEditable') && mw.config.get('wgUserName')) editRequest();
        // Fix the reference error for $.ucFirst
        jQuery.extend({
            ucFirst: function(s) {
                s += '';
                return s.charAt(0).toUpperCase() + s.substring(1);
            }
        });
        // list in reference
        $('.reference-text > ul,.reference-text > ol').each(function() {
            if (this.parentElement.childNodes[0] === this) $(this).addClass('listInRef');
        });
        // fix hash jump
        if ($('.mw-collapsible')[0]) mw.loader.using('jquery.makeCollapsible', function() {
            $('.mw-collapsible').makeCollapsible();
            var hash = location.hash;
            location.hash = '';
            location.hash = hash;
        });
    });
})(jQuery, mediaWiki);