User:Bradv/AjaxPatrolLinks.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function ajaxPatrolLinks(callback) {
    var a = document.evaluate('//div[@class="patrollink"]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
    if (a && typeof sajax_init_object !== 'undefined') {
        var div = a.parentNode;
        var url = a.href;
        a.onclick = function (event) {
            var aj = sajax_init_object();
            if (aj) {
                while (div.firstChild) { div.removeChild(div.firstChild) };
                div.appendChild(document.createTextNode('[Marking as patrolled...]'));
                aj.onreadystatechange = function () {
                    if (aj.readyState == 4 && aj.status == 200) {
                        while (div.firstChild) { div.removeChild(div.firstChild) };
                        div.appendChild(document.createTextNode('[Marked as patrolled]'));
                        aj.onreadystatechange = null;
                        if (callback) {
                            callback();
                        }
                    }
                }                
                aj.open("GET", url, true);
                aj.send(null);
                return false;
            }
        }
    }
}

addOnloadHook(ajaxPatrolLinks);