MediaWiki:Live EriCSSon.js

Catatan: Setelah menyimpan, Anda harus memintas tembolok (cache) peramban Anda untuk melihat perubahannya. Google Chrome, Firefox, Microsoft Edge dan Safari: Tahan tombol Shift dan klik Muat ulang (Reload) di tombol bilah alat. Untuk detail dan instruksi tentang peramban lain, lihat halaman menghapus singgahan (Inggris).

mw.loader.load( ['jquery.ui'] );


var live_ericsson = {

	use_css_cookie : 'Live_EriCSSon_useCSS' ,

	init : function () {
		
//		if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
//		if ( mw.config.get('wgAction') != 'view' ) return ;
		var self = this ;
		var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'Live EriCSSon','t-live-ericsson');
		$(portletLink).click ( function () {
			self.options() ;
			return false ;
		} ) ;
		self.run() ;
	} ,
	
	run : function () {
		var self = this ;
		var use_css = self.getURLParameter ( 'useCSS' ) ;
		if ( !self.isSet ( use_css ) ) {
			use_css = $.cookie( self.use_css_cookie ) ;
			if ( !self.isSet ( use_css ) ) return ;
		}
		self.use_css = use_css ;
		var api = new mw.Api();
		api.get( {
			action: 'query',
			prop: 'revisions' ,
			titles: use_css ,
			rvprop: 'content'
		}, {
			ok: function () {
				var a = arguments[0] ;
				if ( undefined === a || undefined === a.query ) return ;
				var pages = a.query.pages ;
				if ( undefined === pages ) return ;
				$.each ( pages , function ( page , data ) {
					var css = data.revisions[0]['*'] ;
					mw.util.addCSS ( css ) ;
				} ) ;
			}
		} );		
		
		if ( use_css != ($.cookie( self.use_css_cookie)||'') ) {
			$('a').each ( function ( k , v ) {
				var href = $(v).attr('href') || '' ;
				if ( href.match ( /^\/wiki\// ) ) $(v).attr('href',href+'?useCSS='+use_css) ;
				else if ( href.match ( /^\/w\// ) ) $(v).attr('href',href+'&useCSS='+use_css) ;
			} ) ;
		} ;
	} ,

	options : function () {
		var self = this ;
		$('#live_ericsson_dialog').remove() ;
		var h = "<div id='live_ericsson_dialog' title='Live EriCSSon - Settings'>" ;

		h += "<div style='border-bottom:1px solid #DDDDDD;padding-bottom:5px;margin-bottom:5px'>" ;
		if ( self.isSet ( self.use_css ) ) {
			h += "Current CSS :<br/>" + self.use_css ;
			h += "<br/><a href='#' onclick='live_ericsson.makeCurrentDefault();return false'>Make default</a>" ;
			h += "<span id='live_ericsson_clear_link' style='display:none'> | <a href='#' onclick='live_ericsson.clearDefault();return false'>Clear current default</a></span>" ;
		} else {
			h += "<i>No CSS loaded</i>" ;
		}
		h += "</div>" ;
		
		h += "<div style='border-bottom:1px solid #DDDDDD;padding-bottom:5px;margin-bottom:5px'>" ;
		h += "Load CSS page:<br/>" ;
		h += "<input type='text' size='30' id='live_ericsson_text_input' value='User:Magnus_Manske/explosion.css' />" ;
		h += "<button onclick='live_ericsson.reloadPageWithNewCSS()'>Reload with this CSS</button>" ;
		h += "</div>" ;
		
		h += "<div id='live_ericsson_message'></div>" ;
		h += "</div>" ;
		$('body').append ( h ) ;
		$('#live_ericsson_dialog').dialog ( {
			open: function(event, ui) { $('#live_ericsson_dialog a').blur().css({'font-weight':'bold'}) ; }
		} ) ;

		var def = $.cookie( self.use_css_cookie ) ;
		if ( self.isSet ( def ) ) $('#live_ericsson_clear_link').show() ;

	} ,
	
	reloadPageWithNewCSS : function () {
		var ncss = $('#live_ericsson_text_input').val() ;
		var url = mw.util.getUrl( mw.config.get( 'wgPageName' ) ) ;
		url += "?useCSS=" + ncss ;
		window.location.href = url ;
	} ,
	
	isSet : function ( x ) {
		if ( undefined === x ) return false ;
		if ( null === x ) return false ;
		if ( 'null' === x ) return false ;
		return true ;
	} ,
	
	clearDefault : function () {
		var self = this ;
		$.cookie( self.use_css_cookie , null , {
		 expires: 90, // days
		 path: '/' // domain-wide, entire wiki
		} );
		$('#live_ericsson_message').html ( "Default CSS has been removed for this wiki, for this computer." ) ;
		$('#live_ericsson_clear_link').hide() ;
//		console.log ( $.cookie( self.use_css_cookie ) ) ;
	} ,
	
	makeCurrentDefault : function () {
		var self = this ;
		$.cookie( self.use_css_cookie , self.use_css , {
		 expires: 90, // days
		 path: '/' // domain-wide, entire wiki
		} );
		$('#live_ericsson_message').html ( self.use_css + " is now default CSS for this wiki, on this computer." ) ;
		$('#live_ericsson_clear_link').show() ;
//		console.log ( $.cookie( self.use_css_cookie ) ) ;
	} ,
	
	getURLParameter : function(name) {
		return decodeURIComponent(
			(RegExp('\\b' + name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
		);
	} ,

	the_end : ''
} ;

$( function () { live_ericsson.init () } ) ;