MediaWiki:Gadget-wdbox.js

Z Wikipedii, wolnej encyklopedii

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
mw.messages.set({
	'wdbox-name': 'WDbox',
	'wdbox-descroption-page': 'Wikipedia:WDbox',
	'wdbox-always': 'Zawsze wstawiaj automatyczny infobox',
	'wdbox-replace': 'Ukrywaj oryginalny infobox gdy istnieje automatyczny',
});

gConfig.register('wdbox', {name: mw.msg('wdbox-name'), descriptionPage: mw.msg('wdbox-descroption-page')}, [
	{
		name: 'always',
		desc: mw.msg('wdbox-always'),
		type: 'boolean',
		deflt: true,
		legacy: [window, 'wp_wdbox_always']
	},
	{
		name: 'replace',
		desc: mw.msg('wdbox-replace'),
		type: 'boolean',
		deflt: false,
		legacy: [window, 'wp_wdbox_replace']
	}
]);

(function(ms, $){
	if (location.search.match(/[?&]printable=yes/)){return;}
	if (!mw.config.get("wgArticleId")){return;}
	if (mw.config.get('wgNamespaceNumber') !== 0){return;}
	if (mw.config.get('wgAction') !== 'view'){return;}
	if (mw.config.get('wgDiffOldId')){return;}
	
	var hasInfoboxes = $('table.infobox').not('.noprint, .plainlinks').length;
	var hasKA = $('#normdaten').not('.noprint, .plainlinks').length;
	var always = gConfig.get('wdbox', 'always');
	var replace = gConfig.get('wdbox', 'replace');	

	console.log('always: '+always);
	console.log('replace: '+replace);
	console.log('hasInfoboxes: '+hasInfoboxes);
	var api = new mw.Api();
	if (always || !hasInfoboxes){
		api.post({
			action: 'parse',
			format: 'json',
			prop: 'text',
			title: mw.config.get("wgPageName"),
			text: '{{#invoke:WDbox|Infobox}}',
			disablelimitreport: true,
			contentmodel: 'wikitext'
		}).done(function(data){
			var hasWDbox = $('table.infobox.wdbox', $.parseHTML(data.parse.text['*'])).length;
			console.log('hasWDbox: '+hasWDbox);
			if ((hasInfoboxes == 1) && hasWDbox && replace) {
				$('table.infobox').not('.noprint, .plainlinks').css("display", "none");
			}
			$("#mw-content-text").prepend(data.parse.text['*']);
		});
	}
	if (always || !hasKA){
		api.post({
			action: 'parse',
			format: 'json',
			prop: 'text',
			title: mw.config.get("wgPageName"),
			text: '{{#invoke:WDbox|Infobar}}',
			disablelimitreport: true,
			contentmodel: 'wikitext'
		}).done(function(data){
			var hasWDbar = $('div.catlinks.wdbar', $.parseHTML(data.parse.text['*'])).length;
			console.log('hasWDbar: '+hasWDbar);
			if ((hasKA == 1) && hasWDbar && replace) {
				$('#normdaten').not('.noprint, .plainlinks').css("display", "none");
			}
			$("#mw-content-text").append(data.parse.text['*']);
		});
	}
})(mediaWiki, jQuery);