Last active
August 28, 2019 22:15
-
-
Save nitzel/590a97ecbdb2c8256fff to your computer and use it in GitHub Desktop.
Follow/Unfollow all members of a Star Citizen organization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the
POST
's content type toJSON
. 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 byRSI
. Ignore them or change the script to stop after 250 users 👍