跳转到内容

User:A2569875/Gadgets/Graph/SandboxCore.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// User:A2569875/Gadgets/Graph/SandboxCore.js
( function () {
	'use strict';

	window.mw = window.mw || {};
	mw.libs = mw.libs || {};

	// 1. 立即標記為沙盒模式並清空正式版可能已掛載的元件,確保完全由 Module:Graph/*.js 接管
	var GW = mw.libs.GraphWidget = {
		_isSandbox: true,
		Utils: null,
		MathEngine: null,
		Chart2D: null,
		ChartPolar: null,
		Chart3D: null,
		Renderers: {},
		whitelistData: null
	};

	function bootSandboxCore() {
		var $ = window.jQuery;
		var scriptPath = ( window.mw && mw.config && mw.config.get( 'wgScript' ) ) || '/w/index.php';

		// 2. 若頁面上已有被正式版小工具先行渲染過的容器,清除舊圖並解除鎖定,交由沙盒重繪
		$( '.gr-async-chart[data-chart-spec]' ).each( function () {
			this.removeAttribute( 'data-gr-loaded' );
			$( this ).children( '.gr-async-stage, .gr-async-loading' ).remove();
		} );

		// 3. 不帶 ctype=text/javascript 請求純文字,繞過 403 並透過 $("<script>").html() 注入執行
		$.ajax( {
			url: scriptPath,
			data: {
				title: 'Module:Graph/SandboxCore.js',
				action: 'raw'
			},
			dataType: 'text',
			cache: false
		} ).done( function ( rawCode ) {
			$( '<script>' ).html( rawCode ).appendTo( document.head );
			if ( window.console && console.info ) {
				console.info( '[SandboxCore.js] 已成功透過 $("<script>").html() 載入測試機 Module:Graph/SandboxCore.js' );
			}
			if ( mw.libs.GraphWidget && typeof mw.libs.GraphWidget.scanAll === 'function' ) {
				mw.libs.GraphWidget.scanAll();
			}
		} ).fail( function ( jqXHR, textStatus, errorThrown ) {
			if ( window.console && console.error ) {
				console.error( '[SandboxCore.js] 載入 Module:Graph/SandboxCore.js 失敗:', errorThrown || textStatus );
			}
		} );
	}

	if ( window.mw && mw.loader && typeof mw.loader.using === 'function' ) {
		mw.loader.using( [ 'jquery', 'mediawiki.base', 'mediawiki.util', 'mediawiki.ForeignApi' ] ).then( bootSandboxCore );
	} else {
		( window.RLQ = window.RLQ || [] ).push( function () {
			if ( window.mw && mw.loader && typeof mw.loader.using === 'function' ) {
				mw.loader.using( [ 'jquery', 'mediawiki.base', 'mediawiki.util', 'mediawiki.ForeignApi' ] ).then( bootSandboxCore );
			}
		} );
	}
}() );