User:Equazcion/ScriptInstaller.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Equazcion/ScriptInstaller and an accompanying .css page at User:Equazcion/ScriptInstaller.css. |
if (($('span.scriptInstallerLink').length > 0) && (wgAction != 'submit') && (wgAction != 'edit')){
// Set interface text
var installerTitle = 'You currently have the following scripts automatically installed';
var installerMessage = 'To uninstall a script, click its "Uninstall" link.';
var installerLink = 'Install';
var unInstallerLink = 'Uninstall';
var noauto = 'Must be installed manually';
// Get the stylesheet
importStylesheet('User:Equazcion/ScriptInstaller.css');
// Set up the regex pattern for later
var regex1='(importScript)';
var regex2='(\\(.*\\))';
var regex3='; \\/\\/Linkback: \\[.*?\\]\\] Added by Script installer';
var regexObject = new RegExp(regex1+regex2+regex3,["i"]);
// Create array for installed script paths
var installedScripts = [];
// Set URL prefix
var pre = location.protocol + '//' + wgPageContentLanguage + '.' + wgNoticeProject + '.org' + '/w/index.php?title=';
// Append the box of installed scripts. Hide unless we're on a designated installation page
$('div#contentSub').after('<div hidden="hidden" class="scriptInstaller"></div>');
if ((wgPageName == 'Wikipedia:WikiProject_User_scripts/Scripts') || (wgPageName == 'Wikipedia:WikiProject_User_scripts') || (wgPageName == 'User:Equazcion/sandbox1')){
setTimeout(function(){$('.scriptInstaller').fadeIn(800);}, 500);
}
// 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,
dataType: "xml",
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 content = $(response4).find('rev').text();
var lines = content.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[2].replace(/\'/g,'').replace(/\(/g,'').replace(/\)/g,''));
}
});
// If none were found, remove the installed script list box
if (installedScripts.length < 1) $('div.scriptInstaller').remove();
// 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="' + value.replace(/\//g,'.2F').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 + '">' + 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(){
// 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]]: Uninstalled [[" + path + "]]",
token: mw.user.tokens.get("editToken")
};
// 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){
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 this path leads to a valid en-wiki .js script in userspace or wikipedia space, add an install link
if (((path.toLowerCase().substring(0, 5) == "user:") || (path.toLowerCase().substring(0,10) == 'wikipedia:')) && (path.lastIndexOf('.js') == path.length - 3)){
$(this).html(' | <a href="#installerLink" class="installerLink">' + installerLink + '</a>');
// Set the click function for the install link
$(this).find('a.installerLink').click(function(){
// 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('" + path + "'); //Linkback: [[" + path + "]] Added by Script installer",
summary: "[[User:Equazcion/ScriptInstaller|Script installer]]: Installed [[" + path + "]]",
token: mw.user.tokens.get("editToken")
};
// 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){
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>');
}
});
}