User:Yair rand/addcomment.js

$( 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 = $( "", { 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 = $( " ", { append: [							$previewBox = $( " " ), 							$textarea = $( " ", { "attr": { "placeholder": l.boxplaceholder }, "keyup": function { $sigerrorNode.toggle( /\~\~\~$/.test( this.value ) ); }							} ), 							$sigerrorNode = $( " ", { "text": l.sigerror, "class": "error", "css": { "display": "none" } } ), 							$( " ", {								"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 + " (Assisted)", 											token: mw.user.tokens.get('csrfToken')										}, function ( r ) {												location.reload;										}); }								}								} ), 							$( " ", {								"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[ "*" ] );									}); }							} ), 							$( " ", {								"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 ) );	} });