User:Yair rand/addcomment.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

$( document ).ready( function ( $ ) {
	if( true && $( "#ca-addsection" ).length ) {
		var translate = {
			en: {
				button: "comment",
				buttontitle: "Add a comment to this section",
				loading: "Loading...",
				boxplaceholder: "Add a comment.",
				sigerror: "It is not necessary to sign your post with four tildes. The signature is added automatically.",
				submitbutton: "Add comment",
				blanktexterror: "Please enter a comment.",
				previewbutton: "Preview",
				cancelbutton: "Cancel"
			}
		};
		
		var l = translate[ mw.config.get( "wgUserLanguage" ) ] || translate.en,
			isHeader = /^H\d$/i;
		
		var $a = $( "<a>", {
			text: l.button,
			css: { "cursor": "pointer" },
			attr: { "title": l.buttontitle },
			on: { "click" : function () {
				var $this = $( this ),
					$p = $( this.parentNode ), 
					m = /\?title=([^&]+)&action=edit&section=T?-?([0-9]+)/.exec( 
						$p.find( "a" ).attr( "href" )
					),
					n = +m[ 2 ],
					api = mw.config.get( "wgScriptPath" ) + "/api.php";
				if( $this.data( "comment_button_in_use" ) != "1" && n > 0 ) {
					$this.css( { color: "#AAA", cursor: "default" } ).data( "comment_button_in_use", "1" );
					for( var e = this.parentNode.parentNode, t = e, hLevel = e.nodeName, depth = ""; e.nextSibling && ( !isHeader.test( e.nextSibling.nodeName ) || e.nextSibling.nodeName > hLevel ); e = e.nextSibling ) {
						if( e.nodeName == "P" || e.nodeName == "UL" || e.nodeName == "DL" ) {
							t = e;
						}
					}
					if( t.nodeName !== "P" ) {
						for( ; t; ) {
							depth += ( { "UL" : "*", "DL" : ":" } )[ t.nodeName ] || "";
							t = t.lastChild;
							for( ; t && t.nodeType === 3; ) {
								t = t.previousSibling;
							}
						}
					}
					depth += ":";
					function wText() {
						return ( "\n" + $textarea.val() + " ~~" + "~~" ).replace(/\n(?! )/g, "\n" + depth );
					}
					var $textarea, $previewBox, $sigerrorNode, 
						$div = $( "<div>", { append: [
							$previewBox = $( "<div>" ), 
							$textarea = $( "<textarea>", {
								"attr": { "placeholder": l.boxplaceholder }, 
								"keyup": function () { 
									$sigerrorNode.toggle( /\~\~\~$/.test( this.value ) );
								}
							} ), 
							$sigerrorNode = $( "<div>", {
								"text": l.sigerror,
								"class": "error",
								"css": { "display": "none" }
							} ), 
							$( "<button>", {
								"text": l.submitbutton, 
								"click": function () {
									var val = $textarea.val();
									if( val === "" ) {
										alert( l.blanktexterror ); 
									} else {
										$div.text( l.loading );
										document.body.style.cursor = "wait";
										$.post( api, {
											format: "json", 
											action: "edit", 
											title: m[ 1 ], 
											section: n, 
											appendtext: wText(), 
											summary: val + " ([[User:Yair rand/addcomment.js|Assisted]])", 
											token: mw.user.tokens.get('csrfToken')
										}, function ( r ) {
												location.reload();
										});
									}
								}	
							} ), 
							$( "<button>", {
								"text": l.previewbutton, 
								"click": function () {
									$previewBox.text( l.loading );
									$.get( api, {
										format: "json", 
										action: "parse",
										prop: "text", 
										text: wText(), 
										title: m[ 1 ], 
										pst: true
									}, function ( r ) {
										$previewBox.html( r.parse.text[ "*" ] );
									});
								}
							} ), 
							$( "<button>", {
								"text": l.cancelbutton, 
								"click": function () {
									$div.remove();
									$this.css( { color: "", cursor: "pointer" } ).data( "comment_button_in_use", null );
								}
							} )
						] } ).insertBefore( e.nextSibling );
					$textarea.focus();
				}
			}}
		});
		$( ".mw-editsection a" ).after( document.createTextNode( " | " ), $a.clone( true ) );
	}
});