User:Perhelion/Code

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

User-JS snippets

[edit]

Paste the following codes into your common.js (or other MediaWiki skin related) file and customize it. (My js files are here on Commons and here global on Meta.)


Pre-filled Special:Upload description

[edit]

If you tired of always entering the same stuff in Special:Upload (adapted from User:Dschwen, I made it a bit more dynamically / user friendly)

/* jshint multistr:true */
var ul = mw.config.get('wgUserLanguage'),
	pn = mw.config.get('wgPageName'),
	un = mw.config.get('wgUserName');
$(function () {
	// Insert a personalized text-template into the Special:Upload edit box.
	// I upload mostly my own work; prefill input form; adapted from [[User:Dschwen]]<nowiki>
	if (pn === 'Special:Upload' && /&uselang=([^&]*)ownwork/.test(location.search)) {
		var re = RegExp.$1;
		setTimeout(function () {
			$('#wpUploadDescription').val(
				'=={{int:filedesc}}==\n\
				{{Information\n\
				|description=\n{{en|1=}}\n' +
				((re) ? '{{' + re + '|1=}}\n' : '') +
                '|date=' + '{{Originaluploaddate|}} {{vectorization}} {{Uploaddate|{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY2}}}}\n\
				|source={{own based|…}}\n\
				|author={{Author|original|{{unknown}}}}; {{Author|vectorization|[[User: ' + un + ']]}}\n\
				|permission=\n\
				|other versions=\n\
				<gallery widths="100" heights="100" showfilename>\n\n</gallery>\n\
				|other fields={{Igen|h|+|s=|u=' + un + '}}\n\
				}}\n\n\
				=={{int:license-header}}==\n\
				{{self|GFDL|cc-by-sa-4.0|author=I, [[User:' + un + '|]]}}');
		}, 100);
	} else { // If not on Special:Upload change the Upload-link.
		$('#n-uploadbtn').find('a').attr('href', '//commons.wikimedia.org/w/index.php?title=Special:Upload&uselang=' +
			((ul === 'en') ? '' : ul) + 'ownwork&uploadformstyle=basic');
	} // </nowiki>
});

Note: the license IDs were changed recently. Please update the line with licensemenu.value = accordingly.

[edit]

For all file versions (near the date link by the thumbs):

// W3C-Validator check-link for every SVG (by [[User: Perhelion]])
var ns = mw.config.get('wgNamespaceNumber'),
	ti = mw.config.get('wgTitle');
if (ns === 6) { // Files only
	if (/SVG/i.test(ti.slice(-3))) { // SVG
		// SVG edit
		importScript('User:Rillke/SVGedit.js');
		// W3C-Validator check-link for every SVG (by [[User: Perhelion]])
		$('#mw-imagepage-section-filehistory tbody>tr+tr>td:nth-child(2)>a').after(function () {
			return ['<br>', $('<a>', {
					'title' : 'W3C-Validator',
					'href' : 'http://validator.w3.org/check?uri=' + this.href + '#result',
					'target' : '_blank'
				}).text('→ Valid SVG?')];
		});
	}
}

Or simply only the last file version:

// W3C-Validator check-link for every SVG (by User: Perhelion)
var ns = mw.config.get('wgNamespaceNumber'),
	ti = mw.config.get('wgTitle');
if ( ns === 6 && /SVG/i.test(ti.slice(-3)) ) {  // SVG files only
	$('#mw-imagepage-section-filehistory tbody>tr+tr>td:nth-child(2)>a:first').after(function () {
		return ['<br>', $('<a>', {
				'title' : 'W3C-Validator',
				'href' : 'http://validator.w3.org/check?uri=' + this.href + '#result',
				'target' : '_blank'
			}).text('→ Valid SVG?')];
	});
}

