Difference between revisions of "MediaWiki:Common.js"

From WikiConference North America
Jump to navigation Jump to search
(...fix)
(apply dontFixSchedule to the reloading)
 
(6 intermediate revisions by the same user not shown)
Line 17: Line 17:
   
 
if (mw.config.get('wgUserName') === null) {
 
if (mw.config.get('wgUserName') === null) {
addOnloadHook(customizeSidebarForAnon);
+
$(customizeSidebarForAnon);
 
}
 
}
   
Line 54: Line 54:
 
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
 
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
 
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
 
if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
+
mw.loader.load( '/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
 
} else {
 
} else {
 
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
 
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
Line 63: Line 63:
 
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
 
// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
 
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
 
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
+
mw.loader.load( '/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
 
} else {
 
} else {
 
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
 
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
Line 69: Line 69:
 
}
 
}
 
});
 
});
  +
  +
if ( ( mw.config.get( "wgArticleId" ) === /* 2021/Schedule */ 4267 ) && ( mw.config.get( 'wgAction' ) === 'view' ) ) {
  +
mw.loader.load( '/index.php?title=MediaWiki%3AGadget-fix-schedule-times.js&action=raw&ctype=text/javascript' );
  +
mw.loader.using( ['mediawiki.api'], function () {
  +
if(window.location.search.indexOf("dontFixSchedule")>=0)return;
  +
var api = new mw.Api();
  +
window.setInterval(function(){
  +
api.get({
  +
action:'query',
  +
pageids:4267,
  +
prop:'revisions',
  +
rvprop:'ids',
  +
uselang:'content',
  +
format:'json',
  +
formatversion:'2'
  +
}).then(function(res){
  +
var revid = res.query.pages[0].revisions[0].revid;
  +
if(revid !== mw.config.get('wgRevisionId')){
  +
mw.notify('Schedule changed. Reloading.');
  +
window.location.reload();
  +
}
  +
});
  +
},1000 * 60 * 2);
  +
});
  +
}

Latest revision as of 02:49, 7 October 2021

/* Interface modifications for anonymous users */

function removeSidebarSection(section) {
  try {
    var target = "p-" + section;
    var sidebar = document.getElementById("mw-panel");
    var element = document.getElementById(target);
    sidebar.removeChild(element);
  } catch (e) {
    return;
   }
}

function customizeSidebarForAnon() {
  removeSidebarSection("tb");
}

if (mw.config.get('wgUserName') === null) {
  $(customizeSidebarForAnon);
}

/* Add a few links to the toolbox */
$(function() {
  mw.util.addPortletLink( 'p-tb', '/wiki/Special:RecentChanges', 'Recent changes', 't-recentchanges', 'Recent changes ', 'r', '#t-recentchangeslinked' );
  $( '#megaheader img' ).css( 'opacity', 1 ); /* Thank you Jack Phoenix! */
});

// Update from https://tools.wmflabs.org/intuition/wpAvailableLanguages.js.php - Last update: Fri, 06 Mar 2015 22:58:41 +0000
window.wpAvailableLanguages={"en":"English","fr":"Français"};

/**
 * AnonymousI18N
 *
 * Internationalisation for anonymous users.
 *
 * @author [[User:Krinkle]]
 * @stats [[File:Krinkle_AnonymousI18N.js]]
 */
mw.loader.using( 'mediawiki.user', function () {
 if ( mw.user.isAnon() ) {
  mw.loader.load( '//commons.wikimedia.org/w/index.php?title=MediaWiki:AnonymousI18N.js&action=raw&ctype=text/javascript' );
 }
});

/**
 * @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
 * @revision 2020-04-04
 */
mw.loader.using( ['mediawiki.util'], function () {
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' );

	if ( extraCSS ) {
		// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
			mw.loader.load( '/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
			mw.loader.load( '/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}
});

if ( ( mw.config.get( "wgArticleId" ) === /* 2021/Schedule */ 4267 ) && ( mw.config.get( 'wgAction' ) === 'view' ) ) {
mw.loader.load( '/index.php?title=MediaWiki%3AGadget-fix-schedule-times.js&action=raw&ctype=text/javascript' );
mw.loader.using( ['mediawiki.api'], function () {
if(window.location.search.indexOf("dontFixSchedule")>=0)return;
    var api = new mw.Api();
    window.setInterval(function(){
        api.get({
            action:'query',
            pageids:4267,
            prop:'revisions',
            rvprop:'ids',
            uselang:'content',
            format:'json',
            formatversion:'2'
        }).then(function(res){
            var revid = res.query.pages[0].revisions[0].revid;
            if(revid !== mw.config.get('wgRevisionId')){
                mw.notify('Schedule changed. Reloading.');
                window.location.reload();
            }
        });
    },1000 * 60 * 2);
});
}