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.
// [[User:Quarl/watchbutton.js]] - change standard watch/unwatch tab to be
// asynchronous.

// quarl 2006-02-03 initial version

// requires: wikiwatch.js, util.js

// <pre><nowiki>

var watchbutton = new Object();

watchbutton.toggleAsync = function() {
    if (watchbutton.currentlyWatched) {
        wikiwatch.unwatchAsync(wikiPage, watchbutton._wuwSuccess, watchbutton.tab);
    } else {
        wikiwatch.watchAsync(wikiPage, watchbutton._wuwSuccess, watchbutton.tab);
    }
    return false;
}

watchbutton._wuwSuccess = function() {
    watchbutton.currentlyWatched = !watchbutton.currentlyWatched;
    if (watchbutton.currentlyWatched) {
        watchbutton.link.innerHTML = 'Unwatch';
        watchbutton.link.href = wikiPage.qurl + '&action=unwatch';
        watchbutton.link.title = 'Remove this page from your watchlist';
    } else {
        watchbutton.link.innerHTML = 'Watch';
        watchbutton.link.href = wikiPage.qurl + '&action=watch';
        watchbutton.link.title = 'Add this page to your watchlist';
    }
}

watchbutton._load = function()
{
    var tab_unwatch = document.getElementById('ca-unwatch');
    if (tab_unwatch) {
        watchbutton.currentlyWatched = true;
        watchbutton.tab = tab_unwatch;
    }

    var tab_watch = document.getElementById('ca-watch');
    if (tab_watch) {
        watchbutton.currentlyWatched = false;
        watchbutton.tab = tab_watch;
    }

    if (!watchbutton.tab) return;

    watchbutton.link = watchbutton.tab.getElementsByTagName('a')[0];
    watchbutton.link.onclick = watchbutton.toggleAsync;

}

addOnloadHook(watchbutton._load);

// </nowiki></pre>