MediaWiki:Gadget-fixCrossWikiNotif.js

From Support Wiki
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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using(['ext.echo.api', 'mediawiki.language']).then(function() {
	if (mw.echo.api.EchoApi.prototype.DEFAULTfetchNotifications) return;
	mw.echo.api.EchoApi.prototype.DEFAULTfetchNotifications = mw.echo.api.EchoApi.prototype.fetchNotifications;
	mw.echo.api.EchoApi.prototype.fetchNotifications = function() {
		return this.DEFAULTfetchNotifications.apply(this, arguments).then(function(data) {
			var foreignNotifData = ( data || { list: [] } ).list.find(function(notifData) {
					return notifData.type === 'foreign';
				}), wikiList, source, title;
			if (foreignNotifData) {
				wikiList = [];
				for (source in foreignNotifData.sources) {
					title = source.replace('_', '.wiki.gg/').replace(/\/en$/, '');
					wikiList.push(title);
					foreignNotifData.sources[source].title = title;
					foreignNotifData.sources[source].url = 'https://' + title + '/api.php';
					foreignNotifData.sources[source].base = 'https://' + title + '/wiki/$1';
				}
				if (foreignNotifData['*'] && foreignNotifData['*'].body === mw.language.listToText(Array(wikiList.length).fill(''))) {
					foreignNotifData['*'].body = mw.language.listToText(wikiList);
				}
			}
			return data;
		});
	};
});