User:Bryan/Scripts/ExtendedWatchlist/userwatch.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
function ewlUserwatch(oldest, allrev) {
    req = sajax_init_object();
    req.open('GET', wgScriptPath + '/index.php?title=User:' + wgUserName + '/Scripts/ExtendedWatchlist/userwatch&action=raw&smaxage=0&maxage=0&ctype=text/javascript', false);
    req.send(null);
    users = eval('(' + req.responseText + ')');
    var users2 = new Array();
    for (i=0; i<users.length; i++) users2[users2.length] = 'User:' + encodeURIComponent(users[i]);
    
    qs = 'what=usercontribs&uccomments&format=json&titles=' + users2.join('|')
    qs = qs + '&uclimit=' + Math.floor(500 / users.length);
    qs = qs + '&ucstart=' + createTimestamp(oldest);
    if (!allrev) qs = qs + '&uctop=only';

    req = sajax_init_object();
    req.open('GET', '/w/query.php?' + qs, false);
    req.send(null)
    wl = eval('(' + req.responseText + ')')['pages'];
    
    var Watchlist = new Array()
    for (i in wl) {
        if (!wl[i]['contributions']) continue;
        for (j=0; j<wl[i]['contributions'].length; j++) {
            var item = wl[i]['contributions'][j];
            var rev = new Object();
            rev['timestamp'] = parseTimestamp(item['timestamp']);
            rev['user'] = wl[i]['title'].substr(5);
            rev['title'] = item['*'];
            rev['revid'] = item['revid'];
            if (item['top'] == '') rev['top'] = '';
            if (item['new'] == '') rev['new'] = '';
            if (item['minor'] == '') rev['minor'] = '';
            if (item['comment']) rev['comment'] = item['comment'];
            Watchlist[Watchlist.length] = rev;
        }
    }
    
    return Watchlist;
}
WatchlistPlugins['Userwatch'] = ewlUserwatch;

function initUserwatch() {
    var user = new RegExp('^User.*?\:(.*?)(\/.*)?$').exec(wgPageName);
    if (user) {
        user = user[1];
        
        req = sajax_init_object();
        req.open('GET', wgScriptPath + '/index.php?title=User:' + wgUserName + '/Scripts/ExtendedWatchlist/userwatch&smaxage=0&maxage=0&action=raw&ctype=text/javascript', true);
        req.onreadystatechange = function () {
            if (req.readyState != 4) return;
            users = eval('(' + req.responseText + ')');
            addWatchLink(users, user);
        };
        req.send(null);
    }
}
$(initUserwatch);

function addWatchLink(users, user) {
    i = users.indexOf(user);
    if (i == -1) {
        addToolboxLink('javascript:void(setUserWatch(' + user.quote() + ', "watch"));', 'Watch user', 'ewl_watchuser');
    } else {
        addToolboxLink('javascript:void(setUserWatch(' + user.quote() + '));', 'Unwatch user', 'ewl_watchuser');
    }
}


function setUserWatch(user, setWatch) {
    iframe = document.createElement('iframe');
    iframe.setAttribute('style', 'width: 640px; height: 320px;');
    //iframe.setAttribute('style', 'width: 0; height: 0; border: 0;');
    document.body.appendChild(iframe);
    onloadfunc = function () {
        onloadfunc2 = function () {
            a = document.getElementById('ewl_watchuser');
            a.parentNode.parentNode.removeChild(a.parentNode);
            addWatchLink(users, user);
            iframe.onload = function () {};
            iframe.onreadystatechange = function () {};
        }
        if (iframe.readyState) iframe.onreadystatechange = function () {
            if (iframe.readyState == 'complete') onloadfunc2();
        };
        else iframe.onload = onloadfunc2;

        doc = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
        users = eval('(' + getText(doc.getElementById('wpTextbox1')) + ')');
        i = users.indexOf(user);
        var summary = '';
        if (setWatch) {
            if (i == -1) {
                users.push(user);
                summary = '+';
            }
        } else {
            if (i != -1) {
                users = users.filter(function (o) { return o != user; });
                summary = '-';
            }
        }
        setText(doc.getElementById('wpTextbox1'), users.toSource());
        doc.getElementById('wpSummary').value = summary + user;
        doc.getElementById('wpSave').click();
    }
    if (iframe.readyState) iframe.onreadystatechange = function () {
         if (iframe.readyState == 'complete') onloadfunc();
    }
    else iframe.onload = onloadfunc;

    if (iframe.contentDocument) iframe.contentDocument.location.replace(wgScriptPath + '/index.php?title=User:' + wgUserName + '/Scripts/ExtendedWatchlist/userwatch&action=edit&useskin=simple');
    else iframe.contentWindow.document.location.replace(wgScriptPath + '/index.php?title=User:' + wgUserName + '/Scripts/ExtendedWatchlist/userwatch&action=edit&useskin=simple');

}