User:Rodejong/watchlist.js

function WLScript{ var whenPageLoaded = +(new Date) - 20000 //add 20 sec just in case var namespace, $content, mainTab, hideInterfaceCSS var alreadySorted, alreadyAddedUnwatch, isEnhanced, mm var inProgress = null, timeoutID = null mm = {sortTip:'Sort rows by even namespace number, then by page title' ,sortDone:'Watchlist changes already sorted' ,unwatchTip:'Add (x) quick unwatch links' ,unwatchDone:'Use (x) links to quickly unwatch pages' ,onlynew:'Only new' ,onlynewTip:'Show changes since this page was last loaded' ,expandAll:'Expand multiple edits' ,fullPage:'Hide/Show interface' } namespace = document.getElementById('namespace') if (!namespace) return PngFixDisabled = true //enwiki $content = $('#bodyContent') if (!$content.length){ $content = $('#mw_content, #article').eq(0) if (!$content.length) $content = $(document.body) // covered all the skin but who knows... } //enhanced RC: make (x) appear by clicking on timestamp isEnhanced = $content.find('ul.special').length == 0 if (isEnhanced) $content.click(toggleXLink) //find insertion points for links: after "days all" var linksAt = $('#mw-watchlist-options').find('a[href*=&days=7]').eq(0).next.next if (!linksAt.length) linksAt = $('#mw-watchlist-options').find('hr').last //find "Special" tab mainTab = $('#ca-special, #ca-nstab-special').eq(0) //"only new" link and tab addLnk(mm.onlynew, mm.onlynewTip).mousedown(onlyNewEntries).attr('id', 'listSince') addTab(mm.onlynew, mm.onlynewTip).mousedown(onlyNewEntries) //"unwatch" link(s) if (window.unwatchLinksOnLoad) addXLinks else addLnk('x', mm.unwatchTip).click(addXLinks) //"sort" link addLnk('↑↓', mm.sortTip).click(sortWatchlist) //"expand all" link if ($('#mw-rc-openarrow-0').length) addLnk('±', mm.expandAll).click(expandMultipleEdits) //"hideInterface" tab addTab('↸',mm.fullPage).click(hideInterface).attr('href','#') if (document.cookie.indexOf('wlmax=1') != -1) hideInterface function addLnk(txt, tip){ linksAt.before(' | ') return $(''+txt+'').insertBefore(linksAt) } function addTab(txt, tip){ var tab = mainTab.clone(true).removeClass('selected').attr('id','') tab.find('a').text(txt).attr('title', tip).attr('accesskey','') return tab.appendTo(mainTab.parent) } return function onlyNewEntries(e) { var url = window.location.href.split('#')[0] var days = ( +(new Date) - whenPageLoaded)/(1000 * 3600 * 24) e.target.href = /[?&]days=/.test(url) ? url.replace(/([?&]days=)[^&]*/, '$1'+days) : url + (url.indexOf('?') < 0 ? '?':'&') + 'days=' + days return true } function sortWatchlist(e){ e.preventDefault if (alreadySorted) return alert(mm.sortDone) $content.find('h4').each(function(i, H4){ //sort all days separately  sortDay($(H4).next('div, ul')) }) alreadySorted = true } function sortDay(dayDiv){ var i, pgname, rowElem, hiddenDiv, rows = dayDiv.find('a[href*="&action=history"]') for (i = 0; i < rows.length; i++){ pgname = getTitleFromURL(rows[i].href) ns = getNSFromTitle(pgname) if (ns>0) pgname = pgname.substring(getNamespace(ns).length+1) //title w/o prefix if (ns%2) ns-- //sort talk page as if it was a base page rows[i].sortkey = zzz(ns) + ':' + pgname //assign custom tag attribute: namespace+title } //sort rows array rows.sort(function(a,b){  if (a.sortkey > b.sortkey) return 1   else if (a.sortkey < b.sortkey) return -1   else return 0 }) //sort rows in HTML, by moving all to the bottom if (isEnhanced) for (i=0; i<rows.length; i++){ rowElem = rows.eq(i).closest('table') hiddenDiv = rowElem.next('div') dayDiv.append(rowElem, hiddenDiv) } else for (i=0; i<rows.length; i++){ rowElem = rows.eq(i).closest('li') dayDiv.append(rowElem) } } function expandMultipleEdits(e){ e.preventDefault var i = 0, sp, state = $('#mw-rc-openarrow-0')[0].style.display while (sp=document.getElementById('mw-rc-openarrow-'+(i++).toString)) if (sp.style.display == state) $(sp.firstChild).click } function addXLinks(e){ if (e) e.preventDefault if (alreadyAddedUnwatch) return alert(mm.unwatchDone) $content.find('a[href*="&action=history"]').each(function(i, lnk){ addXLink(lnk) }) alreadyAddedUnwatch = true } function addXLink(hst){ hst = $(hst) var x = $('') .attr('href', hst.attr('href').replace(/&curid=\d+/,'').replace(/action=history/,'action=unwatch')) .click(ajaxUnwatch) if (isEnhanced) hst.parent.prepend( x.append('(x)'), ' ' ) else hst.after( ' | ', x.append('x') ) } function toggleXLink(e){ //add (x) when clicking on timestamp if ( ! $(e.target).filter('td.mw-enhanced-rc').length ) return var tbl = $(e.target).parents('table.mw-enhanced-rc') var x = tbl.find('a.aj-unwatch') if (x.length) x.remove else addXLink(tbl.find('a[href*="&action=history"]')[0]) } function ajaxUnwatch(e) { if (inProgress) return false inProgress = getTitleFromURL(this.href) timeoutID = window.setTimeout( function {inProgress = null}, 10000 ) //call server var req = { action:'watch', format:'json', title:inProgress } if (/&action=unwatch/.test(this.href)) req.unwatch = '' $.getJSON( wgScriptPath + '/api.php', req, showUnwatch) return false } function showUnwatch (response) { if (timeoutID) window.clearTimeout(timeoutID) response = response.watch var name = inProgress, state, pg inProgress = null if (response.watched !== undefined) state = false else if (response.unwatched !== undefined) state = true else return //unrecognized response //find the full name of "other page" var ns = getNSFromTitle(name) var name2 = name if (ns > 0) name2 = name2.substring(getNamespace(ns).length+1) //remove old prefix if (ns % 2) ns--; else ns++ //switch to  "other" namespace if (ns > 0) name2 = getNamespace(ns) + ':' + name2 //add new prefix //now mark all rows that are either name or name2 $content.find('a.aj-unwatch').each(function(i, lnk){	lnk = $(lnk)   pg = getTitleFromURL(lnk.attr('href'))    if (pg != name && pg != name2) return    lnk	 .attr('title', mw.msg(state?'watch':'unwatch') ) 	 .attr('href', lnk.attr('href').replace(/&action=\w+/, '&action='+ (state?'watch':'unwatch')) )	 .parent.find('a').css('text-decoration', state ? 'line-through' : '') }) } function hideInterface(e){ if (e) e.preventDefault if (!hideInterfaceCSS) hideInterfaceCSS = mw.util.addCSS('\ div#siteNotice, h1#firstHeading, #siteSub, #contentSub, fieldset#mw-watchlist-options,\ div.mw-rc-label-legend, #mw-fr-watchlist-pending-notice {display:none}') else hideInterfaceCSS.disabled = !hideInterfaceCSS.disabled document.cookie = 'wlmax=' + (!hideInterfaceCSS.disabled ? '1' : '0;expires=' + (new Date).toGMTString + ';;') var a = mainTab.find('a') //replace "Special" tab text with "Watchlist" if (hideInterfaceCSS.disabled){ //restore a.text( a.attr('oldtext') ) }else{//set to "watchlist" a.attr('oldtext', a.text) a.text($('h1#firstHeading').text) } } //common functions; need 'namespace' select element function getNSFromTitle(title){ //returns namespace number var i = title.indexOf(':') if (i == -1) return 0 var prefix = title.substring(0,i+1) //including ':' for (i=2; i < namespace.options.length; i++) if (namespace.options[i].text+':' == prefix) return i-1 return 0 // ':' was just a part of the title } function getNamespace(ns){ //returns namespace name if (ns==0) return '' else return namespace.options[ns+1].text } function getTitleFromURL (url){ //gets 'title=' part from a link var ma = url.match(/(&|\?)title=([^&]+)/) if (ma) return decodeURIComponent(ma[2]).replace(/_/g,' ') else return '' } function zzz(s){ // 5 -> 005 s = s.toString if (s.length==1) return '00'+s else if (s.length==2) return '0'+s else return s } } if (wgCanonicalSpecialPageName == 'Watchlist' && wgAction == 'view') WLScript