User:Thundercraft5/global.js/OutreachMessage.js

/** * Vanguard outreach message sender * A script to quickly send outreach messages for Vanguard work * @version 1.0 * @author Thundercraft5 * @license BSD-3 clause  or CC-BY-SA 4.0 */ /* jshint esversion: 6, jquery: true */ /* global mw, importArticles */ "use strict";

function main { var modal, $title, $ui, $checkbox, $userInput, didSetup; function formatMessage(text, parameters) { parameters.forEach(function(param, i) {			text = text.replaceAll(new RegExp('\\$' + (i+1), 'g'), param);		}); return text; }	function resHandler(username, link) { var messageHref = !link.includes('/wiki/') ? mw.util.getUrl(link) : link; var userHref = mw.util.getUrl(username); mw.notify($(' ').html([ 'Successfully sent an outreach message to ', $('', { href: userHref, title: userHref, text: username }), ' (',			$('', { href: messageHref, title: messageHref, text: 'view', }),			' &bull; ',			$('', { text: "copy", click { window.navigator.clipboard.writeText(mw.config.get('wgServer') + messageHref); },			}),			')',		]));	}	function handler { modal.hide; var username = $userInput.getValue; new mw.Api.get({					action: 'query',			list: 'users',			ususers: username,		}).then(function(data) {			mw.hook('dev.enablewallext').add(wgMessageWallsExist => { var regular = JSON.parse(formatMessage( mw.messages.get('Custom-vanguard-contactmesage2.json'), [ mw.config.get('wgUserName') ])				);				var date = new Date; date.setDate(date.getDate + 2); var optOut = JSON.parse(formatMessage( mw.messages.get('Custom-vanguard-contactmesage1.json'), [ mw.config.get('wgUserName'), date.toDateString, ])				);				var title = $title.val || "Wiki Code Issues"; var useOptout = $checkbox.prop('checked'); var userid = data.query.users[0].userid; wgMessageWallsExist.then( => {					fetch("/wikia.php?controller=Fandom%5CMessageWall%5CMessageWall&method=createThread&format=json", { "headers": { "accept": "application/json, text/javascript, */*; q=0.01", "content-type": "application/x-www-form-urlencoded; charset=UTF-8", "x-requested-with": "XMLHttpRequest", },						"body": $.param({							token: mw.user.tokens.values.editToken,							title,							wallOwnerId: userid,							jsonModel: JSON.stringify(useOptout ? optOut.wall : regular.wall),							attachments: '{"contentImages":[],"openGraphs":[],"atMentions":[]}',						}), "method": "POST", "mode": "cors", "credentials": "include", }).then(res => { return res.json; }).then(json => { resHandler(username, mw.util.getUrl('Message wall:' + username) + '?' + $.param({ threadId: json.id, }));					});				}, => {					new mw.Api.newSection('User talk:' + username, title, (useOptout ? optOut.wikitext : regular.wikitext)).then(function { resHandler(							username, 							mw.util.getUrl('User talk:' + username + '#' + encodeURIComponent(title.replaceAll(' ', '_')).replaceAll('%', '.'))						); });				});			});		});	}	function setup { modal = new mw.libs.QDmodal('VangaurdMessage'); $checkbox = $(' ', {			type: "checkbox",			id: "do-outreach",			name: "do-outreach",			css: {				cursor: "pointer",			},			title: "Check this box to use an Opt-out in the outreach message",		}); $title = $(' ', {			type: "text",			title: "Outreach Message Title",			val: "Wiki Code Issues",			id: "message-title",			name: "message-title",		}); $userInput = new mw.widgets.UserInputWidget({			id: "target-username",		}); $userInput.setValue(mw.config.get('wgRelevantUserName') || mw.config.get('profileUserName')); modal.$element.css({			'width': '60%',			'height': '100%',		}); $ui = $(' ', {			html: [				$(' ', { html: [ $(' ', {							for: "target-username",							text: "Target Username: ",							title: "Target username",						}), $userInput.$element, ],				}),				$(' ', {					html: [ $checkbox, $(' ', {							for: "do-outreach",							text: "Use Opt-out",							css: {								cursor: "pointer",							},							title: "Check this box to use an Opt-out in the outreach message",						}), ],				}),				$(' ', {					html: [ $(' ', {							for: "message-title",							text: "Message title: ",							title: "Outreach Message Title",						}), $title, ],				}),			],		});	}	async function click { // jshint ignore:line /* jshint ignore: start */ await mw.loader.using(['mediawiki.widgets.UserInputWidget', 'mediawiki.ForeignApi']); await new mw.ForeignApi('https://thundercraft5.fandom.com/api.php', {			anonymous: true		}).loadMessagesIfMissing(['Custom-vanguard-contactmesage2.json', 'Custom-vanguard-contactmesage1.json']); /* jshint ignore: end */ importArticles({			type: 'script',			articles: [				'u:dev:MediaWiki:WgMessageWallsExist.js',				'u:dev:MediaWiki:QDmodal.js',			],		}); mw.hook('dev.qdmodal').add( => {			if (!didSetup) setup, didSetup = true;			modal.show({ title: "Vanguard Outreach message sender", content: $ui, buttons: [{ text: "Submit", handler, }, {					text: "Cancel", handler: modal.close, }],			});		});	}	$('.page-tools-module .rail-module__list').append($('', { id: "t-voutreach", html: $('', {			click, 			text: "Vanguard Outreach", 			title: "Send a Vanguard Outreach message to this user", 			css: {				cursor: 'pointer',			},		}), })); }

var inter = setInterval( => {	if ($('.page-tools-module .rail-module__list').length) clearInterval(inter), main; }, 0);