Jump to content

User:He7d3r/Tools/ScoredRevisions.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// [[File:User:He7d3r/Tools/ScoredRevisions.js]] (workaround for [[phab:T35355]])
/**
 * Highlight revisions by their scores
 *
 * @author: Helder (https://github.com/he7d3r)
 * @license: CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0/>
 */
// <nowiki>
(function(e,i){"use strict";function r(){var i,r,n,a,t,o;if(arguments[0][0].error)return e.log.error(arguments[0][0].error),void 0;for(i=Object.keys(arguments[0][0][c.wgDBname].scores)[0],o=[],t=[],r=0;s.length>r;r++)a=arguments[r][0][c.wgDBname].scores[i],a&&!a.error&&a[s[r]]&&!a[s[r]].error&&a[s[r]].score&&(n=a[s[r]].score.probability.true,t.push((100*n).toFixed(0)+"% "+s[r]),o.push(n>=h.high?"sr-"+s[r]+"-high":n>=h.medium?"sr-"+s[r]+"-medium":n>=h.low?"sr-"+s[r]+"-low":"sr-"+s[r]+"-none"));d[i].addClass(o.join(" ")).attr("title","Scores: "+t.join("; "))}function n(){var r=i.Deferred(),n={},a={},t=c.wgCanonicalSpecialPageName==="Watchlist"||c.wgCanonicalSpecialPageName==="Recentchanges"||c.wgCanonicalSpecialPageName==="Recentchangeslinked",o=t?".mw-changeslist":c.wgCanonicalSpecialPageName==="Contributions"?".mw-contributions-list":"#pagehistory",s=e.user.options.get("usenewrc")==1&&t?"tr":"li",l=c.wgCanonicalSpecialPageName==="Contributions"||c.wgAction==="history"?"a.mw-changeslist-date":"a";return i(".unpatrolled").length&&!c.ScoredRevisionsEnableForPatrolledRevs,c.wgIsArticle&&c.wgAction==="view"?(d[c.wgCurRevisionId]=i("#ca-history a"),r.resolve([c.wgCurRevisionId]).promise()):(i(o).find(s).filter(function(){var e=i(this);return e.hasClass("wikibase-edit")?!1:!0}).each(function(){var r,t,o=i(this);o.find(l).each(function(){var n=i(this).attr("href");return r=e.util.getParamValue("diff",n),(r==="prev"||c.wgCanonicalSpecialPageName==="Contributions"||c.wgAction==="history")&&(r=e.util.getParamValue("oldid",n)),r&&/^([1-9]\d*)$/.test(r)?!1:(t||(t=e.util.getParamValue("curid",n)),void 0)}),r?(d[r]=o,n[r]=!0):t&&t!=="0"&&(a[t]=o)}),i.isEmptyObject(a)?r.resolve(Object.keys(n)):i.getJSON(e.util.wikiScript("api"),{format:"json",action:"query",prop:"revisions",rvprop:"ids",pageids:Object.keys(a).join("|")}).done(function(e){e&&e.query&&e.query.pages&&i.each(e.query.pages,function(e,i){var r=i.revisions[0].revid;d[r]||(d[r]=a[e],n[r]=!0)})}).always(function(){r.resolve(Object.keys(n))}),r.promise())}function a(e,r,n){return i.ajax({url:l+e+"-"+r+":predict",data:JSON.stringify({rev_id:n}),contentType:"application/json",type:"POST"})}function t(){var t=0,o=1,l=function(n,s){var g=[];s.forEach(function(e){g.push(a(c.wgDBname,e,Number(n)))}),i.when.apply(i,g).done(function(){s.length===1?r.apply(this,[arguments]):r.apply(this,arguments),t+=o,u.length>t&&l(u[t],s)}).fail(function(){e.log.error("The request failed.",arguments)})};e.loader.load("//meta.wikimedia.org/w/index.php?title=User:He7d3r/Tools/ScoredRevisions.css&action=raw&ctype=text/css","text/css"),n().done(function(e){u=e,u.length&&l(u[t],s)})}var o=e.util.getParamValue("showscores")!=="0",s=["damaging","goodfaith"],c=e.config.get(["wgIsArticle","wgCurRevisionId","wgCanonicalSpecialPageName","wgDBname","wgAction","ScoredRevisionsThresholds","ScoredRevisionsServerUrl","ScoredRevisionsEnableForPatrolledRevs"]),l=c.ScoredRevisionsServerUrl||"https://api.wikimedia.org/service/lw/inference/v1/models/",g=o&&(i.inArray(c.wgCanonicalSpecialPageName,["Watchlist","Recentchanges","Recentchangeslinked","Contributions"])!==-1||c.wgAction==="history"||c.wgIsArticle&&c.wgAction==="view"),u=[],d={},h=c.ScoredRevisionsThresholds||{low:.45,medium:.58,high:.8};g&&e.hook("wikipage.content").add(t)})(mediaWiki,jQuery);
// </nowiki>