Skip to content

Instantly share code, notes, and snippets.

@nitzel
Last active August 28, 2019 22:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitzel/590a97ecbdb2c8256fff to your computer and use it in GitHub Desktop.
Save nitzel/590a97ecbdb2c8256fff to your computer and use it in GitHub Desktop.
Follow/Unfollow all members of a Star Citizen organization.
/**
* @author nitzel
* @desc A simple script to follow/unfollow all members of a organization
* in Star Citizen. To use it just
* 1. log in on https://robertsspaceindustries.com
* 2. Find out the SID(Spectrum ID/handle) of your organization
* 2. Open the javascript console (Chrome: F12, ESC toggles it)
* 3. Paste this whole script and press <ENTER>
* 4. To follow all: changeOrgFollow('SID', true)
* To unfollow all: changeOrgFollow('SID', false)
* 5. Wait till done
* 6. When sharing, please refer to this website ( https://gist.github.com/nitzel/590a97ecbdb2c8256fff )
*
* Example: For "ACES HIGH ~ UP WITH THE IRONS!" the SID would be "ACESHI".
* We want to follow the members, so we say true. To unfollow it would be false.
* > changeOrgFollow("ACESHI", true);
*
* There are a few messages that can come up, you can usually ignore them.
* OK -> it worked :)
* Validation failed -> You already follow this player.
* ErrCannotAddItself -> You cannot follow yourself.
* ErrContactRelationNotFound -> You cannot unfollow s.o. you do not follow.
*/
/* getCookie from http://stackoverflow.com/a/11767598 by Paul Sweatte
* Extracts a cookie from document.cookie
*/
function getCookie(cookiename) {
// Get name followed by anything except a semicolon
var cookiestring=RegExp(""+cookiename+"[^;]+").exec(document.cookie);
// Return everything after the equal sign
return unescape(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : "");
}
// additional headers, here we only use the RSI token from the cookie and set the content type to JSON.
var __hs = {"X-Rsi-Token": getCookie("Rsi-Token"), "content-type": "application/json; charset=utf-8;"};
/**
* Follow one Star Citizen.
* name is the nickname/handle of the player.
* follow=true -> follow, follow=false -> unfollow
*/
function changeFollow(name, follow){
$.ajax({
type: "post",
url: "https://robertsspaceindustries.com/api/contacts/"+(follow?"add":"erase"),
success: function(d){
// tell the user if it worked
if (d.success === 0) {
if(d.code!=="ErrContactRelationNotFound") {
console.warn('Failed to add/remove user', name, 'message:', d.msg);
console.debug(d);
}
}
else {
console.log((follow?"Following ":"Unfollowing ")+name + "-> " + d.msg);
}
},
data: JSON.stringify({nickname: name}),
headers: __hs
});
}
/**
* Follow all members of an organization
* sid is the SID, Spectrum ID/handle of the organization.
* follow=true -> follow, follow=false -> unfollow
* page defaults to 1, dont use it.
*/
function changeOrgFollow(sid, follow, page) {
page = page || 1;
$.ajax({ // request a page of members
type: "post",
url: "https://robertsspaceindustries.com/api/orgs/getOrgMembers",
success: function(d){
//console.debug(d); // debug received data
if(d.success === 0) {
console.warn('Failed to receive data for org. Code:', d.code, 'Message:', d.msg);
console.debug(d);
return;
}
else if(d.data.html){ // still more members available
// parse to DOM object
dt = $('<div></div');
dt.html(d.data.html);
// (un-)follow all members
$('.nick', dt).each(function(i,field){
changeFollow(field.innerHTML, follow);
});
// load next charge/page of members
changeOrgFollow(sid, follow, page+1);
} else { // else stop the recursion
// wait 1sec and display the "DONE" message
setTimeout(function(){console.log('DONE!')}, 1000);
}
},
data: JSON.stringify({symbol: sid.toUpperCase(), page: page}),
headers: __hs
});
}
@nitzel
Copy link
Author

nitzel commented Aug 28, 2019

Updated the POST's content type to JSON. API stopped defaulting to it.
Nota bene: this script just adds user after user. You'll see warning messages when it fails to do so as it reaches the 250 contacts limit imposed by RSI. Ignore them or change the script to stop after 250 users 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment