User:Random User v1/global.js

/** * * AnnouncementsIgnore * Allows users to ignore announcements for certain wikis * With opt-out-all, you will opt out all wikis except those in exceptWikiIds * With opt-in-all, you will opt in all wikis, except those in exceptWikiIds * * Usage: Please see documentation. Personal use only. */ // Show all announcements except those from 12345 and 12606 window.announcementsIgnore = { option: 'opt-in-all', exceptWikiIds: [ 12345,       12606    ] }; // Ignore all announcements except those from 12345 and 12606 window.announcementsIgnore = { option: 'opt-out-all', exceptWikiIds: [ 12345,       12606    ] };   var options = (window.announcementsIgnore) || {}, optionSet = 'option' in options && (options.option == 'opt-in-all' || options.option == 'opt-out-all'), wikiIds = null, isWikiaOrg = mw.config.get('wgServer').indexOf('wikia.org') !== -1, servicesUrl = 'https://services.' + (isWikiaOrg ? 'wikia.org' : 'fandom.com'), isUCP = mw.config.get('wgVersion') !== '1.19.24';
 * (function(mw, $) {

// Clean wiki IDs if ('exceptWikiIds' in options && $.isArray(options.exceptWikiIds)) { wikiIds = []; $.each(options.exceptWikiIds, function(i, id) {        if (wikiIds.indexOf(String(id)) === -1) {            wikiIds.push(String(id));         }      }); }

if (!optionSet || wikiIds === null) { console.error('DiscussionsAnnounceOpt-In-Out: window.announcementsIgnore is not set correctly. No announcements ignored.'); return; }

var dai = {};

// Starts the script and marks all unwanted announcements as read dai.init = function { var newIgnoreURIs = []; // Get all announcements dai.getAnnouncements.then(function(data) {        // Collect URIs to mark as read         data.notifications.forEach(function(announcement) { if (!announcement.read && announcement.type == 'announcement-notification') { if (options.option == 'opt-in-all' && wikiIds.indexOf(announcement.community.id) > -1) { newIgnoreURIs.push(announcement.refersTo.uri); }              if (options.option == 'opt-out-all' && wikiIds.indexOf(announcement.community.id) === -1) { newIgnoreURIs.push(announcement.refersTo.uri); }           }         });

if (!newIgnoreURIs.length) { return; }        dai.markAnnouncementsRead(newIgnoreURIs).then(function(data) {            console.log('Ignored ' + newIgnoreURIs.length + ' announcements.');         }); });  };

// Gets list of unread announcements dai.getAnnouncements = function { return $.ajax({        url: servicesUrl + '/on-site-notifications/notifications/',         type: 'GET',         crossDomain: true,         xhrFields: {            withCredentials: true         },         data: {            contentType: 'announcement-target'         },         dataType: 'json'      }); };

// Marks a list of URIs as read dai.markAnnouncementsRead = function(uris) { return $.ajax({        url: servicesUrl + '/on-site-notifications/notifications/mark-as-read/by-uri/',         type: 'POST',         crossDomain: true,         xhrFields: {            withCredentials: true         },         data: JSON.stringify(uris),         contentType: 'application/json; charset=UTF-8'      }).then(function(data) {         // For each URI, render the notification as read         if (!isUCP) {            require(['ext.wikia.design-system.on-site-notifications.view'], function(View) { uris.forEach(function(uri) {                 View.renderNotificationAsRead(uri);               }); var numNotifications = View._$notificationsCount.text; numNotifications = Math.max(numNotifications - uris.length, 0); View.renderUnreadCount(numNotifications); });        }      });   };

// Start the script dai.init; })(mediaWiki, jQuery);