मीडियाविकि:Gadget-XFDcloser.js

विकिपिडिया, एक स्वतन्त्र विश्वकोशबाट
सूचना: सम्पादनहरू प्रकाशित गरेपछि, परिवर्तनहरू हेर्नको लागि तपाईंले आफ्नो ब्राउजरको क्यासलाई बाइपास गर्नुपर्छ। गुगल क्रोम, फायरफक्स, माइक्रोसफ्ट एजसफारी: Shift कुञ्जी थिचिराखेर रिलोड उपकरणपट्टी बटनमा थिच्नुहोस्।
/*  _______________________________________________________________________________
 * |                                                                               |
 * |                     === WARNING: GLOBAL GADGET FILE ===                       |
 * |                   Changes to this page affect many users.                     |
 * |  Please discuss changes on the talk page or on [[WT:Gadget]] before editing.  |
 * |_______________________________________________________________________________|
 * 
 * Built from source code at GitHub repository [https://github.com/wikimedia-gadgets/xfdcloser]
 * All changes should be made in the repository, otherwise they will be lost.
 * 
 * To update this script from github, you must have a local repository set up. Then
 * follow the instructions at [https://github.com/wikimedia-gadgets/xfdcloser/blob/master/README.md]
 * 
 * XFDcloser: close deletion discussions at XfD venues, including actions to implement closes; see [[WP:XFDC]]
 * Author: Evad37
 * Licencing and attribution: [[WP:XFDC#Licencing and attribution]]
 */
/* jshint esversion: 5, esnext:false, laxbreak: true, undef: true, eqnull: true, maxerr: 3000 */
/* globals console, document, window, $, mw, OO */
/* <nowiki> */
(function(){
	// Check if on a non-existent article/file/portal (to unlink backlinks)
	var isNonexistentPage = mw.config.get("wgArticleId") === 0;
	var isUnlinkableNamespace = [
		0, // Article
		6, // File
		100 // Portal 
	].includes(mw.config.get("wgNamespaceNumber"));
	var isUnlinkablePage = isNonexistentPage && isUnlinkableNamespace;
	
	// Check for edit, history, diff, or oldid mode
	if ( !isUnlinkablePage && /(?:\?|&)(?:action|diff|oldid)=/.test(window.location.href) ) {
		return;
	}

	// XFDcloser options
	var options;
	try {
		options = JSON.parse(mw.user.options.get("userjs-xfdc")) || {};
	} catch(e) {
		options = {};
	}

	// Enable/disable beta mode by visiting [[Wikipedia:XFDcloser/beta]]
	if ( mw.config.get("wgPageName") === "Wikipedia:XFDcloser/beta" ) {
		var onButtonClick = function() {
			var $button = $(this);
			var originalButtonText = $button.text();
			$(this).prop('disabled', true).text("Working, please wait...");
			options.beta = !options.beta;
			mw.loader.using("mediawiki.api")
				.then(function() {
					var API = new mw.Api( { ajax: { headers: { 
						"Api-User-Agent": "XFDcloser/loader ( https://en.wikipedia.org/wiki/WP:XFDC )"
					} } } );
					return API.postWithToken("csrf", {
						"action": "options",
						"format": "json",
						"formatversion": "2",
						"optionname": "userjs-xfdc",
						"optionvalue": JSON.stringify(options)
					});
				})
				.then(
					function() {
						mw.notify("Reloading...", {
							title: "XFDcloser beta succesfully " + (options.beta ? "enabled" : "disabled"),
							type: "success"
						});
						window.setTimeout(function() { window.location.reload(); }, 5 * 1000);
					},
					function(code, error) {
						var errorDetails = (code || "Unknown") + " error" +
							(error && error.info ? ": " + error.info : "");
						mw.notify(errorDetails, {
							"title": "XFDcloser beta mode could not be " + (options.beta ? "enabled" : "disabled") + ":",
							"type": "error"
						});
						// Reset state of options.beta and button 
						options.beta = !options.beta;
						$button.prop('disabled', false).text(originalButtonText);
					}
				);
		};
		$.ready.then(function() {
			$("#mw-content-text").prepend(
				$("<p>")
					.css({
						"padding": "1em",
						"background": options.beta ? "#b0ccdd" : "#dbafaf",
						"border-radius": "1em",
						"text-align": "center",
						"font-size": "110%"
					})
					.append(
						"Beta testing is currently ",
						$("<strong>").text(options.beta ? "enabled" : "disabled"),
						".",
						$("<button>")
							.css({
								"font-size":"105%",
								"margin-left":"1em"
							})
							.text(options.beta ? "Disable XFDcloser beta" : "Enable XFDcloser beta")
							.click(onButtonClick)
					)
			);
		});
	}

	// Check if on an XfD page
	var xfdpage_regex = /(मेटाउनु_पर्ने_लेखहरू\/|Miscellany_for_deletion|User:Cyberbot_I\/AfD's_requiring_attention|Wikipedia:WikiProject_Deletion_sorting\/(?!(Flat|Compact)$)|(Categories|Files|Templates|Redirects)_for_discussion(?!\/(Working|Holding_cell|Speedy)))(?!\/?(?:Administrator_instructions|Common_outcomes)$)/;
	var isXfdPage = xfdpage_regex.test(mw.config.get("wgPageName"));

	// While version 4 is in beta, only load for unlinkable pages if beta testing is enabled 
	var shouldLoadScript = options.beta ? (isXfdPage || isUnlinkablePage) : (isXfdPage);
	if ( !shouldLoadScript ) {
		return;
	}
	mw.loader.load( options && options.beta ? "ext.gadget.XFDcloser-core-beta" : "ext.gadget.XFDcloser-core");
})();
/* </nowiki> */