Jump to content
Wikipedia The Free Encyclopedia

User:MusikAnimal/customWatchlists.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:MusikAnimal/customWatchlists and an accompanying .css page at User:MusikAnimal/customWatchlists.css.
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.
 // customWatchlists.js
 // version 0.1.3
 $(function(){
 $("head").append(mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:MusikAnimal/customWatchlists.css&action=raw&ctype=text/css','text/css'));

 varaddCwLinkListener=function(){
 $(".cw-add").click(function(){
 if($("#cw-overlay")[0])returnfalse;
 varpageName=newmw.Title($(this).siblings(".mw-title").text());
 pageName=pageName.getNamespacePrefix()+pageName.getMain();
 getCustomWatchlists().then(setupCactionInterface.bind(this,pageName),setupCactionInterface.bind(this,null));
 });
 };

 vargetCustomWatchlists=function(){
 returnPromise.resolve($.get("/wiki/User:"+mw.config.get('wgUserName')+"/watchlists?action=raw"));
 };

 varformatDate=function(dateObj){
 // TODO: replace monthNames with wgMonthNames
 varmonthNames=["January","February","March","April","May","June","July","August","September","October","November","December"];
 return(dateObj.getHours()+100).toString().slice(-2)+":"+(dateObj.getMinutes()+100).toString().slice(-2)+", "+(dateObj.getUTCDate()+100).toString().slice(-2)+" "+monthNames[dateObj.getUTCMonth()]+" "+dateObj.getUTCFullYear();
 };

 vargenerateListItem=function(data){
 varmwName=newmw.Title(data.title);
 varpageUrl="/w/index.php?title="+mwName.getNamespacePrefix()+mwName.getMain().replace(/'/g,"%27"),
 editDate=newDate(data.timestamp);
 if(data.newlen){
 varlengthDiff=data.newlen-data.oldlen;
 vardiffClass=lengthDiff>=0?"mw-plusminus-pos":"mw-plusminus-neg";
 }
 return"<li class='mw-line-even mw-changeslist-line-not-watched'>"+
 "("+
 "<a href='"+pageUrl+"&diff=prev&oldid="+data.revid+"' class='nonimage'>diff</a>&nbsp;| "+
 "<a href='"+pageUrl+"&action=history' class='nonimage'>hist</a>&nbsp;| "+
 "<a href='javascript:' class='cw-add'>cw</a>"+
 ")&nbsp;"+
 "<span class='mw-changeslist-separator'>. .</span>&nbsp;"+
 "<span class='mw-changeslist-date'>"+formatDate(editDate)+"</span>&nbsp;"+
 "<span class='mw-title'><a href='/wiki/"+encodeURI(data.title).replace(/'/g,"%27")+"' class='mw-changeslist-title nonimage'>"+data.title+"</a></span>&nbsp;"+
 (data.newlen?"<span class='mw-changeslist-separator'>. .</span>&nbsp;<span dir='ltr' class='"+diffClass+"'>("+(lengthDiff>0?"+":"")+lengthDiff+")</span> ":"")+
 "<span class='mw-changeslist-separator'>. .</span>&nbsp;"+
 "<a href='/wiki/User:"+data.user+"' class='mw-userlink nonimage'>"+data.user+"</a> "+
 "(<a href='/wiki/User talk:"+data.user+"'>talk</a> | <a href='/wiki/Special:Contributions/"+data.user+"'>contribs</a>)"+
 (data.parsedcomment?"&nbsp;<span style='font-style:italic'>("+data.parsedcomment+")</span>":"")+
 "</li>";
 };

 varshowCustomWatchlist=function(type,lists,target,limit,allrev){
 varapiRoot="/w/api.php?action=query";
 varpages=JSON.parse(JSON.stringify(lists[target]));
 varpagesLength=pages.length;
 for(vari=0;i<pagesLength;i++){
 varmwPageName=newmw.Title(pages[i]);
 if(mwPageName.namespace%2===0){
 pages.push(mwPageName.getNamespacePrefix().slice(0,-1)+(mwPageName.namespace===0?"Talk:":"_talk:")+mwPageName.getMain());
 }
 }
 $(".mw-changeslist").html("Loading...");

 varnewHtml="";
 if(type==="rc"){
 $.get(apiRoot+"&list=watchlist&wlprop=user|parsedcomment|timestamp|sizes|title|ids&wltype=edit&wllimit="+limit+(allrev===true?"&wlallrev=true":"")+"&format=json",function(data){
 varmatches=$.grep(data.query.watchlist,function(el){
 varmwName=newmw.Title(el.title);
 returnpages.indexOf(mwName.getNamespacePrefix()+mwName.getMain())>=0;
 });
 for(vari=0;i<matches.length;i++){
 newHtml+=generateListItem(matches[i]);
 }
 $(".mw-changeslist").html(newHtml);
 addCwLinkListener();
 mw.hook('wikipage.content').fire($('.mw-changeslist'));
 });
 }else{
 varqueryablePages=$.map(pages,function(p,i){returnencodeURIComponent(p).replace(/'/g,"%27")}).join("|");
 $.get(apiRoot+"&prop=revisions&rvprop=ids|timestamp|user|parsedcomment&titles="+queryablePages+"&format=json",function(data){
 varsortedData=[];
 for(varpageIdindata.query.pages){
 if(parseInt(pageId)>0){
 varpageData=data.query.pages[pageId];
 sortedData.push({
 parsedcomment:pageData.revisions[0].parsedcomment,
 revid:pageData.revisions[0].revid,
 timestamp:pageData.revisions[0].timestamp,
 title:pageData.title,
 user:pageData.revisions[0].user
 });
 }
 }
 $.each(sortedData.sort(function(x,y){
 if($("#cw-rw-options input:checked").val()==="timestamp"){
 returnnewDate(y.timestamp).getTime()-newDate(x.timestamp).getTime();
 }else{
 returnx.title.localeCompare(y.title);
 }
 }),function(i,v){
 newHtml+=generateListItem(v);
 });
 $(".mw-changeslist").html(newHtml);
 addCwLinkListener();
 mw.hook('wikipage.content').fire($('.mw-changeslist'));
 });
 }
 };

 varshowCustomWatchlistsForm=function(appendHtml){
 console.log("testing");
 varhtml="<form id='custom_watchlist_form'><fieldset>"+
 "<legend>Custom watchlists (<a href='//en.wikipedia.org/w/index.php?title=User:MusikAnimal/customWatchlists'>documentation</a>)</legend>"+appendHtml;
 $("#mw-watchlist-form").after(html);
 };

 varsetupCactionInterface=function(data,argName){
 if(argName){
 pageName=data;
 data=argName;
 }else{
 pageName=mw.config.get('wgPageName');
 }
 varcustomWatchlists=data?JSON.parse(data.split("\n")[0]):{};
 varcustomWatchlistNames=Object.keys(customWatchlists),
 inWatchlists=[];

 for(varwlincustomWatchlists){

 if(customWatchlists[wl].indexOf(pageName.replace("_talk:",":").replace("Talk:",""))!==-1){
 inWatchlists.push(wl);
 }
 }

 varhtml="<div id='cw-overlay'>"+
 "<div class='header'>"+
 "Add/remove page to custom watchlists"+
 "<span class='closer-x' onclick=\"$('#cw-overlay').remove();\"></span>"+
 "</div>"+
 "<div id='cw-overlay-body'>"+
 (argName?"<p style='margin-top:0'>Page name: <i>"+pageName.replace(/_/g," ")+"</i></p>":"");

 for(vari=0;i<customWatchlistNames.length;i++){
 varlistName=customWatchlistNames[i];
 html+="<span><input class='cw-option' type='checkbox' value='"+i+"' "+(inWatchlists.indexOf(listName)!==-1?"checked data-index='"+customWatchlists[listName].indexOf(pageName)+"'":"")+" id='cw-option-"+i+"' /><label for='cw-option-"+i+"'>"+listName+"</label> (<a class='cw-delete' href='javascript:' data-id='"+i+"'>del</a>)</span>";
 }

 html+="<div id='cw-overlay-new-watchlist'>"+
 "<input class='cw-option' type='checkbox' value='-1' id='cw-new-option' "+(data?"":"checked")+" /><label for='cw-new-option'>New watchlist</label>"+
 "<input type='text' id='cw-overlay-new-watchlist-input' placeholder='Enter watchlist name' "+(data?"":"style='display:inline-block'")+" />"+
 "</div><button id='cw-overlay-selector-submit'>Save changes</button></div></div>";

 $("body").append(html);

 if(!data)$("#cw-overlay-new-watchlist-input").focus();

 $("#cw-new-option").change(function(){
 if($(this).is(":checked")){
 $("#cw-overlay-new-watchlist-input").show().focus();
 }else{
 $("#cw-overlay-new-watchlist-input").hide();
 }
 });

 $(".cw-delete").click(function(){
 if($(this).text()==="undel"){
 $("#cw-option-"+$(this).data('id')).prop('checked',false).siblings("label").removeClass("disabled");
 $("#cw-option-"+$(this).data('id')).data('delete',null);
 $(this).text("del");
 }else{
 varname=customWatchlistNames[$(this).data('id')];
 varcwToDelete=customWatchlists[name];
 if(confirm("Mark the watchlist \""+name+"\" and all it's "+cwToDelete.length+" entries for deletion?")){
 $(this).text("undel");
 $("#cw-option-"+$(this).data('id')).data('delete',true);
 $("#cw-option-"+$(this).data('id')).prop('checked',true).one("click",function(){
 $(this).siblings("a").trigger("click");
 }).siblings("label").addClass("disabled");
 }
 }
 });

 $("#cw-overlay-selector-submit").click({
 customWatchlistNames:customWatchlistNames,
 customWatchlists:customWatchlists,
 inWatchlists:inWatchlists,
 pageName:pageName
 },function(e){
 $("#cw-overlay-selector-submit").replaceWith("Saving...");
 varcw=e.data.customWatchlists,
 iw=e.data.inWatchlists,
 pageName=e.data.pageName.replace("_talk:",":").replace("Talk:",":"),
 toWatch=false,
 updateStr="";

 $.each($(".cw-option"),function(i){
 varid=parseInt($(this).val());
 varkey=id<0?$("#cw-overlay-new-watchlist-input").val().replace(/[^\w\s]/gi,''):customWatchlistNames[id];
 varexists=iw.indexOf(key)>=0;

 if($(this).is(":checked")){
 if($(this).data("delete")){
 updateStr="Deleted the custom watchlist <b>"+key+"</b>.";
 deletecw[key];
 }else{
 toWatch=true;
 if(id<0){
 updateStr="Created the custom watchlist <b>"+key+"</b> with <b>"+pageName+"</b>";
 cw[key]=[pageName];
 }else{
 updateStr="Added <b>"+pageName+"</b> to the custom watchlist <b>"+key+"</b>";
 cw[key].push(pageName);
 }
 }
 }elseif(exists){
 updateStr="Removed <b>"+pageName+"</b> from the custom watchlist <b>"+key+"</b>";
 cw[key].splice($(this).data('index'),1);
 }
 });

 varstringifiedCw=JSON.stringify(cw)+"\nBacklink: [[User:MusikAnimal/customWatchlists]]";

 varapi=newmw.Api();
 api.watch(pageName).done(function(watchResult){
 api.postWithToken("edit",{
 action:"edit",
 title:"User:"+mw.config.get('wgUserName')+"/watchlists",
 summary:"updating [[User:MusikAnimal/customWatchlists|custom watchlists]]",
 text:stringifiedCw
 }).done(function(result,jqXHR){
 $("#cw-overlay-body").html("Success!");
 mw.notify($("<div>"+updateStr+"</div"));
 setTimeout(function(){
 $("#cw-overlay").remove();
 },3000);
 }).fail(function(code,result){
 if(code==="http"){
 mw.log("HTTP error: "+result.textStatus);// result.xhr contains the jqXHR object
 }elseif(code==="ok-but-empty"){
 mw.log("Got an empty response from the server");
 }else{
 mw.log("API error: "+code);
 }
 });
 }).fail(function(code,result){
 if(code==="http"){
 mw.log("HTTP error: "+result.textStatus);// result.xhr contains the jqXHR object
 }elseif(code==="ok-but-empty"){
 mw.log("Got an empty response from the server");
 }else{
 mw.log("API error: "+code);
 }
 });
 });
 };

 if(mw.config.get('wgRelevantPageName')==="Special:Watchlist"){
 getCustomWatchlists().then(function(data){
 varcustomWatchlists=JSON.parse(data.split("\n")[0]);
 varcustomWatchlistNames=Object.keys(customWatchlists);
 varhtml="<p><label for='custom_watchlist_selector'>Custom list:</label>&nbsp;<select id='custom_watchlist_selector'>";

 for(vari=0;i<customWatchlistNames.length;i++){
 varlistName=customWatchlistNames[i];
 html+="<option value='"+i+"'>"+listName+"</option>";
 }

 html+="</select></p>"+
 "<p id='cw-list-type'>Show:&nbsp;<label><input type='radio' name='cw-display-type' value='rc' checked /> Recent changes</label>&nbsp;<label><input type='radio' name='cw-display-type' value='rw' /> Raw watchlist</label></p>"+
 "<p id='cw-rw-options' style='display:none'>Sorting:&nbsp;<label><input type='radio' name='cw-rw-sorting' value='timestamp' checked /> Last edited</label>&nbsp;<label><input type='radio' name='cw-rw-sorting' value='title' />Alphabetical</label></p>"+
 "<p id='cw-rc-options'><label for='custom_watchlist_limit'>Search limit (from base watchlist):</label>&nbsp;<select id='custom_watchlist_limit'>";
 varlimitArr=[50,100,250,500,1000,2500,5000];
 for(varj=0;j<limitArr.length;j++){
 html+="<option val='"+limitArr[j]+"'>"+limitArr[j]+"</option>";
 }

 html+="</select><br/>"+
 "<input type='checkbox' id='custom_watchlist_all_rev' value='wlallrev' /><label for='custom_watchlist_all_rev'>Include multiple revisions to same page</label></p>"+
 "<p><button id='custom_watchlist_submit'>Go</button></fieldset></p></form>";

 showCustomWatchlistsForm(html);

 $("#custom_watchlist_submit").click(function(e){
 e.preventDefault();
 showCustomWatchlist($("#cw-list-type input[type=radio]:checked").val(),customWatchlists,customWatchlistNames[parseInt($("#custom_watchlist_selector").val())],$("#custom_watchlist_limit").val(),$("#custom_watchlist_all_rev").is(":checked"));
 });
 $("#custom_watchlist_form input[type=radio]").click(function(){
 if($(this).val()==="rc"){
 $("#cw-rc-options").show();
 $("#cw-rw-options").hide();
 }else{
 $("#cw-rc-options").hide();
 $("#cw-rw-options").show();
 }
 });
 },function(){
 showCustomWatchlistsForm("<p>No <a href='https://en.wikipedia.org/wiki/User:MusikAnimal/customWatchlists'>custom watchlists</a> yet! Go to a <a href='/wiki/Special:Random'>page</a> and create a custom watchlist by selecting the \"Custom Watchlists&hellip;\" item from the More menu.</p>");
 });
 $(".mw-changeslist .special li").each(function(){
 $anchor=$(this).find("a").eq(1);
 if($anchor.text()==="hist"&&!$anchor.siblings(".wikibase-edit").length){
 $anchor.after("&nbsp;| <a class='cw-add' href='javascript:'>cw</a>");
 }
 });
 addCwLinkListener();
 }elseif(mw.config.get('wgNamespaceNumber')>=0){
 mw.util.addPortletLink(
 'p-cactions',
 'javascript:',
 'Custom watchlist...',
 'ca-add-to-cw'
 );
 $("#ca-add-to-cw").click(function(){
 if($("#cw-overlay")[0])returnfalse;
 getCustomWatchlists().then(setupCactionInterface,setupCactionInterface.bind(this,null,null));
 });
 }
 });

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