本Wiki是开放编辑的非官方百科,欢迎加入编辑。

全站通知:

MediaWiki:Common.js

来自学生时代WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

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

  • 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
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
/* 请不要在这里使用ES6,会坏掉的 */

/* 将相关代码用改为DOM加载完成后运行,解决部分情况
模板:ResourceLoader加载不完全的问题 -- 芙兰朵露琪露诺 2021/8/11*/

/* 参考来源:
https://wiki.biligame.com/umamusume/MediaWiki:Common.js 
https://wiki.biligame.com/wukong/MediaWiki:Common.js
*/

/* 特性支持:参见[[模板:ResourceLoader]]*/
function templateResourceLoader() {
    $('.resourceLoader').each(function () {
        var $x = $(this);
        var text = $.trim($x.text());

        if (!text) return;

        //加载模块
        if ($x.data('isModule') == true)
            return mw.loader.load(text);

        //自动补充MediaWiki命名空间
        var ns = text.match('^.*?:');
        if (!ns) text = 'MediaWiki:' + text;

        //加载CSS样式表
        var mime = ($x.data('mime') || "text/javascript").toLowerCase();
        if (mime == "text/css") {
            if (text.slice(-4).toLowerCase() !== '.css') text = text + '.css';
            //if ($x.data('debug') !== true) text = text + '&debug=false';
            return mw.loader.load("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=" + text + "&action=raw&ctype=text/css", "text/css");
        }

        //加载JS脚本
        if (ns && ns[0].toLowerCase() !== 'mediawiki:') {
            return console.log('ResourceLoader: 不允许加载MediaWiki以外的js脚本');
        }
        if (text.slice(-3).toLowerCase() !== '.js') text = text + '.js';
        
        //加载ES6模块
        if ($x.data('isModuleEs6') == true)
            return loadModuleES6("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=" + text + "&action=raw&ctype=text/javascript", function () {
                console.log("ResourceLoader: 加载ES6模块 => " + text);
            });
        //加载React模块
        if ($x.data('isModuleReact')) {
            if ($x.data('isModuleReact')[0]) {
                $.getScript("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=MediaWiki:React.development.min.js&action=raw&ctype=text/javascript", function () {
                    $.getScript("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=MediaWiki:React-dom.development.min.js&action=raw&ctype=text/javascript", function () {
                        console.log("ReactENV => React/ReactDOM Loaded! React is Available For This Page!");
                        console.log("%c React开发提示 %c 请务必在JS编辑界面右下角使用React同步器同步编译你的JSX代码,否则它将不起任何作用", "color: #fff; padding: 5px 0; background: blue;", "padding: 5px 5px 5px 0; background: #e5e5ff; color:blue");
                        $('.resourceLoader').each(function () {
                            var $xr = $(this);
                            var textr = $.trim($xr.text());
                            if (!textr) return;
                            //自动为React模块补充MediaWiki命名空间与-babel后缀
                            var nsr = textr.match('^.*?:');
                            if (!nsr) textr = 'MediaWiki:' + textr.slice(0, -3);
                            textr += '-babel.js';
                            if ($xr.data('isModuleReact') && $xr.data('isModuleReact')[1]) {
                                loadModuleES6("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=" + textr + "&action=raw&ctype=text/javascript", function () {
                                    console.log("ResourceLoader: 加载React模块 => " + textr);
                                });
                            }
                        });
                    });
                });
                return;
            }
        }
        //加载普通脚本
        console.log('ResourceLoader: 加载JS => ' + text);
        //if ($x.data('debug') !== true) text = text + '&debug=false';
        return mw.loader.load("//wiki.biligame.com" + mw.config.values.wgScriptPath + "/index.php?title=" + text + "&action=raw&ctype=text/javascript", "text/javascript");
    });
}
if (document.readyState == 'loading') {
    document.addEventListener('DOMContentLoaded', templateResourceLoader);
} else {
    templateResourceLoader();
}
/* end 特性支持: ResourceLoader */

/* 特性支持:加载ES6模块 */
function loadModuleES6(src, callback) {
    var script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0];
    script.type = 'module';
    script.charset = 'UTF-8';
    script.src = src;
    if (script.addEventListener) {
        script.addEventListener('load', function () {
            callback();
        }, false);
    } else if (script.attachEvent) {
        script.attachEvent('onreadystatechange', function () {
            var target = window.event.srcElement;
            if (target.readyState == 'loaded') {
                callback();
            }
        });
    }
    head.appendChild(script);
}
/* end 特性支持:加载ES6模块 */

// 特性支持: 快捷编辑理由。支持编辑摘要时,点击快捷选项填充编辑理由
// 参考Wikipedia:MediaWiki:Common.js/edit.js 转换为无需JQuery的写法
function fast_edit_summary_btn() {
  if (mw.config.get('wgAction') == "edit" || mw.config.get('wgAction') == "submit" || mw.config.get('wgCanonicalSpecialPageName') == 'Search') { // 编辑页面
    var wpSectionInput = document.querySelector('#editform input[name="wpSection"]');
    
    if (wpSectionInput && wpSectionInput.value === 'new') {
        if (document.getElementById('no-new-title')) {
            document.getElementById('wpSummary').setAttribute('disabled', true);
        }
        return;
    }

    var presetContainer = document.querySelector('#wpSummaryLabel .mw-summary-preset');
    if (presetContainer) {
        presetContainer.addEventListener('click', function(e) {
            if (e.target && e.target.matches('.mw-summary-preset-item a')) {
                e.preventDefault();
                
                var summaryInput = document.getElementById('wpSummary');
                var summary = summaryInput.value;
                var item = e.target.closest('.mw-summary-preset-item');
                
                summary = summary.replace(/\s+$/g, '');
                if (summary !== '') {
                    summary += ' ';
                }
                
                summary += item.getAttribute('title') || e.target.textContent;
                
                summaryInput.value = summary;
                summaryInput.focus();
            }
        });
    }
  }/* end if 'wgAction'*/
}/* end function */
if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", fast_edit_summary_btn);
} else {
  fast_edit_summary_btn();
}
/* end 特性支持: 快捷编辑理由 */

/* -- 验证上传文件 --*/
// $(function() {
// 	if (mw.config.get('wgPageName') === '特殊:上传文件') {
// 		mw.loader.load("https://wiki.biligame.com" + mw.util.wikiScript("") + "title=MediaWiki:UploadValidation.js&action=raw&ctype=text/javascript");
// 	}
// });


/* -- 插件加载-- */
/* -------------------------------------------------- */
//编辑器插件SmartClick引入
$(function () {
    mw.loader.load("//wiki.biligame.com" + mw.util.wikiScript("index") + "?title=MediaWiki:SmartClick.js&action=raw&ctype=text/javascript", "text/javascript");
    console.log("SmartClick.js initialized!");
});

//flowthread评论区表情插件
// 来自seerplan,源自umamusume
$(function(){
	loadModuleES6("//wiki.biligame.com" + mw.util.wikiScript("index") + "?title=MediaWiki:Emotion.js&action=raw&ctype=text/javascript", function() {
		console.log("Flowthread Add-on Emotion loaded!");
	});
});

//动画库animate.css
$(function () {
    mw.loader.load("//wiki.biligame.com" + mw.util.wikiScript("index") + "?title=MediaWiki:Animate.min.css&action=raw&ctype=text/css", "text/css");
    console.log("Animate.min.css initialized!");
});
/* -------------------------------------------------- */
/* -- 插件加载(完)-- */