Jump to content

User:Awesome Aasim/quickcreate.js

From Wikipedia, the free encyclopedia
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.
if (!quickCreate) {
	if (typeof relAfterCreate == 'undefined') var relAfterCreate = true;
	var quickCreate = function(page, index) {
		var wikitext = prompt("Wiki text to add to " + page + ":");
		if (wikitext == null || wikitext == undefined) {
			return;
		}
		$.get(mw.config.get("wgScriptPath") + "/api.php", {
			action: "query",
			format: "json",
			meta: "tokens",
			type: "csrf"
		}).then(function(result, status) {
			if (status != 'success') {
				alert("Connection failed.");
			} else {
				$.post(mw.config.get("wgScriptPath") + "/api.php", {
					action: "edit",
					format: "json",
					createonly: 1,
					title: page,
					token: result.query.tokens.csrftoken,
					text: wikitext,
					summary: "[[wikia:w:c:dev:User:Awesome Aasim/quickcreate|Quick create]] " + page
				}).then(function(result, status) {
					if (status != 'success') {
						alert("Connection failed.");
					} else {
						if (result.error) {
							alert(result.error.info);
						} else {
							$("#quickcreate-" + index).remove();
							if (relAfterCreate) location.reload();
						}
					}
				})
			}
		});
	};
	$(document).ready(function() {
		$(".new").each(function(i) {
			try {
				$(this).after('<span id="quickcreate-' + i + '"><sup>[<a href="javascript:void(0)">QC<span class="quickcreate-text" style="display:none;">' + $(this).attr("title").substring(0, $(this).attr("title").length - 22) + '</span></a>]</sup></span>');
				$("#quickcreate-" + i).find("a").click(function() {
					quickCreate($(this).find(".quickcreate-text").text(), i);
				});
			} catch (Error) {
				
			}
		});
	});
}