User:Novem Linguae/Scripts/EditRequestReadFAQ.js
Appearance
From Wikipedia, the free encyclopedia
(Redirected from User:Novem Linguae/Scripts/edit-request-read-faq.js)
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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Novem Linguae/Scripts/EditRequestReadFAQ.
// <nowiki> // Semi Edit Request - Read FAQ // Places a link below the {{edit semi-protected}} template that says "[Decline - Read FAQ]" // Clicking on it changes {{edit semi-protected|Username|answered=no}} to "answered=yes". // Then it posts the message "{{not done}} Hi, and welcome to Wikipedia. The edit you're requesting has been discussed multiple times, and is against current consensus. For more information, see the FAQ at the top of this page. Thank you! ~~~~" // Then it reloads the page. // Requested by [[User:Valereee]] // https://en.wikipedia.org/wiki/Wikipedia:User_scripts/Requests#Please_Read_the_FAQ // Test pages: // https://en.wikipedia.org/wiki/Category:Wikipedia_semi-protected_edit_requests // TODO: handle blank parentSectionName // TODO: handle sections with same name // TODO: handle COI edit requests // TODO: ping requester $(function(){ asyncfunctiongetWikicode(title){ // if page is deleted, return blank if(!mw.config.get('wgCurRevisionId')){ return''; } letwikicode=''; await$.ajax({ url:'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json', success:function(result){ wikicode=result.parse.wikitext; }, dataType:'json' }); returnwikicode; } functionescapeRegExp(string){ returnstring.replace(/[.*+?^${}()|[\]\\]/g,'\\$&');// $& means the whole matched string } functionmarkAnswered(wikicode,parentSectionName){ constregex=newRegExp('('+escapeRegExp(parentSectionName)+'.*?==.*?\\|answered=)no','s'); wikicode=wikicode.replace(regex,'1ドルyes'); returnwikicode; } functioninsertComment(wikicode,parentSectionName){ constregex=newRegExp('('+escapeRegExp(parentSectionName)+'.*?==.*?)(\n==)','s'); constcomment="\n:{{not done}} Hi, and welcome to Wikipedia. The edit you're requesting has been discussed multiple times, and is against current consensus. For more information, see the FAQ at the top of this page. Thank you! ~~~~"; letnewWikicode=wikicode.replace(regex,'1ドル'+comment+'2ドル'); constnoCommentInserted=(newWikicode===wikicode); if(noCommentInserted){ newWikicode=addWikitextAtEndOfPage(wikicode,comment); } returnnewWikicode; } functionaddWikitextAtEndOfPage(wikitext,textToAdd){ returnwikitext+textToAdd; } functionsubmitEdit(articleName,wikicode){ constsummary='Answer edit request (via [[User:Novem Linguae/Scripts/EditRequestReadFAQ.js|script]])'; editPage(articleName,wikicode,summary); } functiongetArticleName(){ returnmw.config.get('wgPageName'); } functiongetParentSectionName(declineElement){ // not chained, to make it easier to debug letelement=declineElement.prevAll('h2').first(); if(!element){ return''; } element=element.find('.mw-headline'); constvalue=element.html(); returnvalue; } // borrowed from [[Wikipedia:User scripts/Guide#Edit a page and other common actions]] functioneditPage(articleName,wikicode,summary){ $.ajax({ url:mw.util.wikiScript('api'), type:'POST', dataType:'json', data:{ format:'json', action:'edit', title:articleName, text:wikicode,// will replace entire page content summary:summary, token:mw.user.tokens.get('csrfToken') }, async:false }); } functiongetRandomInt(min,max){ returnMath.floor(Math.random()*(max-min+1)+min); } functionreloadPageAtSection(parentSectionName){ leturl=window.location.href; // strip off #abcd at the end url=url.replace(/#[^#]*$/,''); // fix bug where page won't hard refresh. add &rand=1234 to the end constrand=getRandomInt(1000,9999); url=url.replace(/&rand=\d{4}/,''); url=url.replace(/\?rand=\d{4}&/,'?'); url=url.replace(/\?rand=\d{4}$/,''); if(url.includes('?')){ url+='&rand='+rand; }else{ url+='?rand='+rand; } // add correct #abcd at the end url+='#'+encodeURIComponent(parentSectionName.replace(/ /g,'_')); // refresh window.location.href=url; } // don't run when not viewing articles constaction=mw.config.get('wgAction'); if(action!=='view'){ return; } // don't run when viewing diffs constisDiff=mw.config.get('wgDiffNewId'); if(isDiff){ return; } constisDeletedPage=(!mw.config.get('wgCurRevisionId')); if(isDeletedPage){ return; } // need all the class stuff, because #editsemiprotected will not work if there is more than one open edit request on the page. ID is supposed to be unique $('#editsemiprotected.editrequest[data-origlevel="semi"]').after('<a class="edit-request-read-faq" style="font-size: 80%;" href="javascript:void(0)">[Decline - Read FAQ]</a>'); $('.edit-request-read-faq').on('click',asyncfunction(){ constparentSectionName=getParentSectionName($(this)); constarticleName=getArticleName(); letwikicode=awaitgetWikicode(articleName); wikicode=markAnswered(wikicode,parentSectionName); wikicode=insertComment(wikicode,parentSectionName); submitEdit(articleName,wikicode); reloadPageAtSection(parentSectionName); }); }); // </nowiki>