User:Begoon/unWatch.js
Appearance
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:Begoon/unWatch. |
/* This script adds an "unwatch" link to each entry in your watchlist, using the new
* ajax watchlist thing.
*/
var wlUnwatch = {
supported: true,
onLoad: function () {
var i, j, m, a, span, d, links, link;
// history links
links = $( '.mw-changeslist > ul > li .mw-changeslist-links' );
for ( i = links.length-1; i >= 0; i-- ) {
link = $( links[ i ] ).find( 'a' )[0];
if ( !link ) {
continue;
}
m = link.href.match( /title=([^&]*)/ );
if ( !m ) {
continue;
}
a = document.createElement( 'A' );
a.href = mw.util.wikiScript() + '?action=unwatch&title=' + m[ 1 ];
a.title = 'Unwatch ' + link.title;
a.unwatchInProgress = false;
a.onclick = wlUnwatch.onClick;
a.appendChild( document.createTextNode( 'unw' ) );
span = document.createElement( 'SPAN' );
span.appendChild( a );
links[ i ].appendChild( span );
}
// log entries
links = $( '.mw-changeslist > ul > li > .mw-changeslist-line-inner > a' );
for ( i = links.length-1; i >= 0; i-- ) {
if ( /\/Special:Log\/newusers$/.test( links[ i ].href ) ) {
j = i + 1;
} else if ( /\/Special:Log\//.test( links[ i ].href ) ) {
j = i + 2;
} else {
continue;
}
m = links[ j ].href.match( /title=([^&]*)/ );
if ( !m ) {
m = links[ j ].href.match( /\/wiki\/([^?]*)/ );
}
if ( !m ) {
continue;
}
a = document.createElement( 'A' );
a.href = mw.util.wikiScript() + '?action=unwatch&title=' + m[ 1 ];
a.title = 'Unwatch ' + links[ j ].title;
a.unwatchInProgress = false;
a.onclick = wlUnwatch.onClick;
a.appendChild( document.createTextNode( 'unw' ) );
links[ i ].parentNode.insertBefore( a, links[ i ].nextSibling );
// insert a delimiter between the two links
d = document.createTextNode( '\xa0| ' );
links[ i ].parentNode.insertBefore( d, a );
}
},
onClick: function () {
if ( !wlUnwatch.supported || false ) {
wlUnwatch.supported = false;
return true;
}
/* START - CONFIRMATION PROMPT ADDED */
/* ================================= */
if (!confirm('Remove this page from your watchlist?')) {
wlUnwatch.supported = true;
return false;
}
/* END - CONFIRMATION PROMPT ADDED */
/* ================================= */
var link = this;
if ( link.unwatchInProgress ) {
return false;
}
link.unwatchInProgress = true;
link.style.color = 'red';
var timeout = setTimeout( function() {
timeout = null;
link.unwatchInProgress = false;
link.style.color = '';
}, 10000 );
var api = new mw.Api();
api.postWithToken( 'watch', {
action: 'watch',
unwatch: 1,
titles: decodeURIComponent( link.href.match( /&title=(.+)/ )[ 1 ].replace( /_/g,' ' ) )
} ).done( function( data ) {
if ( timeout ) {
clearTimeout( timeout );
}
timeout = null;
link.unwatchInProgress = false;
link.style.color = '';
if ( data.watch[ 0 ].unwatched !== undefined ) {
var li;
for ( li = link; li && li.nodeName !== 'LI'; li = li.parentNode ); // Empty loop
if ( li ) {
var classes = li.className.split(' ');
var i, len, cl, matches, ns, assocCl;
for ( i = 0, len = classes.length; i < len; i++ ) {
cl = classes[i];
matches = cl.match( /^watchlist-(\d+)-(.*)/ );
if ( matches ) {
break;
}
}
if ( matches ) {
ns = Number( matches[ 1 ] );
ns ^= 1; // use bitwise XOR to get the associated namespace number
assocCl = 'watchlist-' + ns + '-' + matches[ 2 ];
$( '.' + cl ).remove();
$( '.' + assocCl ).remove();
} else {
li.parentNode.removeChild( li );
}
}
}
} ).fail( function ( error ) {
if ( timeout ) {
clearTimeout( timeout );
}
timeout = null;
link.unwatchInProgress = false;
link.style.color = '';
} );
return false;
}
};
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Watchlist' && mw.config.get( 'wgAction' ) === 'view' ) {
mw.loader.using( [ 'mediawiki.util','mediawiki.api' ], function() {
$( document ).ready( wlUnwatch.onLoad );
} );
}