MediaWiki:Gadget-site-shared.js

Bhsd讨论 | 贡献2021年1月24日 (日) 11:53的版本

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

  • 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),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/**
 * @Description: 这里是桌面版和手机版通用的全局JS
 * @Author: 如无特殊说明,均为[[User:Bhsd]]
 */
"use strict";
/*global OO, wgULS*/
const pagename = mw.config.get('wgPageName'),
    specialPage = mw.config.get( 'wgCanonicalSpecialPageName' ),
    action = mw.config.get('wgAction'),
    editable = mw.config.get( 'wgIsProbablyEditable' ),
    $body = $('body');

/**
 * @Function: 去掉地址栏最后的"/"
 * @Dependencies: mediawiki.util, mediawiki.Uri
 * @Author: [[wikipedia:User:Majavah]]
 * @EditedBy: [[User:Bhsd]]
 */
if (pagename.endsWith( '/' ) && specialPage != 'Prefixindex') {
    const uri = new mw.Uri();
    delete uri.query.title;
    const query = uri.getQueryString();
    // 最稳健的短地址方案
    location.replace('/zh/' + mw.util.wikiUrlencode( pagename.slice(0, -1) ) + query ? '?' + query : '');
}

/**
 * @Function: 更复杂的快速插入
 * @Source: [[mw:Extension:CharInsert]]
 * @Dependencies: jquery.textSelection(已由CharInsert扩展加载)
 */
if (['edit', 'submit'].includes( action ) && editable) {
    $body.on('click', 'span.mw-charinsert-item', function() {
        const $this = $(this);
        $('#wpTextbox1').textSelection( 'encapsulateSelection', {
            pre: $this.data( 'mw-charinsert-start' ) || $this.data( 'start' ), // undefined也没关系
            post: $this.data( 'mw-charinsert-end' ) || $this.data( 'end' )
        } );
    });
}

mw.hook( 'wikipage.content' ).add(function($content) {
    /**
     * @Function: 点击其他用户主页面的红链不会进入创建页面
     * @Dependencies: mediawiki.Uri, mediawiki.Title
     */
    $content.find( '.new' ).attr('href', function(i, val) {
        const uri = new mw.Uri( val ),
            title = new mw.Title( uri.query.title ), // 借助mediawiki.Title规范用户名格式
            username = title.getMainText();
        // 不处理讨论页或是子页面
        if (title.namespace != 2 || username.includes( '/' ) || username == mw.config.get('wgUserName')) { return; }
        return title.getUrl();
    });
    
    if (['edit', 'submit'].includes(action) && !editable && $('.editRequest').length == 1) {
        var ele = $('.editRequest > a'),
            href = new mw.Uri(ele.attr('href')),
            date = new Date();
        href.query.preloadtitle = '编辑请求' + ' - ' + mw.config.get('wgUserName') + ' (' + date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate() + ')';
        ele.attr('href', href.toString());
    }
    
    // 引自[[moegirl:mediawiki:common.js]]
    if (top !== window || location.host !== 'llwiki.org') {
        var messageDialog = new OO.ui.MessageDialog(),
            windowManager = new OO.ui.WindowManager();
        $("body").append(windowManager.$element);
        windowManager.addWindows([messageDialog]);
        messageDialog.title.$label.html('<p>LLWiki提醒您</p><p>您正在非LLWiki域名' + wgULS('访问', '訪問') + '</p>');
        messageDialog.message.$label.html('<p>' + wgULS('请注意不要在此域名下输入您的用户名或密码', '請注意不要在此域名下輸入您的用戶名或密碼') + ',以策安全!</p><p>LLWiki' + wgULS('域名为 ', '域名為') + ' llwiki.org</p>');
        windowManager.openWindow(messageDialog, {
            actions: [{
                action: "continue",
                label: "我知道了",
                flags: ["primary", "destructive"]
            }, {
                action: "jump",
                label: "前往LLWiki",
                flags: ["primary", "progressive"],
                href: top === window ? "https://llwiki.org" : location.href,
                target: "_blank"
            }]
        });
        var timer = setInterval(function() {
            if ($('.oo-ui-windowManager .oo-ui-buttonElement-button').length) {
                clearInterval(timer);
                $('.oo-ui-windowManager .oo-ui-buttonElement-button').each(function() {
                    if (this.href) { $(this).off('click'); }
                });
            }
        }, 100);
    }
});

mw.hook( 'wikipage.categories' ).add(function($content) {
    $content.find( 'a:contains("Μ\'s")' ).text(function(i, text) { return text.replace("Μ's", "μ's"); });
    $content.find( 'a:contains("Lily white")' ).text(function(i, text) { return text.replace('Lily', 'lily'); });
});

$(function() {
    /* 沙盒顶部 */
    var uri = new mw.Uri();
    if ( ['LLWiki:沙盒', 'Template:Sandbox'].includes(pagename) && action == 'view' &&
        !(uri.query.diff || uri.query.oldid) ) {
        $('#mw-content-text').prepend('<table class="infobox" style="background-color:#f3f5f7;width:70%;border-left-color:#c5c5c5" cellpadding="2" align="center">' +
            '<tbody><tr>' +
                '<td width="15%">' +
                    '<img src="/mediawiki/img_auth.php/0/04/Bandeau-avertisement-3.png" width="50" height="50">' +
                '</td>' +
                '<td><span style="color:">欢迎来到沙盒!您可以在此进行编辑测试。</span></td>' +
            '</tr></tbody>' +
        '</table>');
    }
});