MediaWiki:Common.js: Difference between revisions

From Coral Island Wiki
Jump to navigation Jump to search
(personal styles and scripts normally load last to give highest priority)
(the format varies per action so just make it easier to customize)
Line 13: Line 13:
mw.hook('wikipage.content').add(function(){
mw.hook('wikipage.content').add(function(){
var actions = {
var actions = {
e: 'edit',
e: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=edit'; },
h: 'history',
h: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=history'; },
m: 'move',
m: function(page){ return config.wgServer+mw.util.getUrl('Special:MovePage/'+page); },
d: 'delete',
d: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=delete'; },
};
};
if (config.wgAction=='view') {
if (config.wgAction=='view') {
var newUrl = new URL(document.location.href);
var url = new URL(document.location.href);
var title =
url.searchParams.get('title') || // for `/w/index.php?title=XYZ` links
url.pathname.replace(/^.*?wi?k?i?\//, ''); // for `/wiki/XYZ` links
$(document).on('keydown', function(e) {
$(document).on('keydown', function(e) {
var key = e.key.toLocaleLowerCase();
var key = e.key.toLocaleLowerCase();
Line 27: Line 30:
config.wgCanonicalSpecialPageName===false
config.wgCanonicalSpecialPageName===false
) {
) {
newUrl.searchParams.set('action', actions[key]);
document.location.href= actions[key](title);
document.location.href=newUrl.href;
}
}
});
});

Revision as of 06:05, 10 July 2024

var config = mw.config.values;

// Load site JS
[
	'BetterUpload.js',	// Improvements to Special:Upload
	'BetterDiff.js'		// Aids with patrolling and with diff viewing on RC, page history and user contribs
]
.forEach(function(scr){
	importScript('MediaWiki:'+scr);
});

// Add navigational keybinds
mw.hook('wikipage.content').add(function(){
	var actions = {
		e: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=edit'; },
		h: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=history'; },
		m: function(page){ return config.wgServer+mw.util.getUrl('Special:MovePage/'+page); },
		d: function(page){ return config.wgServer+mw.util.getUrl(page)+'?action=delete'; },
	};
	if (config.wgAction=='view') {
		var url = new URL(document.location.href);
		var title = 
			url.searchParams.get('title') ||			// for `/w/index.php?title=XYZ` links
			url.pathname.replace(/^.*?wi?k?i?\//, '');	// for `/wiki/XYZ` links
		$(document).on('keydown', function(e) {
			var key = e.key.toLocaleLowerCase();
			if (
				actions[key] &&
				!(e.target && ['INPUT', 'TEXTAREA'].includes(e.target.nodeName)) &&
				config.wgCanonicalSpecialPageName===false
			) {
				document.location.href= actions[key](title);
			}
		});
	}
});

// Load personal JS & CSS if logged in user
if (config.wgUserName && config.wgUserName.length>0) {
	importScript('User:'+config.wgUserName+'/common.js');
	importStylesheet('User:'+config.wgUserName+'/common.css');
}