User:Ilmari Karonen/unwatchredlinks.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. |
Documentation for this user script can be added at User:Ilmari Karonen/unwatchredlinks. |
/*
This script adds a "Select all nonexistent pages" button to the watchlist edit page
([[Special:Watchlist/edit]]). To install it, add the line:
importScript('User:Ilmari_Karonen/unwatchredlinks.js');
to your monobook.js page (or to the corresponding page for other skins).
The button will only select pages that do not exist and do not have existing talk
pages either. As an unintended -- but probably desirable -- side effect, it will
not select user pages of any existing users either.
*/
if (mw.config.get('wgCanonicalNamespace') == "Special" && mw.config.get('wgCanonicalSpecialPageName') == "Watchlist") {
addOnloadHook(function () {
// It's somewhat hard to tell if we're on the "edit watchlist" subpage,
// so we'll just proceed as if we were and abort if something looks odd.
var form = document.getElementsByTagName("form")[0];
if (!form) return;
// This is our main check for which page we're on:
if (!/[&?]action=edit(&|$)/.test(form.action)) return;
var inputs = form.getElementsByTagName("input");
if (!inputs || inputs.length < 1) return;
var submit = inputs[inputs.length - 1];
if (submit.type != "submit") return;
inputs = null;
// Okay so far, now add the custom button.
var button = document.createElement("input");
button.type = "button";
button.value = "Select all nonexistent pages";
button.onclick = function () {
// Here's the code for what happens when you click it.
var items = form.getElementsByTagName("li");
if (!items || items.length < 1) return;
for (var i = 0; i < items.length; i++) {
var input = items[i].getElementsByTagName("input")[0];
if (!input || input.getAttribute("type") != "checkbox") continue;
var isRedlink = true; // assume redlink until proven otherwise
var links = items[i].getElementsByTagName("a");
for (var j = 0; isRedlink && j < links.length; j++) {
isRedlink = /[&?]action=edit(&|$)/.test(links[j].href);
}
input.checked = isRedlink;
}
};
// Now add the button to the page.
if (submit.nextSibling)
submit.parentNode.insertBefore(button, submit.nextSibling);
else
submit.parentNode.appendChild(button);
// Looks nicer with a space...
button.parentNode.insertBefore(document.createTextNode(" "), button);
});
}