Note: var ns, ti is a custom name and could be redundant. There could be also an additional URL parameter &ss=1 (direct before #result) but this has some disadvantages.[1]


SVG-template-buttons

[edit]

Using the module "Wikieditor" from Schnark (This script is no longer maintained, but you can also use this from Krinkle or some other).

For a/my full extensive config see User:Perhelion/Wikieditor-config.js.

mw.loader.load('//de.wikipedia.org/w/index.php?title=Benutzer:Schnark/js/wikieditor.js/basic.js&action=raw&ctype=text/javascript&maxage=86400&smaxage=86400');

mw.libs.wikieditor = {
 config: function (wikieditor) {
	// Valid SVG :
	wikieditor.addMyButton(
		'', //position
		'//upload.wikimedia.org/wikipedia/commons/thumb/6/66/W3C_valid.svg/40px-W3C_valid.svg.png', //imageFile
		'Insert Valid SVG', //speedTip
		'{{Valid SVG}}', //tagOpen
		'', //sampleText
		'\n', //tagClose
		true, //ownline
		//callbackFunc:
		wikieditor.addComment("Valid SVG+ ")
	);
	// Invalid SVG :
	wikieditor.addMyButton(
		'', //position
		'//upload.wikimedia.org/wikipedia/commons/thumb/4/4b/W3C_Icon.svg/40px-W3C_Icon.svg.png', //imageFile
		'Insert Invalid SVG', //speedTip
		'{{Invalid SVG}}', //tagOpen
		'', //sampleText
		'\n', //tagClose
		true, //ownline
		//callbackFunc:
		wikieditor.addComment("Invalid SVG+ ")
	);
 }
};

Note: If you have Fliegelflagel you don't need the first line.


Mediawiki:Edittools

[edit]
	// [[Mediawiki:Edittools]] smaller (HACK)
	if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1)
		$.when(mw.loader.using('ext.wikiEditor'), $.ready).then( mw.util.addCSS(".page-Edittools1.page-characters {font-size:11px}") );

Help:QuickDelete

[edit]
  • I'm using an expensive config of AjaxDeleteExtraButtons on my js-skin-page. (Don't worry, this need no admin rights.)

Scripts

[edit]

For sorting files tagging with {{Convert to SVG}}:

var ns = mw.config.get('wgNamespaceNumber'),
	ti = mw.config.get('wgTitle');
 // SORT CONVERT-TO-SVG TAGS:
 if ([6, 14].indexOf(nc) !== -1 && !/SVG/i.test(ti.slice(-3))))
     importScript('User:Perhelion/fixconverttosvg.js');
Note: The yellow lines are the custom var name and could be redundant or adapted. Code: User:Perhelion/fixconverttosvg.js

AWB

[edit]

sMirc =)

[edit]
  • All sMirC-smilies on the toolbar =>

Image-URL-Export

[edit]

At wish:[2]

var list = [];
var cat = (mw.config.get('wgNamespaceNumber') === 14)? mw.config.get('wgPageName'): "Category:" + prompt("Category-name");
mw.loader.using('mediawiki.api', getAllImages);
function getAllImages(con) { // Get Image-URLs
	if (typeof con !== "string") con = "";
	new mw.Api().get({
		prop: "imageinfo",
		iiprop: "url",
		generator: "categorymembers",
		gcmtitle: cat,
		gcmtype: "file",
		gcmnamespace: 6,
		gcmcontinue: con,
		gcmlimit: 500
	}).done(function (json) {
		if (!json || !json.query || !json.query.pages) return;
		var data = json.query.pages;
		for (var p in data) list.push(data[p].imageinfo[0].url);
		if (json.continue) return getAllImages(json.continue.gcmcontinue);
		$("#mw-content-text").prepend(list.join(" ")); // Output
		console.log(list.length, list);
	});
}

Uncommon file extension move

[edit]

Sometimes a set of images have a uppercase file extension (.JPG). But if you need to rename a file name, there is no way to keep the uppercase, so we can use this per manually API:[3]

$.getJSON(mw.util.wikiScript("api"), {
	"action": "move",
	"format": "json",
	"from": "File:***.jpg",
	"to": "File.***.JPG",
	"reason": "name of a set (wish of uploader)",
	"noredirect": 1,
	"ignorewarnings": 0,
	"token": mw.user.tokens.get("csrfToken")
});

Enhanced watchlist expand button

[edit]

Toggles all collapsible groups per day of "changes by page" on the enhanced watchlist.

var ns = mw.config.get('wgNamespaceNumber');
var cpn = mw.config.get('wgCanonicalSpecialPageName');

$(function () {
if (ns === -1 && cpn === 'Watchlist') { // Watchlist expand
	var cont = $('#mw-content-text');
	var jQsel = 'table.mw-collapsible span.mw-collapsible-toggle.mw-collapsible-arrow';  // .mw-enhancedchanges-arrow.mw-enhancedchanges-arrow-space
	var wlToggleArrow = cont.find(jQsel).first().addClass('mw-collapsible-toggle-collapsed');
	cont.find('.mw-changeslist h4').append(wlToggleArrow.clone().css('float', 'left').off('click').on('click', function () {
			$(this).parent().next().find(jQsel).click();
		}));
	// $("table.mw-collapsed").removeClass("mw-collapsed");  // Hacky expand
}
});

See also

[edit]


English: “The best tools are written from people they are using itself.”
Deutsch: „Die besten Tools werden von Leuten geschrieben die sie selbst benutzen.“