User:Ilmari Karonen/unwatchredlinks.js

/* 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); }); }