User:Dcoetzee/Followed users.js

Source: 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.
/* Followed users is an English Wikipedia tool on Toolserver that
allows you to keep a list of users that you follow, similar to a
watchlist, and lists the most recent edit by each user. The idea was
conceived by User:Quintucket. It requires you to log in with a
TUSC account before you can use it at:

http://toolserver.org/~dcoetzee/followedusers/

This has many uses, such as keeping an eye on warned users,
collaborating with users in a team, mentoring, following students in a
class, or just for learning from the edits of others. All followed
user lists are public.

This optional Javascript extension adds "Follow user"/"Unfollow user"
options to your toolbox on user/user talk pages and a link to Followed
users to the upper-right. To use it, follow this link to edit your
common.js and add "importScript("User:Dcoetzee/Followed_users.js");"
to the end:

http://en.wikipedia.org/w/index.php?title=Special:MyPage/common.js&action=edit&editintro=User:Dcoetzee/Followed_users_js_notice
*/

function follow_user() {
    $.get(
	"https://toolserver.org/~dcoetzee/followedusers/api.php",
	{action : 'follow', username: $followee},
	function(data) {
	    if (data == 'ok') {
		document.getElementById('t-followuser').innerHTML =
		    "<a href=\"javascript:(function(){unfollow_user();})();\" title=\"Remove this user from your followed users list\">Unfollow user</a>";
            } else if (data == 'unauthenticated') {
		alert("You are not currently logged into the Followed users tool.");
	    }
	}
    );
}

function unfollow_user() {
    $.get(
	"https://toolserver.org/~dcoetzee/followedusers/api.php",
	{action : 'unfollow', username: $followee},
	function(data) {
	    if (data == 'ok') {
		document.getElementById('t-followuser').innerHTML =
		    "<a href=\"javascript:(function(){follow_user();})();\" title=\"Add this user to your followed users list\">Follow user</a>";
            } else if (data == 'unauthenticated') {
		alert("You are not currently logged into the Followed users tool.");
	    }
	}
    );
}

function add_portlet_link() {
    if (wgNamespaceNumber === 2 || wgNamespaceNumber === 3) {
	$.ajaxSetup({
	    type: "GET",
	    data: {},
	    xhrFields: { withCredentials: true },
	    crossDomain: true
	});
	$followee = wgTitle.match(/^([^\/]*)/)[0];
	$.get(
	    "https://toolserver.org/~dcoetzee/followedusers/api.php",
	    {action : 'query', prop : 'iswatching', follower: wgUserName, followee: $followee},
	    function(data) {
		if (data == 'yes') {
		    $( document ).ready( function() {
			mw.util.addPortletLink(
			    'p-tb',
			    'javascript:(function(){unfollow_user();})();',
			    'Unfollow user',
			    't-followuser',
			    'Remove this user from your followed users list'
			);
		    });
		} else if (data == 'no') {
		    $( document ).ready( function() {
			mw.util.addPortletLink(
			    'p-tb',
			    'javascript:(function(){follow_user();})();',
			    'Follow user',
			    't-followuser',
			    'Add this user to your followed users list'
			);
		    });
		}
	    }
	);
    }
}

followingNode = document.getElementById("pt-mycontris");
mw.util.addPortletLink( "p-personal"
		, "//toolserver.org/~dcoetzee/followedusers/list.php"
		, "Followed users"
		, "pt-followedusers"
		, "Lists recent edits by users you are following"
		, "d"
		, followingNode );

add_portlet_link();