Jump to content
Wikipedia The Free Encyclopedia

User:Enterprisey/orcp-helper.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.
This user script seems to have a documentation page at User:Enterprisey/orcp-helper.
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>
 (function($,mw){

 functionspecificElementId(username){
 varprefix="orcp-"+username.replace(/[^a-z0-9]/g,"_")+"-";
 return{
 "rating":prefix+"rating-field",
 "comment":prefix+"comment-field",
 "button":prefix+"rate-button",
 "placeholder":prefix+"placeholder",
 "cancel":prefix+"cancel-button"
 };
 }

 functionspecificElement(username){
 varids=specificElementId(username);

 // Object "mapping" taken from http://stackoverflow.com/a/14810722
 returnObject.keys(ids).reduce(function(previous,current){
 previous[current]=$("#"+ids[current]);
 returnprevious;
 },{});
 }

 functionmakeActionLink(username){
 return$("<a>").css("cursor","pointer").text("add rating").attr("href","#").on("click",function(e){
 console.log("Link for "+username+" was clicked.");
 buildPanel($(this),username);
 $(this).replaceWith("<span id='"+specificElementId(username).placeholder+"'>add rating</a>");
 returnfalse;
 });
 }

 /*
  * target is the "add request" link that was clicked to trigger this.
  */
 functionbuildPanel(target,username){
 varids=specificElementId(username),
 createSeparator=function(){return$("<span class='separator'>|</span>");};
 target.parent().parent().after(`
  <div class="orcp-rating">
  <table>
  <tr>
  <th><label for="${ids.rating}">Rating</label></th>
  <th><label for="${ids.comment}">Comment</label></th>
  <th>&nbsp;</th>
  </tr>
  <tr>
  <td>
  <input type="text" id="${ids.rating}" class="mw-ui-input mw-ui-input-inline" value="5" maxlength="3" style="width:3em" />
  <span style="margin-right: 6px"> / 10</span>
  </td>
  <td>
  <textarea id="${ids.comment}" class="mw-ui-input" rows="1" />
  </td>
  <td>
  <button id="${ids.button}" class="mw-ui-button mw-ui-constructive">Rate</button>
  <button id="${ids.cancel}" class="mw-ui-button mw-ui-destructive mw-ui-quiet">Cancel</button>
  </td>
  </tr>
  </table>
  </div>`);
 specificElement(username).rating.blur(function(){validate(username);});
 specificElement(username).comment.blur(function(){validate(username);});
 specificElement(username).button.click(function(){saveRating(username,$(this).parent());});
 specificElement(username).cancel.click(function(){

 // This statement walks back up the DOM to the .orcp-rating div
 $(this).parent().parent().parent().parent().parent().remove();
 specificElement(username).placeholder.replaceWith(makeActionLink(username));
 });
 }

 functionvalidate(username){
 varuserInput=specificElement(username).rating.val(),
 validRating=/^[0-9]+(\.[0-9]+)?$/.test(userInput)&&
 (parseInt(userInput,10)<=10);
 specificElement(username).rating.toggleClass("invalid",!validRating);
 specificElement(username).button.prop("disabled",!validRating);
 returnvalidRating;
 }

 // The argument "that" is the entire panel div
 functionsaveRating(username,that){
 if(!validate(username))return;
 $(specificElement(username).button).prop("disabled",true);
 $(specificElement(username).rating).prop("disabled",true);
 $(specificElement(username).comment).prop("disabled",true);
 varstatusElement=$("<span style='margin-left:10px' class='status'>Saving rating...</span>").appendTo(that),
 setStatus=function(status,callback){statusElement.fadeOut(function(){statusElement.html(status).fadeIn(callback);})};
 setStatus("Getting wikitext...");
 varwikitext;
 $.getJSON(
 mw.util.wikiScript('api'),
 {
 format:'json',
 action:'query',
 prop:'revisions',
 rvprop:'content',
 rvlimit:1,
 titles:"Wikipedia:Requests for adminship/Optional RfA candidate poll"
 }
 ).done(function(data){
 try{
 varpageId=Object.keys(data.query.pages)[0];
 wikitext=data.query.pages[pageId].revisions[0]['*'];
 setStatus("Got this page's wikitext, processing...");

 varheaderMatch=(newRegExp("(==\\s*"+username+"\\s*==)")).exec(wikitext);
 varsectionTextPlus=wikitext.slice(headerMatch.index+headerMatch[1].length);

 varsectionText=sectionTextPlus;
 if(sectionTextPlus.indexOf("==")>=0){
 sectionText=sectionText.substring(0,sectionText.indexOf("=="));
 }
 sectionText=sectionText.trim();

 // Make the new wikitext
 varrating=specificElement(username).rating.val();
 varnewLine="*'''"+rating+"/10''' - "+specificElement(username).comment.val()+" ~~~~";
 varnewWikitext=wikitext.replace(sectionText,sectionText+"\n"+newLine);

 setStatus("Wikitext processed. Saving...");
 (newmw.Api()).postWithEditToken({
 action:"edit",
 title:"Wikipedia:Requests for adminship/Optional RfA candidate poll",
 summary:rating+"/10 for "+username+" ([[User:Enterprisey/orcp-helper|orcp-helper.js]])",
 text:newWikitext
 }).done(function(data){
 if(data&&data.edit&&data.edit.result&&data.edit.result=='Success'){
 setStatus("Rating saved! (<a href='javascript:void(0)' class='reload'>Reload</a>)");
 $(":animated").promise().done(function(){
 that.children("span.status").children("a").click(function(){document.location.reload(true);});
 });
 specificElement(username).cancel.text("Done");
 }else{
 setStatus("While saving, the edit query returned an error. =(");
 }
 }).fail(function(){
 setStatus("While saving, the AJAX request failed.");
 });
 }catch(e){
 setStatus("While getting the wikitext, there was an error.");
 console.log("Content request error: "+e.message);
 console.log("Content request response: "+JSON.stringify(data));
 }
 }).fail(function(){
 setStatus("While getting the wikitext, there was an AJAX error.");
 });
 }

 // Function is called when dependencies are loaded and document is
 // ready.
 functiononReady(){
 $(`<style>
  .orcp-rating {
  box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
  padding: 5px;
  padding-left: 19px;
  display: inline-block;
  }

  .orcp-rating input {
  padding: 4px 4px 4px 8px;
  height: 35px;
  }

  .orcp-rating input.invalid {
  box-shadow: 0px 0px 0px 2px #D11D13 inset;
  border-color: #D11D13;
  }

  .orcp-rating textarea {
  height: auto;
  }

  .orcp-rating textarea.mw-ui-input {
  min-height: 0;
  }

  .orcp-rating th {
  text-align: left;
  }
  </style>`).appendTo("head");
 $("h2:not(:contains('Instructions')) .mw-editsection").each(function(index,element){
 varusername=$(this).parent().text().replace(/\[[\s\S]+\]/,"");

 // We shouldn't be able to rate ourselves
 if(mw.config.get("wgUserName")===username)return;

 // We shouldn't be able to rate people twice
 vartestLink=function(link){returnlink.href.indexOf(mw.config.get("wgUserName"))>=0;};
 varlinksInComments=$(this).parent().nextUntil("h2").filter("ul").children().children("a").toArray();
 if(linksInComments.some(testLink))return;
 $(this).children(".mw-editsection-bracket").last()
 .before(" | ")
 .before(makeActionLink(username));
 });
 }// End function onReady

 if(mw.config.get('wgPageName')==="Wikipedia:Requests_for_adminship/Optional_RfA_candidate_poll"){
 mw.loader.load("mediawiki.ui.input","text/css");
 mw.loader.using(["mediawiki.api","mediawiki.util"]).then(function(){
 $(document).ready(onReady);
 });
 }
 }(jQuery,mediaWiki));
 //</nowiki>

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