User:Jovtrc/common.js

mediaWiki.util.addPortletLink('p-navigation', "javascript:CEPMailing;", 'CEP Mass-mailing', 't-cepmassmail', 'Send wikimails to the user using the CEP mass-mailing prototype', , );

var totalUsers = 1; var usersList = [];

function CEPMailing { var firstHeading = document.querySelector('#firstHeading'); var bodyContent = document.querySelector('#bodyContent');

var CEPContent = ' 0 Participants Add new participant  Add user   Send message to attendees Message title Message body  Send message   #bodyContent {overflow: hidden;}.cep__mailing { float: left; } .cep__mailing *, .cep__sendmail * {box-sizing: border-box;}.cep__mailing {display: flex;max-width: 500px;flex-wrap: wrap;gap: 24px;}.cep__participants {width: calc(30% - 12px);background: #f6f6f6;font-size: 18px;font-weight: bold;text-align: center;padding: 15px 0;}.cep__add {width: calc(70% - 12px);background: #f6f6f6;padding: 15px 30px;font-weight: bold;}.cep__list {border-radius: 4px;width: 100%;border: 1px solid #a7d8f9;}.cep__participants span {display: block;font-size: 24px;}.cep__add fieldset {margin: 10px 0 0;border: 0;padding: 0;display: flex;}.cep__add input {height: 30px;padding: 8px;width: 70%;border: 1px solid #a2a9b1;border-radius: 2px 0 0 2px;}.cep__add button {cursor: pointer;border: 1px solid #a2a9b1;background: #fff;border-left: 0;width: 30%;}.cep__list table {width: 100%;}.cep__list thead {background: #a7d8f9;}.cep__list th {padding: 12px;text-align: left;}.cep__list td {padding: 18px 12px;}.cep__list tr+tr td {border-top: 1px solid #a7d9f9;}.cep__sendmail {float: right;width: 500px;border: 1px solid #a2a9b1;}.cep__header {border-bottom: 1px solid #e5e5e5;padding: 10px 15px;font-weight: bold;}.cep__form {padding: 25px 15px;}.cep__form label {display: block;margin-bottom: 15px;}.cep__form input, .cep__form textarea {width: 100%;border: 1px solid #a2a9b1;padding: 10px;}.cep__form input {height: 35px;}.cep__form textarea {height: 120px;}.cep__button {background: #0845ad;margin: 0 auto;display: inherit;padding: 12px 15px;border: 0;color: #fff;font-weight: bold;cursor: pointer;} ';

firstHeading.innerText = 'CEP Mass-mailing'; bodyContent.innerHTML = CEPContent;

var addUserButton = document.querySelector('.cep__add button'); var sendMessageButton = document.querySelector('.cep__button');

addUserButton.addEventListener('click', addUser); sendMessageButton.addEventListener('click', prepareEmail); }

function addUser { var addUserInput = document.querySelector('.cep__add input');

if (addUserInput.value && totalUsers <= 50) { updateUserList(addUserInput.value); } }

function updateUserList(user) { if (user) { var userListBody = document.querySelector('.cep__list tbody'); var userNumber = document.querySelector('.cep__participants span');

userNumber.innerHTML = totalUsers; userListBody.insertAdjacentHTML('beforeend', " " + user + " "); totalUsers++; usersList.push(user); } }

function prepareUsers(users) { var req = newRequest; var usersString = users.join('|'); var allUsers = { enabled: [], disabled: [] }; var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&list=users&ususers=' + usersString + '&usprop=emailable';

req.open("GET", url, false); req.send("");

var reqResponse = req.responseXML.getElementsByTagName('user'); var reqResponseArr = Array.prototype.slice.call(reqResponse);

reqResponseArr.forEach(function (user, key) {       if (user.getAttribute('emailable') !== null) {            allUsers.enabled.push(user.getAttribute('name'));        } else {            allUsers.disabled.push(user.getAttribute('name'));        }    });

return allUsers; }

function getToken { var theToken = false; var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&meta=tokens'; var req = newRequest;

req.open("GET", url, false); req.send("");

var reqResponse = req.responseXML.getElementsByTagName('tokens');

if (reqResponse) { theToken = reqResponse[0].getAttribute('csrftoken'); }

return theToken; }

function prepareEmail(e) { e.preventDefault;

var theToken = getToken;

if (theToken) { var allUsers = prepareUsers(usersList) var enabledUsers = allUsers.enabled; var disabledUsers = allUsers.disabled.join(', ');

var emailSubject = document.querySelector('.cep__form input'); var emailMessage = document.querySelector('.cep__form textarea');

sendEmail(enabledUsers, emailSubject.value, emailMessage.value, theToken);

if (allUsers.disabled.lenght >= 1) { alert("E-mails sent. It was not possible to send email to the following users: " + disabledUsers); } else { alert("All e-mails sent"); }   } else { alert("You must be authenticated to use the tool."); } }

function sendEmail(users, subject, message, token) { var req = newRequest; users.forEach(function (user, key) {       var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php?action=emailuser&target=" + user + "&text=" + message + "&subject=" + subject;

req.open("POST", url, true); req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); req.send(url + "&token=" + encodeURIComponent(token)); }); }

function newRequest { try { if (window.XMLHttpRequest) { return new XMLHttpRequest; } else { return new ActiveXObject("MSXML2.XMLHTTP"); }   } catch (e) { return false; } }