Skip to content

Instantly share code, notes, and snippets.

@Seth-Johnson
Last active January 30, 2018 19:10
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Seth-Johnson/7445b149cf980e992e8a to your computer and use it in GitHub Desktop.
Show a site specific profile link in chat
// ==UserScript==
// @name Chat site profiler thingy
// @namespace http://askubuntu.com/users/44179
// @include *chat.stackexchange.com/*
// @include *chat.stackoverflow.com/*
// @include *chat.meta.stackexchange.com/*
// @version 1
// @grant none
// ==/UserScript==
var networkID = "";
var userID = "";
var userBaseSite = "";
var userBaseSiteID = "";
var siteProfile = "";
var mainSiteURL = $('#footer-logo > a').attr('href');
var baseUserURL = mainSiteURL + "/users/";
var mainSiteName = $('#footer-logo > a').attr('title').replace(/&/g, "&");
var json;
function getAssocAccounts(pageNum) {
xhr = new XMLHttpRequest();
xhr.open('GET', "https://api.stackexchange.com/2.2/users/" + networkID + "/associated?page=" + pageNum + "&pagesize=100&filter=!*L2(U8cAI.NDVpWE", false);
xhr.send();
return xhr.response;
}
function findLocalProfile(data) {
for (var item in data.items) {
if (data.items[item].site_name == mainSiteName || data.items[item].site_name == mainSiteName + " Stack Exchange") {
console.log(data.items[item].site_name);
userID = data.items[item].user_id;
siteProfile = "<a href='" + baseUserURL + userID + "'> user profile on this site </a>";
return true;
}
}
return false;
}
function setSiteProfile() {
if (mainSiteName == "The Stack Exchange Network") {
siteProfile = "<a href='" + baseUserURL + networkID + "?tab=accounts" + "'> network profile </a>";
}
else {
var json = JSON.parse(getAssocAccounts(1));
if (findLocalProfile(json)) {
return;
}
json = JSON.parse(getAssocAccounts(2));
if (findLocalProfile(json)) {
return;
}
else {
siteProfile = "<a> user has no profile on site </a>";
}
}
}
function setNetworkID() {
userBaseSite = $('.user-popup').find('div:contains("user profile on") > a').attr('href').split('/')[2].split('.')[0];
userBaseSiteID = $('.user-popup').find("a:contains('user profile')")[1].href.split("/")[4];
xhr = new XMLHttpRequest();
xhr.open('GET', "https://api.stackexchange.com/2.2/users/" + userBaseSiteID + "?order=desc&sort=reputation&site=" + userBaseSite + "&filter=!23IbozPckvHAbU7zwAs_E", false);
xhr.send();
var response = xhr.response;
var json = JSON.parse(response);
networkID = json.items[0].account_id;
console.log(networkID);
}
function doIt() {
if ($('.user-popup').length > 0) {
setNetworkID();
setSiteProfile();
console.log("inserting link");
$('.user-popup').find('div:contains("user profile")').next().eq(0).after(siteProfile);
return;
}
else {
console.log("No popup");
}
}
$('.avatar, #chat').click(function() {
// if it doesn't work initially, try increasing the timeout (default: 300)
window.setTimeout(doIt, 300);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment