User:Xover/Quick closer.js

var Closed_config = { name: 'Quick closer', version: '1.0.1', 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'); } }

mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Title', 'mediawiki.RegExp'], function {	console.log("QC: in mw.loader.using");	mw.loader.load('//en.wikisource.org/w/index.php?title=User:DannyS712/closed.css&action=raw&ctype=text/css', 'text/css'); // Import stylesheet	console.log("QC: loaded CSS");

// Main script logic. function dc_run { console.log("QC: in dc_run"); $('span.mw-headline').each(function(index, value){     var editSectionUrl = $(this).parent.find('.mw-editsection a:first').attr('href');      var sectionReg = /&section=(.*)/;      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(' | ' + 'Close' + ' ');          $(this).parent(sectionLevel).find('a.closeLink').click(function{ $('body').prepend(' '+             ' '+              ' '+              ' '+              ' '+              ' '+              ' '+              ' '+              ' '); $("#DC-interface-header, #DC-interface-content, #DC-interface-footer").empty; $("#DC-interface-header").text("Quick closer"); $("#DC-interface-content").append(             $(' ').append( $(' ').text('Closing comment:'), $(' ').css({'resize':'none'}).attr({'name':'DC-com','id':'DC-com','rows':'17','cols':'30'}) ),             $(' ').append( $(' ').text('Edit summary ("Closing discussion" if left blank): '), $(' ').attr({'type':'text','name':'DC-es','id':'DC-es'}) ),             $(' ').attr('id','DC-prevt'),              $(' ').attr('id','DC-prevdiv'),              $(' ').attr('id', 'DC-closeprev').text('Close preview').hide            ); $("#DC-interface-footer").append(             $(' ').attr({'name':'DC-done', 'id':'DC-done'}).text('Close discussion'),              $(' ').attr({'name':'DC-prev', 'id':'DC-prev'}).text('Preview'),              $(' ').attr('id', 'DC-cancel').text('Cancel'),              $(' ').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\n'; 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(' Preview: ');                  $('#DC-prevdiv').html(r.parse.text['*']+' ').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('Done! 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\n';                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('Aborted by user Finished!'); $('#DC-interface-footer').append(                   $(' ').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('Done! Finished!'); $('#DC-interface-footer').append(                     $(' ').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;                    }); }));               }              });            });          });        }      }    });  }

console.log("Waiting for DOM to coalesce…") $(function {   // Only do anything in view mode.    if (mw.config.get('wgAction') == 'view') {      // And only be active on pages where it's useful.      var wgPageName = mw.config.get('wgPageName');      if (wgPageName == 'Wikisource:Copyright_discussions' || wgPageName == 'Wikisource:Proposed_deletions') {    		console.log("QC: about to dc_run");    		dc_run;      }    }  }); // END: $(document).ready }); // END: mw.loader.using