Jump to content

User:Ost316/substLongComment.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
//Script to tag pages with {{long comment}}
// Adapted from https://en.wikipedia.org/wiki/Wikipedia:autoEd/complete.js and https://en.wikipedia.org/wiki/Wikipedia:autoEd/core.js

function addLongComment() { //Activates individual modules when "substLC" tab is clicked
	var $textbox = $( '#wpTextbox1' );
	var txt = $textbox.textSelection('getContents');
	txt = txt + "\n{{" + "subst:long comment}}";
	$textbox.textSelection('setContents', txt);
}

//Initiates substLC
function substLCExecute() {
 if(!document.getElementById('wpTextbox1')) return;

	 // copy wikEd ([[User:Cacycle/wikEd.js]]) frame to wpTextbox1 textarea
	 // for compatibility with WikiEd
	 if (typeof wikEdUseWikEd !== 'undefined') {
	   if (wikEdUseWikEd === true) {
		 WikEdUpdateTextarea();
	   }
	 }

	 //alert/return if substLCFunctions is not defined
	 if( typeof addLongComment === 'undefined' ) {
	  alert('addLongComment is undefined');
	  return;
	 }

	 addLongComment();
	 substLCEditSummary();

	 // copy wpTextbox1 textarea back to wikEd frame
	 // for compatibility with WikiEd
	 if (typeof wikEdUseWikEd !== 'undefined') {
	  if (wikEdUseWikEd === true) {
	   WikEdUpdateFrame();
	  }
	 }
}

//Adds Tag to edit summary textbox
function substLCEditSummary() {
	 var txt = document.forms.editform.wpSummary;
	 var tag;

	 if( typeof substLCTag === 'undefined' ) {
	  tag = '+{{' + 'subst:long comment}}';
	 } else {
	  tag = substLCTag;
	 }

	 // Is the tag blank?
	 if( tag.match(/[^\s]/) ) {
	  // Has it already been tagged?
	  if( txt.value.indexOf(tag) == -1 ) {
	   // Append a pipe if necessary
	   if( txt.value.match(/[^\*\/\s][^\/\s]?\s*$/) ) {
		txt.value += ' | ';
	   }
	   // Append our tag
	   txt.value += tag;
	  }
	 }

	 // Check 'This is a minor edit'
	 if( typeof substLCMinor === 'undefined' || substLCMinor ) {
	  document.forms.editform.wpMinoredit.checked = true;
	 }

	 // Click 'Show changes'
	 if( typeof substLCClick === 'undefined' || substLCClick ) {
	  document.forms.editform.wpDiff.click();
	 }
}

// Add "substLC" tab and associate with actions
// Make sure the document is ready and our dependencies are loaded
$.when(
	 $.ready,
	 mw.loader.using(['mediawiki.util'])
	).done(function () {
	 var $link;

	 //Execute substLC after call from "view mode"
	 if( mw.util.getParamValue('substLC') ) {
	  substLCExecute();
	 }

	 // Set default values for any unset variables
	 if( typeof substLCLinkHover === 'undefined' ) {
	  substLCLinkHover = "Run substLC";
	 }
	 if( typeof substLCLinkName === 'undefined' ) {
	  substLCLinkName = "substLC";
	 }
	 if( typeof substLCLinkLocation === 'undefined' ) {
	  substLCLinkLocation = "p-cactions";
	 }

	 // Add the "substLC" tab
	 if( document.getElementById('ca-edit') ) {
	  var url = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', substLC: 'true' });
	  $link = $(mw.util.addPortletLink(
	   substLCLinkLocation,
	   url,
	   substLCLinkName,
	   'ca-substLC',
	   substLCLinkHover,
	   '',
	   document.getElementById('ca-move')
	  ));
	  if( typeof document.forms.editform !== 'undefined' ) {
	   $link.on('click', function (e) {
		e.preventDefault();
		substLCExecute();
	   });
	  }
	 }
	}
);

//
// </nowiki>