User:Xaosflux/globalwatchlist.js
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)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
//<nowiki>
$(function (){
var GlobalWatch_config = {
name: '[[:w:en:User:DannyS712/Global Watchlist|Global Watchlist]]',
version: 1.1,
debug: true
};
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
/*
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'GlobalWatch', 'ca-globalWatch', 'GlobalWatch');
$('#ca-globalWatch').on('click', function() {
GlobalWatch();
} );
*/
var page = mw.config.get('wgPageName');
console.log( page );
if ( page && page === 'Special:BlankPage/GlobalWatchlist'){
GlobalWatch();
}
} );
} );
async function GlobalWatch(){
window.document.title = 'Global watchlist';
$('#firstHeading')[0].innerText = 'Global watchlist';
/* Blank the "This page is intentionally left blank." */
var content = $('#mw-content-text')[0];
console.log( content );
content.innerHTML = ''; // blanked
/* Sites to retrieve from */
var sites = ['test.wikipedia.org', 'test2.wikipedia.org', 'meta.wikimedia.org', 'commons.wikimedia.org', 'www.wikidata.org'];
for (var iii = 0; iii < sites.length; iii++){
content.innerHTML += (await getWatch( sites[iii] ) );
}
}
async function getWatch( site ){
var local = mw.config.get( 'wgServerName' );
var raw;
if (site && local === site){
raw = await getLocalWatch();
} else {
raw = await getForeignWatch( site );
}
console.log( raw );
var feed = makeSiteList( raw, site );
return feed;
}
function getForeignWatch ( target ) {
return new Promise((resolve) => {
target = '//' + target + '/w/api.php';
var api = new mw.ForeignApi( target );
var getter = {
action: 'query',
list: 'watchlist',
wlprop: 'ids|title|flags|user|userid|comment|parsedcomment|timestamp|sizes|notificationtimestamp|loginfo|tags',
wltype: 'edit',
wlshow: 'unread',
wllimit: 'max',
format: 'json',
origin: 'https://' + mw.config.get( 'wgServerName' ),
};
console.log( getter );
api.get( getter ).done( function( response ) {
console.log(response);
//location.reload();
resolve(response.query.watchlist);
} );
} );
}
function getLocalWatch () {
return new Promise((resolve) => {
var getter = {
action: 'query',
list: 'watchlist',
wlprop: 'ids|title|flags|user|userid|comment|parsedcomment|timestamp|sizes|notificationtimestamp|loginfo|tags',
wltype: 'edit',
wlshow: 'unread',
wllimit: 'max',
format: 'json'
};
$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', getter, function( response ) {
console.log(response);
//location.reload();
resolve(response.query.watchlist);
} );
} );
}
function linked( WL, site ){
var list = [];
for (var iii = 0; iii < WL.length; iii++){
list.push( makeLink( WL[iii].title, WL[iii].title, site ) );
}
console.log( list );
return list;
}
function makeLink( link, text, site ){
return "<a href='https://" + site + '/w/index.php?title=' + link.replace(/'/g, '%27') + "'>" + text + "</a>";
}
function makeSiteList( watched, site){
var list = '';
var links = linked( watched, site );
list += '<h2>' + makeLink( 'Special:Watchlist', site, site) + '</h2>';
list += '<div class="div-col columns column-width" style="-moz-column-width: 30em; -webkit-column-width: 30em; column-width: 30em;">';
list += '<ul>';
for (var iii = 0; iii < links.length; iii++){
list += '<li>' + links[iii] + '</li>';
}
list += '</div>';
list += '</ul>';
return list;
}
});
//</nowiki>