User:PiRSquared17/ICE.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
if (typeof ICEReps == "undefined") var ICEReps = [[/(\s)(t|T)he [tT]he(\s)/g, "$1$2he$3"]]; // default
// The following is taken directly from [[en:User:TheFearow/simpleajax.js]]
function simpleAJAXEdit(page, newtext, summary, minor, watch, startsum, endsum, refresh, callback){
xmlhttp = sajax_init_object();
xmlhttp.query = {
'title': page,
'action': 'submit'
}
if(startsum){
document.getElementById('contentSub').innerHTML = startsum;
}
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true);
xmlhttp.onload = function() {
form = this.responseXML.getElementById( 'editform' );
var text = form.wpTextbox1.value;
var postData = {
'wpMinoredit': minor,
'wpWatchthis': watch,
'wpStarttime': form.wpStarttime.value,
'wpEdittime': form.wpEdittime.value,
'wpAutoSummary': form.wpAutoSummary.value,
'wpEditToken': form.wpEditToken.value,
'wpSummary': summary,
'wpTextbox1': newtext
};
var xmlhttp = sajax_init_object();
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'POST' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( this.query ), true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.onload = function() {
if(endsum){
document.getElementById('contentSub').innerHTML = endsum;
}else{
alert('Edit Complete');
}
if(refresh){
window.location.reload();
}
if(callback){
setTimeout(callback, 100);
}
};
xmlhttp.send( QueryString.create( postData ) );
}
xmlhttp.send( null );
}
function simpleAJAXAppend(page, newsuffix, summary, minor, watch, startsum, endsum, refresh, callback){
xmlhttp = sajax_init_object();
xmlhttp.query = {
'title': page,
'action': 'submit'
}
if(startsum){
document.getElementById('contentSub').innerHTML = startsum;
}
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true);
xmlhttp.onload = function() {
form = this.responseXML.getElementById( 'editform' );
var text = form.wpTextbox1.value;
var postData = {
'wpMinoredit': minor,
'wpWatchthis': watch,
'wpStarttime': form.wpStarttime.value,
'wpEdittime': form.wpEdittime.value,
'wpAutoSummary': form.wpAutoSummary.value,
'wpEditToken': form.wpEditToken.value,
'wpSummary': summary,
'wpTextbox1': text+"\n"+newsuffix
};
var xmlhttp = sajax_init_object();
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'POST' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( this.query ), true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.onload = function() {
if(endsum){
document.getElementById('contentSub').innerHTML = endsum;
}else{
alert('Edit Complete');
}
if(refresh){
window.location.reload();
}
if(callback){
setTimeout(callback, 100);
}
};
xmlhttp.send( QueryString.create( postData ) );
}
xmlhttp.send( null );
}
function simpleAJAXPrepend(page, newprefix, summary, minor, watch, startsum, endsum, refresh, callback){
xmlhttp = sajax_init_object();
xmlhttp.query = {
'title': page,
'action': 'submit'
}
if(startsum){
document.getElementById('contentSub').innerHTML = startsum;
}
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true);
xmlhttp.onload = function() {
form = this.responseXML.getElementById( 'editform' );
var text = form.wpTextbox1.value;
var postData = {
'wpMinoredit': minor,
'wpWatchthis': watch,
'wpStarttime': form.wpStarttime.value,
'wpEdittime': form.wpEdittime.value,
'wpAutoSummary': form.wpAutoSummary.value,
'wpEditToken': form.wpEditToken.value,
'wpSummary': summary,
'wpTextbox1': newprefix+"\n\n"+text
};
var xmlhttp = sajax_init_object();
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'POST' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( this.query ), true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.onload = function() {
if(endsum){
document.getElementById('contentSub').innerHTML = endsum;
}else{
alert('Edit Complete');
}
if(refresh){
window.location.reload();
}
if(callback){
setTimeout(callback, 100);
}
};
xmlhttp.send( QueryString.create( postData ) );
}
xmlhttp.send( null );
}
function simpleAJAXAppendPrepend(page, newprefix, newsuffix, summary, minor, watch, startsum, endsum, refresh, callback){
xmlhttp = sajax_init_object();
xmlhttp.query = {
'title': page,
'action': 'submit'
}
if(startsum){
document.getElementById('contentSub').innerHTML = startsum;
}
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true);
xmlhttp.onload = function() {
form = this.responseXML.getElementById( 'editform' );
var text = form.wpTextbox1.value;
var postData = {
'wpMinoredit': minor,
'wpWatchthis': watch,
'wpStarttime': form.wpStarttime.value,
'wpEdittime': form.wpEdittime.value,
'wpAutoSummary': form.wpAutoSummary.value,
'wpEditToken': form.wpEditToken.value,
'wpSummary': summary,
'wpTextbox1': newprefix+"\n\n"+text+"\n"+newsuffix
};
var xmlhttp = sajax_init_object();
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open( 'POST' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( this.query ), true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.onload = function() {
if(endsum){
document.getElementById('contentSub').innerHTML = endsum;
}else{
alert('Edit Complete');
}
if(refresh){
window.location.reload();
}
if(callback){
setTimeout(callback, 100);
}
};
xmlhttp.send( QueryString.create( postData ) );
}
xmlhttp.send( null );
}
var qCatPageNames = [];
var apis = [{}];
var api;
if (wgNamespaceNumber != 14) { // if not cat
api = sajax_init_object();
api.open('GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + wgPageName + '&action=raw', true);
api.onreadystatechange = show_result;
api.send(null);
} else {
for (var i = 0; i < document.getElementById("mw-pages").getElementsByTagName("li").length; i++) {
qCatPageNames[i] = document.getElementById("mw-pages").getElementsByTagName("li")[i].getElementsByTagName("a")[0].href.replace(/http:\/\/simple\.wikipedia\.org\/wiki\/(.*)/, "$1");
apis[i] = sajax_init_object();
apis[i].open('GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + qCatPageNames[i] + '&action=raw', true);
apis[i].onreadystatechange = show_result;
apis[i].send(null);
}
}
// handle response
function show_result(_api) {
if(_api.readyState==4) {
if(_api.status==200) {
return _api.responseText;
} else {
alert('The query returned an error.');
}
}
}
var ICE = { // ICE
addlilink: function(node, href, text, id, tooltip, accesskey) {
// the code below is mostly copied from mw.util.addPortletLink()
var link = document.createElement( "a" );
link.appendChild( document.createTextNode( text ) );
link.href = href;
var item = document.createElement( "li" );
item.appendChild( link );
if ( id ) item.id = id;
if ( accesskey ) {
link.setAttribute( "accesskey", accesskey );
tooltip += " ["+accesskey+"]";
}
if ( tooltip ) {
link.setAttribute( "title", tooltip );
}
updateTooltipAccessKeys( new Array( link ) );
node.appendChild( item );
return item;
},
tab: function(q) {
if (q == 'wkfy') document.editform.wpTextbox1.value = "{"+"{wikify}}\n\n" + document.editform.wpTextbox1.value;
if (q == 'cmplx') document.editform.wpTextbox1.value = "{"+"{complex}}\n\n" + document.editform.wpTextbox1.value;
if (q == '0refs') document.editform.wpTextbox1.value = "{"+"{nosources}}\n\n" + document.editform.wpTextbox1.value;
if (q == '+top') document.editform.wpTextbox1.value = prompt('Append to top:') + document.editform.wpTextbox1.value;
if (q == '+bttm') document.editform.wpTextbox1.value = document.editform.wpTextbox1.value + prompt('Append:');
if (confirm('Save?')) document.editform.submit();
},
addJSTab: function(name, id, title, q) {
return mw.util.addPortletLink('p-cactions', "javascript:ICE.tab(\'"+name+"\')", name, id, title, q);
}
};
function makeReplacements(){
if (wgNamespaceNumber == 14) { // if cat
var edSum = "Making text replacements to all pages in Category:"+wgTitle+"; "+mySumICE;
for (var i = 0; i < apis.length; i++) {
var outstring = show_result(apis[i]);
outstring = outstring.replace(ICEReps[i][0], ICEReps[i][1]);
simpleAJAXEdit(qCatPageNames[i], outstring, edSum + '([[WP:ICE-JS|ICE]])', true, false);
}
} else {
var outstring = show_result(api);
var edSum = "Making text replacements; " + mySumICE;
for (var i = 0; i < ICEReps.length; i++) {
outstring = outstring.replace(ICEReps[i][0], ICEReps[i][1]);
}
simpleAJAXEdit(wgPageName, outstring, edSum + '([[WP:ICE-JS|ICE]])', true, false);
}
}
function renameCategory(newName){
if (wgNamespaceNumber == 14) {
var oldCat = new RegExp("Category:"+wgTitle, "i");
var newRep = "Category:" + newName;
var edSum = "Renaming category \'" + wgTitle + "\' to \'" + newName + "\'";
for (var i = 0; i < apis.length; i++) {
var outstring = show_result(apis[i]);
outstring = outstring.replace(oldCat, newRep);
simpleAJAXEdit(qCatPageNames[i], outstring, edSum + '([[WP:ICE-JS|ICE]])', true, false);
}
}
}
function doCat(){
var pro = "Enter new name for Category (without 'Category:' prefix) [under-tested feature]";
var newNameCat = prompt(pro);
var aborted = "Aborted! (ICE)";
if (newNameCat == null) return (alert(aborted));
newNameCat.replace(/^Category:/i, "");
var notover = confirm("New name (careful! ICE is not shift key or typo 'smart'!): " + "Category:" + newNameCat + "\n Are you sure you want to continue?");
if (!notover) return (alert(aborted));
return renameCategory(newNameCat);
}
addOnloadHook(
function() {
mw.util.addPortletLink( 'p-cactions', "javascript:makeReplacements()", "rep", "ca-icerep", "Replace", "");
if (wgNamespaceNumber == 14) mw.util.addPortletLink( 'p-cactions', "javascript:doCat()", "rename", "ca-icecat", "Rename category", "");
}
);