User:Spas.Z.Spasov/InternalWhitelistManager.js
Appearance
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.
/**
* Add a page to MediaWiki:Whitelist or remove it via the dropdown toolbar 'More'.
* The script is designed to work with Extension:InternalWhitelist.
* Currently available languages: En, Bg, Ru.
*
* @author Spas Zdravkov Spasov <spas.z.spasov@gmail.com> (c) 2019
*
* References:
* - https://www.mediawiki.org/wiki/Extension:InternalWhitelist
* - https://www.mediawiki.org/wiki/API:Edit
* - https://www.mediawiki.org/wiki/Manual:Interface/JavaScript
* - https://en.wikipedia.org/wiki/Help:Customizing_toolbars
**/
(function (mw, $) {
// Definition of the necessary variables.
var whitelistMenuItem;
var currentPageName = mw.config.get('wgPageName');
var currentUserLanguage = mw.config.get('wgUserLanguage');
var whitelisPageURI = mw.config.get('wgArticlePath').replace('$1', 'MediaWiki:Whitelist?action=raw');
var label;
var captionPublic;
var captionPrivate;
// If the action is 'view', then run the main function.
if (mw.config.get('wgAction') === 'view') {
generateLabelAndCaption();
isWhitelisted();
}
// This is the main function.
function isWhitelisted() {
$.ajaxSetup({ cache: false });
$.get(whitelisPageURI, function(data){
if (data.includes(currentPageName) === true) {
publicPageMenuItem();
$(whitelistMenuItem).click(function () {
removeFromWhitelist(); //alert('Remove the page from MediaWiki:Whitelist.');
privatePageMenuItem();
window.location.reload(true); // Avoid confusions
});
} else {
privatePageMenuItem();
$(whitelistMenuItem).click(function () {
addToWhitelist(); //alert('Add the page to MediaWiki:Whitelist.');
publicPageMenuItem();
window.location.reload(true); // Avoid confusions
});
}
});
}
// Generate menu item if the current page belongs to MediaWiki:Whitelist
function publicPageMenuItem() {
//alert('The page is public!');
if (whitelistMenuItem) { whitelistMenuItem.parentNode.removeChild(whitelistMenuItem); }
whitelistMenuItem = mw.util.addPortletLink('p-cactions', '#', '✗ ' + label + ' ', 'ca-internal-whitelist-manager', captionPrivate, 'g', '#ca-delete');
}
// Generate menu item if the current page doesn't belong to MediaWiki:Whitelist
function privatePageMenuItem() {
//alert('The page is not public.');
if (whitelistMenuItem) { whitelistMenuItem.parentNode.removeChild(whitelistMenuItem); }
whitelistMenuItem = mw.util.addPortletLink('p-cactions', '#', '✓ ' + label + ' ', 'ca-internal-whitelist-manager', captionPublic, 'g', '#ca-delete');
}
// Generate the menu item label, depending on the user's language.
function generateLabelAndCaption() {
switch(currentUserLanguage) {
case "bg":
label = 'Досъп';
captionPublic = 'Направи статията публична';
captionPrivate = 'Направи статията частна';
break;
case "ru":
label = 'Доступ';
captionPublic = 'Сделайте статью общедоступной';
captionPrivate = 'Сделайте статью приватной';
break;
default: // "English"
label = 'Access';
captionPublic = 'Make the article public';
captionPrivate = 'Make the article private';
}
}
function addToWhitelist() {
$.get(whitelisPageURI, function(data){
if (data.includes(currentPageName) === false) {
var params = {
action: 'edit',
title: 'MediaWiki:Whitelist',
section: 'new',
appendtext: '* ' + currentPageName,
format: 'json'
},
api = new mw.Api();
api.postWithToken('csrf', params).done(function (data) {
console.log(data);
});
}
});
}
function removeFromWhitelist() {
$.get(whitelisPageURI, function(data){
// Catch all cases: if the entry is found one or many times,
// at the beginning, at the end or at the middle of the list.
// Also when the syntaxis of the blocks is correct or not.
if (data.includes('* ' + currentPageName + '\n\n') === true) {
while (data.includes('* ' + currentPageName + '\n\n') === true) {
data = data.replace('* ' + currentPageName + '\n\n', '');
}
}
if (data.includes('* ' + currentPageName + '\n') === true) {
while (data.includes('* ' + currentPageName + '\n') === true) {
data = data.replace('* ' + currentPageName + '\n', '');
}
}
if (data.includes('\n\n* ' + currentPageName) === true) {
while (data.includes('\n\n* ' + currentPageName) === true) {
data = data.replace('\n\n* ' + currentPageName, '');
}
}
if (data.includes('\n* ' + currentPageName) === true) {
while (data.includes('\n* ' + currentPageName) === true) {
data = data.replace('\n* ' + currentPageName, '');
}
}
if (data.includes('* ' + currentPageName) === true) {
while (data.includes('* ' + currentPageName) === true) {
data = data.replace('* ' + currentPageName, '');
}
}
var params = {
action: 'edit',
title: 'MediaWiki:Whitelist',
text: data,
format: 'json'
},
api = new mw.Api();
api.postWithToken('csrf', params).done(function (data) {
console.log(data);
});
});
}
}(mediaWiki, jQuery));