User:Hamish/installer.js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/*
<nowiki>在common.js中添加一行 {{subst:iusc|User:Hamish/installer.js}} 以使用該工具。</nowiki>
可實現半自動安裝/管理使用者小工具。
*/
(function($, mw){
'use strict';
var wgPageName = mw.config.get('wgPageName');
var wgUserName = mw.config.get('wgUserName');
var wgNamespaceNumber = mw.config.get('wgNamespaceNumber');
var jsPage = false;
if (wgNamespaceNumber === 0){
return;
}
if ((wgPageName.lastIndexOf('.js') == wgPageName.length - 3) && (mw.config.get('wgAction') == 'view') && (wgNamespaceNumber > -1)) {
var fixedUn = wgUserName.replace(/ /g,'_');
if ((wgPageName != 'User:' + fixedUn + '/common.js') &&
(wgPageName != 'User:' + fixedUn + '/monobook.js') &&
(wgPageName != 'User:' + fixedUn + '/minerva.js') &&
(wgPageName != 'User:' + fixedUn + '/vector.js') &&
(wgPageName != 'User:' + fixedUn + '/modern.js') &&
(wgPageName != 'User:' + fixedUn + '/cologneblue.js') &&
(wgPageName != 'User:' + fixedUn + '/timeless.js') &&
(wgPageName != 'User:Hamish/installer.js')){
importStylesheet('User:Hamish/installer.css');
jsPage = true;
$('h1#firstHeading').append('<span style="font-size:0.8em;font-weight:bold;" id="' + wgPageName + '" class="scriptInstallerLink jsPage"></span>');
}
}
if ((($('span.scriptInstallerLink').length > 0) && (mw.config.get('wgAction') != 'submit') && (mw.config.get('wgAction') != 'edit')) || (wgPageName == 'User:Hamish/installer')){
if (jsPage !== true) {
importStylesheet('User:Hamish/installer.css');
}
if (wgPageName == 'User:Hamish/Installer') {
var homePage = true;
}
var pre = location.protocol + '//' + mw.config.get('wgPageContentLanguage') + '.' + mw.config.get('wgNoticeProject') + '.org' + '/w/index.php?title=';
var installerTitle = '您已經安裝了以下JavaScript程式<div class="titleBadge"><a href="' + pre + 'User:Hamish/installer.js' + '">安裝程式</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 = '必須手動安裝';
var regexObject = new RegExp( 'importScript\\(\'(.*)\'\\); \\/\\/Linkback: \\[\\[.*?\\]\\] 經由安裝程式安裝' , ["i"]);
var installedScripts = [];
$('div#contentSub').after('<div hidden="hidden" class="scriptInstaller"></div>');
var request4 = {
action:"query",
titles: "User:" + mw.config.get("wgUserName") + "/common.js",
prop: "revisions|info",
intoken: "edit",
rvprop: "content",
indexpageids: 1,
format: "xml"
};
$.get(mw.config.get("wgScriptPath")+"/api.php", request4, function(response4){
var lines = $(response4).find('rev').text().split('\n');
$.each(lines, function(index, value){
var match = regexObject.exec(value);
if (match !== null){
installedScripts.push(match[1]);
}
});
if (installedScripts.length < 1){
$('div.scriptInstaller').remove();
} else if ((wgPageName == 'Wikipedia:用戶工具') || (wgPageName == 'Wikipedia:小工具') ||
(wgPageName == 'Wikipedia:Play_store') || (jsPage === true) || (homePage === true)){
var toggleMessage = ((jsPage === true) || (homePage === true)) ? '檢視已安裝腳本列表' : '隱藏已安裝腳本列表';
$('.firstHeading').append(' <a style="font-weight:bold;font-size:10px" class="scriptinstallerTog" href="#bbx">' + toggleMessage + '</a>');
function setScriptInstallerToggle(){
$('.scriptinstallerTog').click(function(){
if ($('.scriptinstallerTog').html() == "檢視已安裝腳本列表"){
$('.scriptInstaller').fadeIn(500);
$('.scriptinstallerTog').html('隱藏已安裝腳本列表');
} else {
$('.scriptInstaller').fadeOut(200);
$('.scriptinstallerTog').html('檢視已安裝腳本列表');
}
});
}
if ((jsPage !== true) && (homePage !== true)){
setTimeout(function(){
$('.scriptInstaller').fadeIn(800, function(){ setScriptInstallerToggle(); });
}, 500);
} else {
setScriptInstallerToggle();
}
}
var installedList = '<ul style="list-style-type:none;">';
$.each(installedScripts, function(index, value){
$('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');
installedList = installedList + '<li>' +
'<a href="#installerLink" class="unInstallerLink">' + unInstallerLink + '</a>: ' +
'<a href="' + pre + value + '">' + decodeURIComponent(value) + '</a>' +
'</li>';
});
installedList = installedList + '</ul>';
$('.scriptInstaller').html('<div class="installerTitle">' + installerTitle + '</div>' +
'<div class="container1">' +
'<div class="installerMessage">' + installerMessage + '</div>' +
'<div class="uninstallList">' + installedList + '</div>' +
'</div>');
$('.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>正在解除安裝<span style="font-weight:normal;color:#003366;">' + path + '</span>...</div>');
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"
};
$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){
var content = $(response5).find('rev').text();
var newText = content.replace("\n" + "importScript('" + path + "'); //Linkback: [[" + path + "]] 經由安裝程式安裝", "");
var request6 = {
action : "edit",
title : "User:" + mw.config.get("wgUserName") + "/common.js",
text : newText,
summary : "[[User:Hamish/installer.js|JavaScript]]: 解除安裝 [[" + path + "]]",
token: mw.user.tokens.get("editToken")
};
$.post(mw.config.get("wgScriptPath")+"/api.php", request6, function(response6){
$('.arcProg').append('<div><span style="color:#00008C">操作成功!</span>重新整理中...</div>');
location.reload();
});
});
});
});
});
$('span.scriptInstallerLink').each(function(){
var path = $(this).attr('id');
path = path.replace(/.2F/g,'/').replace(/\_/g,' ');
if ((path.split(".").length - 1) > 1){
var parts = path.split('.');
path = parts.slice(0,-1).join('%') + '.' + parts.slice(-1);
}
if (((path.toLowerCase().substring(0, 5) == "user:") || (path.toLowerCase().substring(0,10) == 'wikipedia:')) && (path.lastIndexOf('.js') == path.length - 3)){
//var pipe = (jsPage == true) ? '' : ' | ';
$(this).html('<a href="#installerLink" class="installerLink">' + installerLink + '</a>')
.before(pipe);
$(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>正在安裝<span style="font-weight:normal;color:#003366;">' + path + '</span>...</div>');
var request1 = {
action:"edit",
title: "User:" + mw.config.get("wgUserName") + "/common.js",
appendtext: "\nimportScript('" + decodeURIComponent(path) + "'); //Linkback: [[" + decodeURIComponent(path) + "]] 經由安裝程式安裝",
summary: "[[User:Hamish/installer.js|JavaScript]]: 安裝 [[" + path + "]]",
token: mw.user.tokens.get("editToken")
};
$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
$('.arcProg').append('<div><span style="color:#00008C">操作成功!</span>重新整理中...</div>');
location.reload();
});
});
} else {
$(this).html(' | <span class="noauto">' + noauto + '</span>');
}
});
}
})(jQuery, mediaWiki);