User:AttemptToCallNil/common.js

// /* globals $, mw */ // -- Patchver redirect maker -- function tryMakePatchverRedir(event, page_name, matches) { var api = new mw.Api; api.create(matches[0], {}, "#REDIRECT " + page_name + "") .done(function {			mw.notification.notify("Title with V created successfully.");		}).fail(function {			mw.notification.notify("Title with V creation failed!", {type: "error"});		}).always(function {			api.create(matches[1], {}, "#REDIRECT " + page_name + "")				.done(function { mw.notification.notify("Title without V created successfully."); }).fail(function { mw.notification.notify("Title without V creation failed!", {type: "error"}); });		});	event.preventDefault; }

// -- Skill redirect maker - function tryMakeSkillRedir(event, matches) { new mw.Api.get({		action: "query",		prop: "info",		titles: matches[1],		formatversion: 2	}).then(function(response) { skillRedir_processUnskilledPageResponse(matches, response) } ); event.preventDefault; }

function skillRedir_processUnskilledPageResponse(matches, response) { var page = response.query.pages[0]; if (page.missing) { skillRedir_tryCreateRedirect(matches); } else if (page.redirect) { new mw.Api.get({			action: "query",			prop: "redirects",			titles: matches[0],			rdlimit: "max",			formatversion: 2		}).then(function (response) { skillRedir_processSelfRedirQuery(matches, response) }); } else { mw.notification.notify("Skill-less page already exists and is not a redirect!", {type: "warn"}); } }

function skillRedir_tryCreateRedirect(matches) { new mw.Api.create(matches[1], {}, "#REDIRECT " + matches[0] + "") .done(function {			mw.notification.notify("Redirect created successfully.");		}).fail(function {			mw.notification.notify("Redirect creation failed!", {type: "error"});		}); }

function skillRedir_processSelfRedirQuery(matches, response) { var page = response.query.pages[0]; if (page.redirects) { var missing = true; console.log(matches); for (var i = 0; i < page.redirects.length; i++) { console.log(page.redirects[i]); if (page.redirects[i].title === matches[1]) { mw.notification.notify("Skill-less page already redirects where needed!"); missing = false; break; }		}		if (missing) { mw.notification.notify("Skill-less page already redirects somewhere else!", {type: "warn"}); }	} else { mw.notification.notify("Skill-less page already redirects somewhere else!", {type: "warn"}); } }

// -- Unbreak search links on "no article" pages --- $(function {	$('.noarticletext a[href*="Special:Search/"]')		.each(function(link) { var $this = $(this); var linkTarget = $(this).attr("href"); var match = linkTarget.match("^(.+?)Special:Search/(.+)$"); linkTarget = match[1] + "Special:Search?search=" + encodeURIComponent(match[2]).replaceAll("_", "+"); $(this).attr("href", linkTarget); }); });

// -- Link missing pages on Special:Search - $(function {	if (mw.config.get("wgPageName") !== "Special:Search") return;	if (new URL(document.URL).searchParams.get("query") === null) return;	new mw.Api.get({ "action": "query", "titles": $(".unified-search__input__query").val, "prop": "info", "inprop": "url", "formatversion": "2" }).done(function(response) { var pageData = response.query.pages[0]; if (pageData.invalid) { console.log("LinkSearchedPage: Not a valid title, not doing anything."); } else if (!pageData.missing) { console.log("LinkSearchedPage: Page exists, should appear in the search results. Not doing anything."); } else { $(".unified-search__layout__main p:first-child").append(" | Create the page \"" + pageData.title + "\" on this wiki."); }	}).fail(function { console.error("LinkSearchedPage: Could not query page information."); });	});

// -- Main loader -- mw.loader.using("mediawiki.notification", function { $(function { var page_name = mw.config.get("wgPageName").replace(/_/g, " "); var matches = page_name.match(/^(.*?) \(Skill\)$/); if (matches) { var redir_link = mw.util.addPortletLink("p-cactions", "#", "Redirect skill", "#t-redirskill"); $(redir_link).on('click', function(event) { tryMakeSkillRedir(event, matches) }); }	var matches2 = page_name.match(/v(\d+\.\d+\.\d+\.\d+)/); if (matches2) { var redir_link2 = mw.util.addPortletLink("p-cactions", "#", "Redirect vernums", "#t-redirvers"); $(redir_link2).on('click', function(event) { tryMakePatchverRedir(event, page_name, matches2) }); } }); }); //