МедиаУики:Scripts.js/ScriptInstaller.js

Уикипедия — ашық энциклопедиясынан алынған мәлімет
Навигацияға өту Іздеуге өту

Ескерту: Сақтағаннан кейін өзгерістерді көру үшін браузеріңіздің бүркемесін (кэшін) тазарту керек болуы мүмкін.

  • Firefox / Safari: Қайта жүктеуді нұқығанда  Shift пернесін басып тұрыңыз немесе Ctrl+F5 не Ctrl+ Shift+R екеуінің біреуін басыңыз
    (Mac — +R)
  • Google Chrome: Ctrl+ Shift+R басыңыз (Mac — + Shift+R)
  • Internet Explorer: Жаңарту батырмасын нұқығанда Ctrl пернесін басып тұрыңыз немесе Ctrl+F5 басыңыз, не F5 пернесін басыңыз
  • Opera: Құралдар → Бапталымдар дегеннен бүркемесін тазарту керек.
// Source code: [[:en:User:Equazcion/ScriptInstaller.js]]
// Never do anything in article space
if (mw.config.get('wgNamespaceNumber') != 0){ 
	
	// Handle .js pages
	if ((mw.config.get('wgPageName').lastIndexOf('.js') == mw.config.get('wgPageName').length - 3) && (mw.config.get('wgAction') == 'view') && (mw.config.get('wgNamespaceNumber') > -1)) {
		
		// Exclude users' own common.js and skin.js pages
		var fixedUn = mw.config.get('wgUserName').replace(/ /g,'_');
		if ((mw.config.get('wgPageName') != 'Қатысушы:' + fixedUn + '/common.js') && 
			(mw.config.get('wgPageName') != 'Қатысушы:' + fixedUn + '/monobook.js') &&
			(mw.config.get('wgPageName') != 'Қатысушы:' + fixedUn + '/vector.js') &&
			(mw.config.get('wgPageName') != 'Қатысушы:' + fixedUn + '/modern.js') &&
			(mw.config.get('wgPageName') != 'Қатысушы:' + fixedUn + '/cologneblue.js') &&
			(mw.config.get('wgPageName') != 'МедиаУики:Scripts.js/ScriptInstaller.js')){
				importStylesheet('МедиаУики:Stylesheets-ScriptInstaller.css');
				
				// Set a flag so the rest of the script knows we're on a .js page
				var jsPage = true;
				
				// Append the install link to the header, along with a "scriptInstallerLink" class for use by the rest of the script
				$('h1#firstHeading').append('<span style="font-size:0.8em;font-weight:bold;" id="' + mw.config.get('wgPageName') + '" class="scriptInstallerLink jsPage"></span>');
		}
	}
	
	// If script links are found on the page, start the music. Detection is based on span tags with "scriptInstallerLink" class, which are added by {{userscript}}.
	// Also run if we're on User:Equazcion/scriptInstaller, where we'll simply provide access to the installed script list and uninstall links
	if ((($('span.scriptInstallerLink').length > 0) && (mw.config.get('wgAction') != 'submit') && (mw.config.get('wgAction') != 'edit')) || (mw.config.get('wgPageName') == 'User:Equazcion/ScriptInstaller')){
		
		if (jsPage != true) importStylesheet('MediaWiki:Stylesheets-ScriptInstaller.css'); // Stylesheet already fetched above on .js pages
		if (mw.config.get('wgPageName') == 'User:Equazcion/ScriptInstaller') var homePage = true; // Set a flag if we're running the limited User:Equazcion/ScriptInstaller functions
		
		// Set URL prefix
		var pre = location.protocol + '//' + wgPageContentLanguage + '.' + wgNoticeProject + '.org' + '/w/index.php?title=';
		
		// Set interface text
		var installerTitle = 'Сізде қазір төмендегідей скрипттер орнатылған <div class="titleBadge"><a href="' + pre + 'Уикипедия:Скрипттер' + '">Скрипттер</a></div>';
		var installerMessage = 'Мұнда тек қана <span style="font-weight:bold">скрипт орнатқыш</span> қолданып орнатылған скрипттер көрсетілген. Скрипті онатпау үшін "Орнатпау" сілтемесін шертіңіз.'; 
		var pipe = '<span style="font-size:1.1em;"> | </span>';
		var installerLink = 'Орнату';
		var installed = 'Орнатылды';
		var unInstallerLink = 'орнатпау';
		var noauto = 'Қолмен орнатуды қажет етеді';
	
		// Set up the regex pattern for later
		var regexObject = new RegExp( 'importScript\\(\'(.*)\'\\); \\/\\/Кері сілтеме: \\[\\[.*?\\]\\] скрипт орнатқыш арқылы қосылған' , ["i"]);
		
		// Create array for installed script paths
		var installedScripts = [];
		
		// Append the box of installed scripts. Hide unless we're on a designated installation page
		$('div#contentSub').after('<div hidden="hidden" class="scriptInstaller"></div>');
		
		// Set parameters for common.js ajax request	
		var request4 = {
			action:"query", 
			titles: "User:" + mw.config.get("wgUserName") + "/common.js", 
			prop: "revisions|info", 
			intoken: "edit", 
			rvprop: "content",
			indexpageids: 1,
			format: "xml"
		};
		
		// Do common.js ajax request
		$.get(mw.config.get("wgScriptPath")+"/api.php", request4, function(response4){
			
			// Grab common.js content and split by lines 
			var lines = $(response4).find('rev').text().split('\n');
			
			// Use the regex to iterate through the lines, looking for the ones that ScriptInstaller added
			$.each(lines, function(index, value){
				var match = regexObject.exec(value);
				
				// Put the paths of the matches into the array of installed scripts
				if (match != null){
					installedScripts.push(match[1]);
				}
			});
			
			// If none were found, remove the installed script list box. Otherwise fade-in the box and set up the toggle link
			if (installedScripts.length < 1){
				$('div.scriptInstaller').remove();
			} else if ((mw.config.get('wgPageName') == 'Уикипедия:Скрипттер') || (jsPage == true) || (homePage == true)){
				
				// Insert script list toggle link
				var toggleMessage = ((jsPage == true) || (homePage == true)) ? 'Орнатылған скрипттер тізімін көрсету' : 'Орнатылған скрипттер тізімін жасыру';
				$('.firstHeading').append(' <a style="font-weight:bold;font-size:10px" class="scriptinstallerTog" href="#bbx">' + toggleMessage + '</a>');
				
				// The function to set the toggle link to
				function setScriptInstallerToggle(){
					$('.scriptinstallerTog').click(function(){
						if ($('.scriptinstallerTog').html() == "Орнатылған скрипттер тізімін көрсету"){
							$('.scriptInstaller').fadeIn(500);
							$('.scriptinstallerTog').html('Орнатылған скрипттер тізімін жасыру');
						} else {
							$('.scriptInstaller').fadeOut(200);
							$('.scriptinstallerTog').html('Орнатылған скрипттер тізімін көрсету');
						}
					});
				}
				
				// Set the toggle link function. Also fade the box in initially, if we're at the script listing page.
				if ((jsPage != true) && (homePage != true)){ 
					setTimeout(function(){
						$('.scriptInstaller').fadeIn(800, function(){ setScriptInstallerToggle(); });
					}, 500);
				} else {
					setScriptInstallerToggle();
				}					
			}
			
			// Start building the code for display of the installed list. Iterate through each installed script in the array
			var installedList = '<ul style="list-style-type:none;">';
			$.each(installedScripts, function(index, value){
				
				// For each script already installed, change the install links (into "installed" messages) that are on the current page (if any)
				$('span.scriptInstallerLink[id="' + encodeURIComponent(value).replace('%3A',':').replace(/\//g,'.2F').replace(/ /g,'_').replace(/%/g,'.').replace(/\.20/g,'_') + '"]')
					.attr('id','installed' + index)
					.addClass('installed')
					.html(installed)
					.css('font-weight','bold');
				if (jsPage == true)
					$('span.scriptInstallerLink[id="' + value.replace(/ /g,'_') + '"]')
						.attr('id','installed' + index)
						.addClass('installed')
						.html(installed)
						.css('font-weight','bold');
				
				// Add an HTML list element for each installed script, containing .js and uninstall links
				installedList = installedList + '<li>' + 
					'<a href="#installerLink" class="unInstallerLink">' + unInstallerLink + '</a>: ' +
					'<a href="' + pre + value + '">' + decodeURIComponent(value) + '</a>' + 
				'</li>'; 
			});
			
			// Cap off the list of installed scripts
			installedList = installedList + '</ul>';
			
			// Build and append the rest if the list box code, and insert our constructed list of installed scripts
			$('.scriptInstaller').html('<div class="installerTitle">' + installerTitle + '</div>' +	
			'<div class="container1">' + 
					'<div class="installerMessage">' + installerMessage + '</div>' + 
					'<div class="uninstallList">' + installedList + '</div>' + 
			'</div>');
			
			// Iterate through each line in the installed list and set the click function for their uninstall links
			$('.scriptInstaller li').each(function(){
				var path = $(this).find('a:last').html();
				$(this).find('a:first').click(function(){
					
					$('body').append('<div class="overlay" style="background-color:#000;opacity:.4;position:fixed;' + 
						'top:0;left:0;width:100%;height:100%;z-index:500;"></div>');					
						
					$('body').prepend('<div class="arcProg" style="font-weight:bold;box-shadow: 7px 7px 5px #000;font-size:0.9em;line-height:1.5em;' + 
						'z-index:501;opacity:1;position:fixed;width:50%;left:25%;top:30%;background:#F7F7F7;border:#222 ridge 1px;padding:20px;"></div>');
						
					$('.arcProg').append('<div>Uninstalling <span style="font-weight:normal;color:#003366;">' + path + '</span>...</div>');
					
					// Set parameters for the first uninstall ajax request that occurs when the uninstall link is clicked
					var request5 = {
						action:"query", 
						titles: "User:" + mw.config.get("wgUserName") + "/common.js", 
						prop: "revisions|info", 
						intoken: "edit", 
						rvprop: "content",
						indexpageids: 1,
						dataType: "xml",
						format: "xml"
					};
					
					// Send the request
					$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){
						
						//Grab common.js content and find/replace our line with nothing, thereby removing the script
						var content = $(response5).find('rev').text();
						var newText = content.replace("\n" + "importScript('" + path + "'); //Linkback: [[" + path + "]] Added by Script installer", "");
						
						// Set paraemeters for the ajax post that replaces common.js with our edited version
						var request6 = {
							action : "edit",
							title : "User:" + mw.config.get("wgUserName") + "/common.js", 
							text : newText,
							summary : "[[User:Equazcion/ScriptInstaller|Script Installer]]: Removed [[" + path + "]]",
							token: mw.user.tokens.get("csrfToken")
						};
						
						// Send the ajax post to save the new common.js, then reload the current page
						$.post(mw.config.get("wgScriptPath")+"/api.php", request6, function(response6){
							$('.arcProg').append('<div><span style="color:#00008C">Done!</span> Reloading...</div>');
							location.reload();
						});
					});
				});
			});
		});
		
		// Iterate through each templated (via {{userscript}}) script on the page
		$('span.scriptInstallerLink').each(function(){
			
			// Get the script path, which the template places in the span's ID
			var path = $(this).attr('id');
			path = path.replace(/.2F/g,'/').replace(/\_/g,' ');
			
			// If there's more than one dot left in the path, assume percent encoding was converted to dots. Leave the last dot for ".js"
			if ((path.split(".").length - 1) > 1){
				var parts = path.split('.');
	    		path = parts.slice(0,-1).join('%') + '.' + parts.slice(-1);
			}
			
			// If this path leads to a valid kk-wiki .js script in userspace or wikipedia space, add an install link
			if (((path.toLowerCase().substring(0, 9) == "қатысушы:") || (path.toLowerCase().substring(0,10) == 'уикипедия:')) && (path.lastIndexOf('.js') == path.length - 3)){
				//var pipe = (jsPage == true) ? '' : ' | ';
				$(this).html('<a href="#installerLink" class="installerLink">' + installerLink + '</a>')
					.before(pipe);
				
				// Set the click function for the install link
				$(this).find('a.installerLink').click(function(){
					
					$('body').append('<div class="overlay" style="background-color:#000;opacity:.4;position:fixed;' + 
						'top:0;left:0;width:100%;height:100%;z-index:500;"></div>');					
						
					$('body').prepend('<div class="arcProg" style="font-weight:bold;box-shadow: 7px 7px 5px #000;font-size:0.9em;line-height:1.5em;' + 
						'z-index:501;opacity:1;position:fixed;width:50%;left:25%;top:30%;background:#F7F7F7;border:#222 ridge 1px;padding:20px;"></div>');
						
					$('.arcProg').append('<div>Installing <span style="font-weight:normal;color:#003366;">' + path + '</span>...</div>');
					
					// Set ajax parameters for the ajax post that occurs when the install link is clicked 
					var request1 = {
						action:"edit", 
						title: "User:" + mw.config.get("wgUserName") + "/common.js", 
						appendtext: "\nimportScript('" + decodeURIComponent(path) + "'); //Linkback: [[" + decodeURIComponent(path) + "]] Added by Script installer", 
						summary: " [[" + path + "]] скриптісін орнатты", 
						token: mw.user.tokens.get("csrfToken")
					};
					
					// Send the ajax post, which appends our new importScript line to common.js, then reload the current page
					$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
						$('.arcProg').append('<div><span style="color:#00008C">Бітті!</span> Қайта оқылып жатыр...</div>');
					    location.reload();
					});
				});	
			} else {
				// If this is not a valid path to an en-wiki .js script in user or wikipedia space, add a "must install manually" message
				$(this).html(' | <span class="noauto">' + noauto + '</span>');
			}
		});
	}
}