Jump to content

User:Awesome Aasim/SRGL.js

From Meta, a Wikimedia project coordination wiki

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.
/**
 * Script to make reporting to steward requests easier.
 * NOTICE:
 * This script /must/ be installed on your global.js file to work properly. This is due to this thing called CORS ([[w:Cross-origin resource sharing]]) which prevents requests from being made cross-site without first initial verification.
 * If you can help me with making this script work with CORS, please let me know on my talk page.
 **/
if (!SRG) {
	var SRG = {};
	SRG.API_PAGE = "//meta.wikimedia.org/w/api.php";
	SRG.user = (function() {
		if (mw.config.get("wgPageName").toLowerCase().includes("Special:Contributions".toLowerCase()) || mw.config.get("wgPageName").toLowerCase().includes("Special:CentralAuth".toLowerCase())) {
			if (mw.config.get("wgTitle").includes("/")) {
				return mw.config.get("wgTitle").split("/")[1];
			} else {
				return (new URL(window.location)).searchParams.get("target");
			}
		} else if (mw.config.get("wgNamespaceNumber") == 2 || mw.config.get("wgNamespaceNumber") == 3) {
			return mw.config.get("wgTitle").split("/")[0];
		}
	})();
	mw.loader.using("jquery.ui", function() {
		$(document).ready(function() {
//			if ((mw.config.get("wgNamespaceNumber") == 2 || mw.config.get("wgNamespaceNumber") == 3 || mw.config.get("wgPageName").toLowerCase().includes("Special:Contributions".toLowerCase() + "/") || mw.config.get("wgPageName").toLowerCase().includes("Special:CentralAuth".toLowerCase() + "/")) && !mw.config.get("wgTitle").split("/")[0].match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g) && !(mw.config.get("wgTitle").split("/")[1] ? mw.config.get("wgTitle").split("/")[1].match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g) : (new URL(document.location)).searchParams.get("target"))) {
			if (SRG.user && !SRG.user.match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g)) {
				switch (mw.config.get("skin")) {
					case "timeless": mw.util.addPortletLink(
						'p-userpagetools',
						'//meta.wikimedia.org/wiki/Steward_requests/Global',
						'GL',
						'srgl',
						'Request global lock',	
						'l'
					);
					break;
					case "cologneblue": mw.util.addPortletLink(
						'p-pageoptions',
						'//meta.wikimedia.org/wiki/Steward_requests/Global',
						'GL',
						'srgl',
						'Request global lock',	
						'l'
					);
					break;
					default: 
					mw.util.addPortletLink(
						'p-tb',
						'//meta.wikimedia.org/wiki/Steward_requests/Global',
						'GL',
						'srgl',
						'Request global lock',	
						'l'
					);
				}
				SRG.requestGlobalLock = function() {
					if (SRG.user == mw.config.get('wgUserName')) {
						alert("You can't request locking of your own account.");
						return;
					}
					// CORS breaks my code
					if (mw.config.get("wgSiteName") != "Meta") {
						var destination = new URL(window.location);
						destination.hostname = "meta.wikimedia.org";
						destination.searchParams.set("globallock", "1");
						destination.searchParams.set("from", window.location.href);
						location.href = destination.href;
						return;
					}
					$('<form id="srgl-box"></form>').append('<p><b>Please verify that you are logged in at <a href="https://meta.wikimedia.org/">meta.wikimedia.org</a> or this tool will not work properly.</b></p><p>Please specify a reason why this user should be <a href="https://meta.wikimedia.org/wiki/Global_locks">globally locked</a>.</p><input name="reason" type="text" id="srgl-reason" placeholder="Reason"><br><input name="hideuser" type="checkbox" id="srgl-hideuser"><label for="srgl-hideuser">Hide this user on the Steward Requests page.</label><div id="srgl-preview"></div></form>').dialog({
						buttons: [
							{
								/*class: "mw-ui-button mw-ui-progressive",*/
								text: 'Report', 
								click: function() {
									$("#srgl-reason").prop("disabled", true);
									$(".ui-button").prop("disabled", true).addClass("ui-state-disabled");
									var section = 2;
									$.get(SRG.API_PAGE, {
										"action": "parse",
										"prop": "text",
										"format": "json",
										"page": "Steward requests/Global"
									}).done(function(html) {
										$(html.parse.text["*"]).find('.mw-headline').each(function(i) {
										    if ($(this).text() == "Requests for global (un)lock and (un)hiding") {
										        section = i + 1;
										    }
										});
										var text = "\n=== Global lock for ";
										if ($("#srgl-hideuser").prop("checked")) {
											text += "[username hidden]";
										} else {
											text += SRG.user;
										}
										text += " ===\n{{status}}\n* {{LockHide|" + SRG.user;
										if ($("#srgl-hideuser").prop("checked")) {
											text += "|hidename=yes";
										}
										text += "}}\n";
										if ($("#srgl-reason").val().length < 3) {
											mw.notify($("<span class=\"error\">" + "Please specify a reason." + "</span>"));
											$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
											$("#srgl-reason").prop("disabled", false);
											return;
										}
										text += $("#srgl-reason").val() + " ~~" + "~~";
										/**
										 * @Crossorigin
										 **/
										$.get(SRG.API_PAGE, {
											"action": "query",
											"meta": "tokens",
											"type": "csrf",
											"format": "json"
										}).done(function(token) {
											if (token.error) {
												mw.notify($("<span class=\"error\">" + tokens.error.info + "</span>"));
												$( "#srgl-box" ).dialog( 'destroy' ).remove();
											} else {
												/**
												 * @Crossorigin
												 **/
												$.post(SRG.API_PAGE, {
													"action": "edit",
													"summary": "Reporting user via [[m:User:Awesome Aasim/SRGL|SRGL]] gadget",
													"section": section,
													"appendtext": text,
													"title": "Steward requests/Global",
													"format": "json",
													"token": token.query.tokens.csrftoken
												}).done(function(editresult) {
													if (editresult.error) {
														mw.notify($("<span class=\"error\">" + editresult.error.info + "</span>"));
													} else {
														mw.notify("User successfully reported.");
														location.href = (new URL(window.location)).searchParams.get("from");
													}
													$( "#srgl-box" ).dialog( 'destroy' ).remove();
												}).fail(function(editresult) {
													// debugger;
													console.log(editresult);
													mw.notify($("<span class=\"error\">" + editresult + "</span>"));
						
													$( "#srgl-box" ).dialog( 'destroy' ).remove();
												});
											}
										}).fail(function(token) {
											// debugger;
											console.log(token);
											mw.notify($("<span class=\"error\">" + token + "</span>"));
											$( "#srgl-box" ).dialog( 'destroy' ).remove();
										});
									}).fail(function(html) {
										
									})
								}
							},
							{
								/*class: "mw-ui-button",*/
								text: 'Preview',
								click: function() {
									$("#srgl-reason").prop("disabled", true);
									$(".ui-button").prop("disabled", true).addClass("ui-state-disabled");
									var text = "\n=== Global lock for ";
									if ($("#srgl-hideuser").prop("checked")) {
										text += "[username hidden]";
									} else {
										text += SRG.user;
									}
									text += " ===\n{{status}}\n* {{LockHide|" + SRG.user;
									if ($("#srgl-hideuser").prop("checked")) {
										text += "|hidename=yes";
									}
									text += "}}\n";
									if ($("#srgl-reason").val().length < 3) {
										mw.notify($("<span class=\"error\">" + "Please specify a reason." + "</span>"));
										$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
										$("#srgl-reason").prop("disabled", false);
										return;
									}
									text += $("#srgl-reason").val() + " ~~" + "~~";
									/**
									 * @Crossorigin
									 **/
									$.get(SRG.API_PAGE, {
										"action": "parse",
										"format": "json",
										"prop": "text",
										"text": text,
										"title": "Steward requests/Global",
										"pst": true
									}).done(function(preview) {
										if (preview.error) { 
											mw.notify($("<span class=\"error\">" + preview.error.info + "</span>"));
										} else {
											$("#srgl-preview").html(preview.parse.text["*"]);
											$("#srgl-preview").find(".mw-editsection").css("display", "none");
										}
										$("#srgl-reason").prop("disabled", false);
										$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
									}).fail(function(preview) {
										// debugger;
										mw.notify($("<span class=\"error\">" + preview + "</span>"));
										console.log(preview);
										$("#srgl-reason").prop("disabled", false);
										$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
									});
								}
							},
							{
								/*class: "mw-ui-button",*/
								text: 'Cancel',
								click: function() {
									if ((new URL(document.location)).searchParams.get("globallock")) {
										$("#srgl-reason").prop("disabled", true);
										$(".ui-button").prop("disabled", true).addClass("ui-state-disabled");
										window.history.back();
									} else {
										$( this ).dialog( 'destroy' ).remove();
									}
								}
							}
						],
						closeOnEscape: false,
						modal: true,
						title: "Request global lock",
						width: 600
					});
				}
				$("#srgl").click(function(e) {
					e.preventDefault();
					SRG.requestGlobalLock();
				});
				if ((new URL(document.location)).searchParams.get("globallock")) {
					SRG.requestGlobalLock();
				}
			}
		})
	});
}