Jump to content
Wikipedia The Free Encyclopedia

User:Writ Keeper/Scripts/teahouseResponder.js

From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Writ Keeper/Scripts/teahouseResponder.
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.
 //Most of this code comes from User:Equazcion (from User:Equazcion/TeahouseRespond.js); nearly all credit should go to him.

 importStylesheet('User:Equazcion/TeahouseRespond.css');
 if(mw.config.get("wgPageName")=='Wikipedia:Teahouse/Questions'){
 //<nowiki>
 (function($,mw){

 $(function(){
 mw.loader.using(['jquery.ui','mediawiki.api'],function(){

 functionaddResponse(section,headline,text){
 varwikitext='\n\n'+text;

 $('.wp-teahouse-respond-form').hide();

 $('.wp-teahouse-respond')
 .find('.selflink')
 .empty()
 .addClass('mw-ajax-loader');

 varapi=newmw.Api();

 api.getEditToken().done(function(token)
 {
 api.post(
 {
 'action':'edit',
 'section':section,
 'title':'Wikipedia:Teahouse/Questions',
 'appendtext':wikitext,
 'summary':'/* '+headline+' */'+' response',
 'token':token
 }.done(function(){window.location.reload();});
 );
 });
 }
 functionmakeIndentList(parent,k)
 {
 varchildArray=parent.nextUntil("h2","dl").find("dd");
 varmaxIndents=0;
 childArray.each(function(index)
 {
 varcurIndents=this.parents("dl").length;
 if(curIndents>maxIndents)
 {
 maxIndents=curIndents;
 }
 });
 varstring="<select id='indentLevel"+k+"'><option value=0>Start a new thread (no indent)</option><option value=1>Reply to asker (1 indent)</option>";
 varindex=2;
 for(index;index<maxIndents;index++)
 {
 string+="<option value="+index+">Reply to responder #"+(index-1)+"("+index+" indents)</option>";
 }
 string+="</select>";
 returnstring;
 }

 varheaders=$('h2:gt(0)').find('.editsection');
 vark;
 varheader;
 headers.each(function(k){

 $(this).prepend("<span style='font-size: 2em;color:#3fb6ff;text-shadow: #444 0.1em 0.1em 0.1em;'>«</span>&nbsp;&nbsp;&nbsp;<a style='font-weight:bold;cursor:pointer; color:#7d7b75;' href='#' class='wp-teahouse-respond' id='wp-teahouse-respond-"+k+"'>Respond to this discussion</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");

 varformCode='<div class="wp-teahouse-respond-form" id="wp-teahouse-respond-form-'+k+'">'+makeIndentList(this,k)+'<p>You can type your response below.</p><textarea rows="10" cols="20" id="wp-th-respond-text-'+k+'"></textarea><p><span class="wp-th-sign-hint">On Wikipedia, you should sign all of your posts by ending them with four tildes (~~~~)</span><a href="#" class="wp-th-respond" id="wp-th-respond-'+k+'">Add my response</a></p></div>';

 varrLink=$('#wp-teahouse-respond-'+k);
 rLink.after(formCode);

 varrForm=$('#wp-teahouse-respond-form-'+k);
 varrText=$('#wp-th-respond-text-'+k);
 varrIndent=$('#indentLevel'+k);
 varrButton=$('#wp-th-respond-'+k);
 varheadline=$(this).parents('h2').find('span.mw-headline').html();

 // Prevent flash
 rForm.css('left','-10000px');

 // Set up position
 setTimeout(function(){
 varpos=rLink.position();
 varhCenter=($(window).width()/2);
 rForm.css('top',pos.top+20+'px');
 rForm.css('left',(hCenter-(rForm.width())/2)+'px');
 rForm.hide();
 },0);

 rButton.button({disabled:true}).click(function(e){
 e.preventDefault();

 vartext=rText.val();
 varindex;
 for(index=0;index<maxIndent;index++)
 {
 text=":"+text;
 }
 addResponse(k+1,headline,text);
 }).end()

 rText.keypress(function(e){
 var$textbox=$(this);
 setTimeout(function(){
 if((/~~~~\s*$/).test($textbox.val())){
 rButton.button('option','disabled',false);
 }else{
 rButton.button('option','disabled',true);
 }
 },0);
 });

 rLink.click(function(e){
 rForm.toggle('fast');
 e.cancelBubble=true;// for IE
 if(e.stopPropagation){
 e.stopPropagation();
 e.preventDefault();
 }
 });

 $(document).click(function(e){
 var$target=$(e.target);
 if(!$target.is('.wp-teahouse-respond-form *')&&
 !$target.is('.wp-teahouse-respond *')
 ){
 $('.wp-teahouse-respond-form').fadeOut();
 }
 });

 $(document).keydown(function(e){
 if(e.keyCode==27){// ESC
 rForm.fadeOut();
 }
 });//after loop
 });
 });
 });
 })(jQuery,mediaWiki);
 //</nowiki>
 }

AltStyle によって変換されたページ (->オリジナル) /