User:DannyS712/Quick closer.js
Jump to navigation
Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
This script seems to have a documentation page at User:DannyS712/Quick closer and an accompanying .css page at User:DannyS712/Quick closer.css. |
//<nowiki>
var Closed_config = {
name: '[[User:DannyS712/Quick closer|Quick closer]]',
version: '1.0.2',
debug: false
};
var Closed_ending = ' (' + Closed_config.name + ' v.' + Closed_config.version + ')';
/* HELPER FUNCTIONS */
function dc_radio(i) {
if (i == 1) {
$('#DC-e').prop('disabled', false);
$('#DC-e').val("");
}
if (i == 2) {
$('#DC-e').prop('disabled', true);
$('#DC-e').val('Not available');
}
}
/* MAIN FUNCTIONs */
function dc_init(){
if ((($('#ca-addsection').length > 0) ||
(mw.config.get('wgPageName') == 'Wikisource:Copyright_discussions') ||
(mw.config.get('wgPageName') == 'Wikisource:Proposed_deletions')) &&
(mw.config.get('wgAction') == 'view')) {
dc_run();
}
}
function dc_run() {
$('span.mw-headline').each(function(index, value){
var editSectionUrl = $(this).parent().find('.mw-editsection a:first').attr('href');
var sectionReg = /§ion=(.*)/;
var sectionRaw = sectionReg.exec(editSectionUrl);
if ((sectionRaw !== null) && (sectionRaw[1].indexOf('T') < 0)){
var section = parseInt(sectionRaw[1]);
var sectionLevel = false;
switch ($(this).parent().prop("tagName")) {
case 'H2':
sectionLevel = 'h2';
break;
case 'H3':
sectionLevel = 'h3';
break;
case 'H4':
sectionLevel = 'h4';
break;
default:
break;
}
if (sectionLevel && sectionLevel !== false) {
$(this).parent(sectionLevel).append(' <div style="font-size:.6em;font-weight:bold;float:right"> | <a id="' + section +
'" class="closeLink">' + 'Close' + '</a></div>');
$(this).parent(sectionLevel).find('a.closeLink').click(function(){
$('body').prepend('<div id="DC-modal">'+
'<div id="DC-interface">'+
'<h4 id="DC-interface-header"></h4>'+
'<hr>'+
'<div id="DC-interface-content"></div>'+
'<hr>'+
'<div id="DC-interface-footer"></div>'+
'</div>'+
'</div>');
$("#DC-interface-header, #DC-interface-content, #DC-interface-footer").empty();
$("#DC-interface-header").text("Quick closer");
$("#DC-interface-content").append(
$('<div class="DC-bottom">').append(
$('<label>').text('Closing comment:'),
$('<textarea>').css({'resize':'none'}).attr({'name':'DC-com','id':'DC-com','rows':'17','cols':'30'})
),
$('<div class="DC-bottom">').append(
$('<label>').text('Edit summary ("Closing discussion" if left blank): '),
$('<input>').attr({'type':'text','name':'DC-es','id':'DC-es'})
),
$('<div class="DC-bottom">').attr('id','DC-prevt'),
$('<div class="DC-bottom">').attr('id','DC-prevdiv'),
$('<button>').attr('id', 'DC-closeprev').text('Close preview').hide()
);
$("#DC-interface-footer").append(
$('<button>').attr({'name':'DC-done', 'id':'DC-done'}).text('Close discussion'),
$('<button>').attr({'name':'DC-prev', 'id':'DC-prev'}).text('Preview'),
$('<button>').attr('id', 'DC-cancel').text('Cancel'),
$('<label>').attr('id','DC-loading').text(' Loading preview...').hide()
);
document.getElementById("DC-modal").addEventListener( "keydown", function ( key ) {
if( key.ctrlKey && ( key.keyCode == 10 || key.keyCode == 13 ) ) {
document.getElementById("DC-done").click();
}
else if ( key.keyCode == 27 ){
document.getElementById("DC-cancel").click();
}
});
$('#DC-cancel').click(function() {
$('#DC-modal').remove();
});
$('#DC-prev').click(function() {
$('#DC-loading').show();
var comment = $('#DC-com').val();
var request = {
action:"query",
titles: mw.config.get('wgPageName'),
rvsection: section,
prop: "revisions|info",
intoken: "edit",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.get(mw.config.get("wgScriptPath")+"/api.php", request, function(response){
var contentSection = '\r\r' + $(response).find('rev').text();
var discussiontext = contentSection.substring(contentSection.substring(3,contentSection.length).indexOf('\n')+3, contentSection.length);
var title = contentSection.substring(2, contentSection.length - discussiontext.length);
var wikitext = title + '\n{{closed|' +comment+ '|text=\n'+discussiontext+'\n}}\n{{section resolved|1=~~~~}}';
var sectiontitle = title.replace('==', '').replace('==', '').replace('\n', '').replace('/r','').trim();
var sectiontitle2 = sectiontitle.replace(/ /g, '_');
jQuery.ajax({
url:mw.util.wikiScript('api'),
dataType:'json',
type:'POST',
data:{
format:'json',
action:'parse',
pst:1,
text:wikitext,
title:mw.config.get('wgPageName'),
prop:'text'
}
}).done(function(r) {
if(!r.parse || !r.parse.text || !r.parse.text['*']){
alert('Error previewing.');
}
$('#DC-loading').hide();
$('#DC-prevt').html('<hr>Preview:<br>');
$('#DC-prevdiv').html(r.parse.text['*']+'<br />').css({'display':'block','background-color':'white'});
$('#DC-closeprev').show();
$('#DC-closeprev').click(function() {
$('#DC-prevt').empty();
$('#DC-prevdiv').empty().css({'display':'','background-color':'','border-color':'',"border-width":"", "border-style":""});
$('#DC-closeprev').hide();
});
});
});
});
$('#DC-done').click(function() {
var editsummary = $('#DC-es').val();
if (editsummary === "") {
editsummary = "Closing discussion";
}
var comment = $('#DC-com').val();
$('#DC-done').remove();
$('#DC-prev').remove();
$('#DC-cancel').remove();
$('#DC-interface-content').empty();
$("#DC-interface-content").text('Retrieving information...');
var request = {
action:"query",
titles: mw.config.get('wgPageName'),
rvsection: section,
prop: "revisions|info",
intoken: "edit",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$("#DC-interface-content").append('<b>Done!</b><br>Closing discussion...');
$.get(mw.config.get("wgScriptPath")+"/api.php", request, function(response){
var contentSection = '\r\r' + $(response).find('rev').text();
var discussiontext = contentSection.substring(contentSection.substring(3,contentSection.length).indexOf('\n')+3, contentSection.length);
var title = contentSection.substring(2, contentSection.length - discussiontext.length);
var wikitext = title + '\n{{closed|' +comment+ '|text=\n'+discussiontext+'\n}}\n{{section resolved|1=~~~~}}';
var sectiontitle = title.replace(/=/g, '').replace('\n', '').replace('/r','').trim();
var sectiontitle2 = sectiontitle.replace(/ /g, '_');
var request2 = {
action:"edit",
section: section,
title: mw.config.get('wgPageName'),
text: wikitext,
summary: '/* ' + sectiontitle + ' */ ' + editsummary + Closed_ending,
token: mw.user.tokens.get("csrfToken")
};
var willClose = true;
if ( contentSection.toLowerCase().includes( '{{closed' ) ) {
willClose = confirm('The discussion may have already been closed. Continue?');
}
if (!willClose) {
$("#DC-interface-content").append('<b>Aborted by user</b><br><b>Finished!</b>');
$('#DC-interface-footer').append(
$('<button>').attr('id', 'DC-close').text('Done')
);
$('#DC-close').click(function() {
$('#DC-modal').remove();
window.location.href = 'https://en.wikisource.org/wiki/'+mw.config.get('wgPageName')+'#'+sectiontitle2;
window.location.reload();
});
}
else {
$.when(
$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response2){
}).done(function() {
$("#DC-interface-content").append('<b>Done!</b><br><b>Finished!</b>');
$('#DC-interface-footer').append(
$('<button>').attr('id', 'DC-close').text('Done')
);
$('#DC-close').click(function() {
$('#DC-modal').remove();
window.location.href = 'https://en.wikisource.org/wiki/'+mw.config.get('wgPageName')+'#'+sectiontitle2;
window.location.reload();
});
}));
}
});
});
});
}
}
});
}
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Title'], function() {
mw.loader.load('//en.wikisource.org/w/index.php?title=User:DannyS712/closed.css&action=raw&ctype=text/css', 'text/css'); // Import stylesheet
dc_init();
});
//</nowiki>