User:Yair rand/ShowTalkLabels.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
$( function () {
function fromTalk( pageName ) {
var [ ns, title ] = pageName.split( ':' );
return ns === 'Talk' ? title : 'Property:' + title;
}
function fetchLabel( titles ) {
return fetch( '/w/api.php?format=json&action=query&prop=pageterms&wbptterms=label&titles=' + titles ).then( x => x.json() );
}
if ( ( [ 'Recentchanges', 'Watchlist', 'Recentchangeslinked', 'Contributions' ] ).includes( mw.config.get( 'wgCanonicalSpecialPageName' ) ) ) {
let talkLinks = Array.from( document.querySelectorAll( '.mw-title > a, .mw-contributions-title' ) )
.filter( link => [ 'Talk', 'Property talk' ].includes( link.title.split( ':' )[ 0 ] ) ),
titles = [ ...new Set( talkLinks.map( link => link.title ) ) ],
fetchTitles = () => titles.length === 0 ? Promise.resolve( [] ) : fetchLabel(
titles.splice( 0, 50 ).map( title => fromTalk( title ) ).join( '|' )
)
.then( results => fetchTitles().then( titles => titles.concat( Object.values( results.query.pages ) ) ) );
titles.length && fetchTitles().then( results => {
var map = {};
results.forEach( page => {
map[ page.title ] = page.terms && page.terms.label[ 0 ];
} );
talkLinks.forEach( link => {
var title = map[ fromTalk( link.title ) ];
if ( title ) {
link.innerText += ' (' + title + ')';
}
} );
} );
}
if ( [ 'Talk', 'Property_talk' ].includes( mw.config.get('wgCanonicalNamespace') ) ) {
fetchLabel( fromTalk( mw.config.get( 'wgPageName' ) ) )
.then( result => {
var title = Object.values( result.query.pages )[ 0 ].terms.label[ 0 ],
titlestr = ' (' + title + ')';
document.querySelector( '#firstHeading' ).appendChild( document.createTextNode( titlestr ) );
document.title = document.title.replace(' – ', titlestr + ' – ');
} );
}
} );