MediaWiki

Difference between revisions of "Common.js"

m (renamed LinguaZip)
 
(96 intermediate revisions by 5 users not shown)
Line 1: Line 1:
// Replace Wikidata IDs with their [label, description]
+
// Page-specific scripts modules and conditional loading
if ( $( '.wb-external-id' ).length > 0 ) {
+
// RecordWizard sugar
mw.loader.using( 'mediawiki.ForeignApi', function() {
+
 
$( '.wb-external-id' ).each( function() {
+
/* *************************************************************** */
if ( $( this ).attr( 'href' ).lastIndexOf( 'https://www.wikidata.org', 0 ) === 0 ) {
+
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', {
+
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
anonymous: true,
+
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
parameters: { 'origin': '*' },
+
//🎷🥁🎸🐣📔🗻🌏
ajax: { timeout: 10000 }
+
// LastAudiosBoxes
} ),
+
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
lang = mw.config.get( 'wgUserLanguage' ),
+
  console.log('🎸 Last Audios');
node = $( this );
+
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
wikidataApi.get( {
+
}
'action': 'wbgetentities',
+
// SoundLibrary
'format': 'json',
+
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
'ids': node.text(),
+
  console.log('🥁 Sound library');
'props': 'labels|descriptions',
+
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
'languages': lang,
+
}
'languagefallback': 1,
+
// Item pages additional sugar
'origin': '*'
+
if (/^(Q|Property:P)[0-9]+$/.test(mw.config.get('wgPageName'))) {
} ).then( function( data ) {
+
  console.log('🍭 ItemsSugar ');
                                       
+
  mw.loader.load('/index.php?title=MediaWiki:ItemsSugar.js&action=raw&ctype=text/javascript');
var entity = data.entities[ node.text() ],
+
}
label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ),
+
// Lexeme Queries Generator (OOUI)
description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' );
+
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_/.test(mw.config.get('wgPageName'))) {
+
  console.log('📔 Lexeme Queries Generator');
node.html( label + '<br>' + description )
+
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
} );
+
}
}
+
// SPARQL to data (OOUI)
} );
+
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
} );
+
  console.log('🚀 SPARQL to data');
 +
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
 +
}
 +
// Click bug & browser
 +
if (/browser/.test(mw.config.get('wgPageName')) || /bug/.test(mw.config.get('wgPageName'))) {
 +
  console.log('🎷 Click bug & browser');
 +
  mw.loader.load('/index.php?title=MediaWiki:Browser.js&action=raw&ctype=text/javascript');
 
}
 
}
 +
/* *************************************************************** */
 +
/* In development *************************************** */
 +
// User:Yug: Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
 +
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
 +
  console.log('🐣 Search by word');
 +
  mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
 +
}
 +
// Minorities languages statistics page
 +
 +
// Iframe gallery for Lingualibre:Apps
 +
// Prototype https://jsfiddle.net/hugolpz/meygpo6t/
 +
// Problem: iframe are not accepted into wikicode. Solution: hide key values in html tag, generate iframe via JS injection.
 +
 +
 +
/*Query visualisation: Replace loading text by a spinner gif***********************************************************/
 +
$( '.queryviz-loading' ).html( '<center><img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Balls.gif" width="50" /></center>' );
  
//Add an audio player to the audio records links in the wikibase items
+
// Load LiLiZip.js gadget (full script only loaded when user visits the dedicated page)
const BASE_FILE_PATH = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
+
mw.loader.load('/index.php?title=MediaWiki:LiLiZip.js/load.js&action=raw&ctype=text/javascript');
$( '#P3 a.extiw' ).each( function() {
 
    var $node = $( this ),
 
        $audio = $( '<audio>' )
 
            .attr( 'src', BASE_FILE_URL + $node.text() )
 
            .attr( 'controls', true );
 
    $node.after( $audio );
 
} );
 

Latest revision as of 10:32, 3 September 2024

// Page-specific scripts modules and conditional loading
// RecordWizard sugar

/* *************************************************************** */
/* PAGE-SPECIFIC SCRIPTS LOADINGS ******************************** */
// Documentation: https://m.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Scripts
// Documentation: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.loader
//🎷🥁🎸🐣📔🗻🌏
// LastAudiosBoxes
if (/^LinguaLibre:Main/.test(mw.config.get('wgPageName'))) {
  console.log('🎸 Last Audios');
  mw.loader.load('/index.php?title=MediaWiki:LastAudios.js&action=raw&ctype=text/javascript');
}
// SoundLibrary
if (/^LinguaLibre:Explore_the_sound_library(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🥁 Sound library');
  mw.loader.load('/index.php?title=MediaWiki:SoundLibrary.js&action=raw&ctype=text/javascript');
}
// Item pages additional sugar
if (/^(Q|Property:P)[0-9]+$/.test(mw.config.get('wgPageName'))) {
  console.log('🍭 ItemsSugar ');
  mw.loader.load('/index.php?title=MediaWiki:ItemsSugar.js&action=raw&ctype=text/javascript');
}
// Lexeme Queries Generator (OOUI)
if (/^Template:LexemeQueriesGenerator$/.test(mw.config.get('wgPageName')) || /^Help:SPARQL_/.test(mw.config.get('wgPageName'))) {
  console.log('📔 Lexeme Queries Generator');
  mw.loader.load('/index.php?title=MediaWiki:LexemeQueriesGenerator.js&action=raw&ctype=text/javascript');
}
// SPARQL to data (OOUI)
if (/SPARQL/.test(mw.config.get('wgPageName')) || /parql/.test(mw.config.get('wgPageName'))) {
  console.log('🚀 SPARQL to data');
  mw.loader.load('/index.php?title=MediaWiki:Sparql2data.js&action=raw&ctype=text/javascript');
}
// Click bug & browser
if (/browser/.test(mw.config.get('wgPageName')) || /bug/.test(mw.config.get('wgPageName'))) {
  console.log('🎷 Click bug & browser');
  mw.loader.load('/index.php?title=MediaWiki:Browser.js&action=raw&ctype=text/javascript');
}
/* *************************************************************** */
/* In development *************************************** */
// User:Yug: Search by words, see https://jsfiddle.net/hugolpz/ecpzy0fo/89/
if (/^LinguaLibre:Search_by(\/[a-z_-]+)?$/.test(mw.config.get('wgPageName'))) {
  console.log('🐣 Search by word');
  mw.loader.load('/index.php?title=MediaWiki:SearchBy.js&action=raw&ctype=text/javascript');
}
// Minorities languages statistics page

// Iframe gallery for Lingualibre:Apps
// Prototype https://jsfiddle.net/hugolpz/meygpo6t/
// Problem: iframe are not accepted into wikicode. Solution: hide key values in html tag, generate iframe via JS injection.


/*Query visualisation: Replace loading text by a spinner gif***********************************************************/
$( '.queryviz-loading' ).html( '<center><img src="https://upload.wikimedia.org/wikipedia/commons/7/7a/Balls.gif" width="50" /></center>' );

// Load LiLiZip.js gadget (full script only loaded when user visits the dedicated page)
mw.loader.load('/index.php?title=MediaWiki:LiLiZip.js/load.js&action=raw&ctype=text/javascript');