MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (Clean test output) |
||
(42 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
/*Code from https://www.mediawiki.org/ | /*Code from https://www.mediawiki.org/w/index.php?title=User:TheDJ/common.js&oldid=2187846*/ | ||
function | function sideJSONMessage(messageName) { | ||
//console.log("Entering " + messageName); | |||
jQuery.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), jQuery.ready ).done( function() { | |||
//console.log("Entered"); | |||
new mediaWiki.Api().loadMessagesIfMissing([messageName, 'tooltip-' + messageName]).done( function() { | |||
AddJSONLinkToSidebar(messageName); | |||
}); | |||
}); | |||
} | |||
} | } | ||
function AddItemToSidebar(section, idName | function AddItemToSidebar(section, idName, link) { | ||
var target | var target; | ||
try { | try { | ||
Line 82: | Line 34: | ||
//See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript | //See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript | ||
console.log("idName: " + idName | //console.log("idName: " + idName); | ||
var node = document.getElementById( target ) | var node = document.getElementById( target ) | ||
Line 109: | Line 43: | ||
var liNode = document.createElement( 'li' ); | var liNode = document.createElement( 'li' ); | ||
aNode.text = mw.message(idName).text(); | |||
aNode.setAttribute( 'href', link ); | |||
aNode.setAttribute( 'title', mw.message('tooltip-' + idName).text()); | |||
liNode.appendChild( aNode ); | liNode.appendChild( aNode ); | ||
liNode.id = idName; | liNode.id = idName; | ||
node.appendChild( liNode ); | node.appendChild( liNode ); | ||
Line 128: | Line 58: | ||
} | } | ||
function AddJSONLinkToSidebar(messageName) { | |||
function AddJSONLinkToSidebar() { | |||
/*Returns an array containing the variables requested. | /*Returns an array containing the variables requested. | ||
/*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/ | /*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/ | ||
Line 155: | Line 64: | ||
'wgServer', | 'wgServer', | ||
'wgWikibaseItemId', | 'wgWikibaseItemId', | ||
'wgCanonicalNamespace', | |||
'wgPageName' | 'wgPageName' | ||
]); | ]); | ||
var PageName = conf.wgPageName; | var PageName = conf.wgPageName; | ||
var nameSpace = conf.wgCanonicalNamespace; | |||
var nameSpaceList = ['Item', 'Property', 'Lexeme'] | |||
/* | /* | ||
console.log(PageName); | console.log(PageName); | ||
console.log(PageName.substr(0, 5)); | console.log(PageName.substr(0, 5)); | ||
console.log(PageName.substr(0, 5).toLowerCase()); | console.log(PageName.substr(0, 5).toLowerCase()); | ||
console.log('Namespace ' + conf.wgCanonicalNamespace); | |||
console.log('Namespace ' + conf.wgCanonicalNamespace); | |||
console.log('IndexOf ' + nameSpaceList.indexOf(PageName)); | |||
*/ | */ | ||
if (PageName.substr(0, 5).toLowerCase() == "item:") { | |||
if (nameSpaceList.indexOf(nameSpace) > -1) { | |||
//if (PageName.substr(0, 5).toLowerCase() == "item:") { | |||
var ItemName = PageName.split(':'); | var ItemName = PageName.split(':'); | ||
AddItemToSidebar('toolbox', messageName, conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json'); | |||
AddItemToSidebar('toolbox', | |||
} | } | ||
} | } | ||
jQuery(sideJSONMessage('t-wb-json')); | |||
Latest revision as of 21:45, 28 March 2021
/* Any JavaScript here will be loaded for all users on every page load. */
/*Code from https://www.mediawiki.org/w/index.php?title=User:TheDJ/common.js&oldid=2187846*/
function sideJSONMessage(messageName) {
//console.log("Entering " + messageName);
jQuery.when(mw.loader.using(['mediawiki.api', 'mediawiki.jqueryMsg']), jQuery.ready ).done( function() {
//console.log("Entered");
new mediaWiki.Api().loadMessagesIfMissing([messageName, 'tooltip-' + messageName]).done( function() {
AddJSONLinkToSidebar(messageName);
});
});
}
function AddItemToSidebar(section, idName, link) {
var target;
try {
switch ( section ) {
case 'languages':
target = 'p-lang';
break;
case 'toolbox':
target = 'p-tb';
break;
case 'navigation':
target = 'p-navigation';
break;
default:
target = 'p-' + section;
break;
}
//See https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Message
//See https://www.mediawiki.org/wiki/Manual:Messages_API#Using_messages_in_JavaScript
//console.log("idName: " + idName);
var node = document.getElementById( target )
.getElementsByTagName( 'div' )[0]
.getElementsByTagName( 'ul' )[0];
var aNode = document.createElement( 'a' );
var liNode = document.createElement( 'li' );
aNode.text = mw.message(idName).text();
aNode.setAttribute( 'href', link );
aNode.setAttribute( 'title', mw.message('tooltip-' + idName).text());
liNode.appendChild( aNode );
liNode.id = idName;
node.appendChild( liNode );
} catch( e ) {
// let's just ignore what's happened
return;
}
}
function AddJSONLinkToSidebar(messageName) {
/*Returns an array containing the variables requested.
/*From https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#mediaWiki.config*/
var conf = mw.config.get([
'wgServer',
'wgWikibaseItemId',
'wgCanonicalNamespace',
'wgPageName'
]);
var PageName = conf.wgPageName;
var nameSpace = conf.wgCanonicalNamespace;
var nameSpaceList = ['Item', 'Property', 'Lexeme']
/*
console.log(PageName);
console.log(PageName.substr(0, 5));
console.log(PageName.substr(0, 5).toLowerCase());
console.log('Namespace ' + conf.wgCanonicalNamespace);
console.log('Namespace ' + conf.wgCanonicalNamespace);
console.log('IndexOf ' + nameSpaceList.indexOf(PageName));
*/
if (nameSpaceList.indexOf(nameSpace) > -1) {
//if (PageName.substr(0, 5).toLowerCase() == "item:") {
var ItemName = PageName.split(':');
AddItemToSidebar('toolbox', messageName, conf.wgServer + '/wiki/Special:EntityData/' + ItemName[1] + '.json');
}
}
jQuery(sideJSONMessage('t-wb-json'));