Jump to content

User:Awesome Aasim/editrequestor.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Awesome Aasim (talk | contribs) at 01:19, 30 November 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
if (!EditRequestor) {
	var EditRequestor = {}
	mw.loader.using("jquery.ui", function() {
		$(document).ready(function() {
			EditRequestor.url = new URL(location);
			let url = EditRequestor.url;
			if (mw.config.get('wgAction') == 'edit' && url.searchParams.get('section') == 'new' && url.searchParams.get('preload') == 'Template:Submit an edit request/preload') {
				EditRequestor.requestPage = '';
				EditRequestor.requestType = '';
				var preloadArray = EditRequestor.url.searchParams.getAll('preloadparams[]');
				for (var item of preloadArray) {
					switch (item) {
						case 'edit fully-protected': EditRequestor.requestType = item;
						break;
						case 'edit interface-protected': EditRequestor.requestType = item;
						break;
						case 'edit template-protected': EditRequestor.requestType = item;
						break;
						case 'edit extended-protected': EditRequestor.requestType = item;
						break;
						case 'edit semi-protected': EditRequestor.requestType = item;
						break;
						default: EditRequestor.requestPage = item;
						break;
					}
				}
				$('<form id="editrequestor-form"></form>').append('<p>Please make the change that you unambiguously would like to implement.</p><textarea id="editrequestor-area" disabled></textarea><textarea name="reason" id="editrequestor-reason" disabled placeholder="Reason for change"></textarea>').dialog({
					'buttons': [{
						"text": "Submit",
						"click": async function() {
							// fetch diff
							let wikitext = $("#editrequestor-area").val();
							let reason =  $("#editrequestor-reason").val();
							let compareRes = await $.get(mw.config.get('wgScriptPath') + '/api.php', {
								'action': 'compare',
								'format': 'json',
								'fromtitle': EditRequestor.requestPage,
								'toslots': 'main',
								'totext-main': wikitext
							});
							if (compareRes.error) {
								alert(compareRes.error.info);
								return;
							}
							let diffHTML = compareRes.compare['*'];
							// place in edit request
							let editRequestWikitext = `
== ${url.searchParams.get("preloadtitle")} ==
{{${EditRequestor.requestType}|answered=no}}

${diffHTML}

${reason}

~~~~
`
							// save
							let saveResult = await (new mw.Api()).postWithEditToken(editRequestWikitext, {
								"section": "new",
								"summary": "Creating new edit request (EditRequestor)"
							})
							console.log(saveResult);
							if (saveResult.error) {
								alert(saveResult.error.info);
							} else {
								let nexturl = new URL(location.href);
								nexturl.searchParams.delete("action");
								location.href = nexturl;
							}
						}
					},
					{
						"text": "Preview",
						"click": async function() {
							// preview change
							let wikitext = $("#editrequestor-area").val();
							let parsedText = await (new mw.Api()).parse(wikitext, {"title": EditRequestor.requestPage})
							$(`<div id="editrequestor-preview"></div>`).html(parsedText).dialog({modal: true, width: (0.75 * window.innerWidth > 300) ? 0.75 * window.innerWidth : 300});
						}
					},
					{
						"text": "Show change",
						"click": async function() {
							// show diff of change
							let wikitext = $("#editrequestor-area").val();
							let compareRes = await $.get(mw.config.get('wgScriptPath') + '/api.php', {
								'action': 'compare',
								'format': 'json',
								'fromtitle': EditRequestor.requestPage,
								'toslots': 'main',
								'totext-main': wikitext
							});
							console.log(compareRes);
							$(`<div id="editrequestor-change"></div>`).html(compareRes.compare['*']).dialog({modal: true, width: (0.75 * window.innerWidth > 300) ? 0.75 * window.innerWidth : 300});
						}
					}
					],
					width: (0.50 * window.innerWidth > 300) ? 0.50 * window.innerWidth : 300,
					modal: true
				});
				$("#editrequestor-area").text("Loading...");
				$.get(mw.config.get('wgScriptPath') + '/api.php', {
					'action': 'parse',
					'format': 'json',
					'prop': 'wikitext',
					'page': EditRequestor.requestPage
				}).done(function(result) {
					if (result.error) {
						$("#editrequestor-area").text('Error: ' + result.error.info);
					} else {
						$("#editrequestor-area").text(result.parse.wikitext['*']);
						$("#editrequestor-area").prop("disabled", false);
						$("#editrequestor-reason").prop("disabled", false);
					}
				});
			}
		});
	});
}
// </nowiki>