MediaWiki:Common.js

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

  • 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将为所有用户在每次页面加载时加载。 */

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/umamusume/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/umamusume/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/umamusume/index.php?title=MediaWiki:React.development.min.js&action=raw&ctype=text/javascript", function() {
					$.getScript("//wiki.biligame.com/umamusume/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/umamusume/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/umamusume/index.php?title="+text+"&action=raw&ctype=text/javascript", "text/javascript");
	});
}

if (document.readyState == 'loading') {
	document.addEventListener('DOMContentLoaded', templateResourceLoader);
} else {
	templateResourceLoader();
}

//加载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);
}