User:Ahecht/Scripts/watchlistcleaner.js
Appearance
< User:Ahecht | Scripts
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:Ahecht/Scripts/watchlistcleaner. |
// Watchlist cleaner
function cleanWatchlist() {
var cleanMiss = confirm("Remove redlinked (missing) pages from Watchlist?\n\n(OK for yes, Cancel for no)");
var cleanRedir = confirm("Remove redirects from Watchlist?\n\n(OK for yes, Cancel for no)");
var unwatchPages = [], unwatchPagesCount = 0;
var statusText = "Fetching watchlist...";
function doUnwatch() { // Recursively unwatch pages in batches of 50
if (unwatchPages.length > 0) { // Still have pages to unwatch
console.log("Pages to unwatch: ");
console.log(unwatchPages);
statusText = "Removing " + unwatchPages.length + " pages from watchlist...";
mw.notify(statusText, {type: 'info', tag: 'status'});
var uwTitles = unwatchPages.splice(0,50).join("|"); // Remove 50 items from top of list
var params = {
action: "watch",
unwatch: "true",
titles: uwTitles
};
new mw.Api().postWithToken("watch", params ).done( function(reslt) {
console.log("Unwatch successful: ");
console.log(reslt);
doUnwatch();
} ).fail( function(code, reslt) {
console.error("API error when unwatching pages: ");
console.error(reslt);
statusText = "API error when unwatching pages: " + code;
mw.notify(statusText, {type: 'error', tag: 'status'});
return;
} );
} else { // No more pages to unwatch
statusText = "Done. Removed " + unwatchPagesCount + " pages from watchlist";
mw.notify(statusText, {type: 'success', tag: 'status'});
}
}
function fetchWatchlist(cont) { // Recursively fetch watchlist
var query = {
action: "query",
prop: "info",
generator: "watchlistraw",
gwrlimit: "max",
formatversion: "2"
};
if (cont) {
query = Object.assign(query, cont);
}
mw.notify(statusText, {type: 'info', tag: 'status'});
statusText = statusText + ".";
new mw.Api().get( query )
.done (function (d) {
if (d && d.query && d.query.pages) { //API query returned pages
d.query.pages.forEach( function(i) {
if(i.ns % 2 == 0) { // Page isn't a talk page
if(cleanMiss && i.missing){ // Add missing page to list
mw.notify("Found missing page [[" + i.title + "]].", {type: 'warn', tag: 'found'});
unwatchPages.push(i.title);
} else if (cleanRedir && i.redirect) { // Add redirect to list
mw.notify("Found redirect [[" + i.title + "]].", {type: 'warn', tag: 'found'});
unwatchPages.push(i.title);
}
}
} );
}
if (d && d.continue) { // More results are available
fetchWatchlist(d.continue);
} else { // No more results, start removing from watchlist
unwatchPagesCount = unwatchPages.length;
mw.notify("Found " + unwatchPagesCount + " pages to remove.", {type: 'warn', tag: 'found'});
doUnwatch();
}
} ).fail (function(code, error) {
console.error("API error when fetching watchlist: ");
console.error(error);
statusText = "API error fetching watchlist: " + code;
mw.notify(statusText, {type: 'error', tag: 'status'});
} );
return;
}
if (cleanMiss || cleanRedir) { // Cancel wasn't selected for both options
fetchWatchlist();
}
}
$(document).ready( function() { // Add "Clean" link to toolbar
if( /Watchlist$/.test(mw.config.get('wgCanonicalSpecialPageName')) ) {
var cleanLink = ' | <a href="#" title="Run cleanwatchlist.js" id="clean-watchlist-link">Clean the watchlist</a>';
$('.mw-watchlist-toollinks').find('a').last().after(cleanLink);
$("#clean-watchlist-link").on("click", cleanWatchlist );
}
} );