2019-09-30 20:55:04 +00:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Stack Exchange Pronoun Assistant
|
|
|
|
// @namespace https://github.com/Glorfindel83/
|
2019-10-01 09:52:09 +00:00
|
|
|
// @description Displays users' pronouns (mentioned in their profiles) in chat
|
|
|
|
// @author Glorfindel
|
|
|
|
// @contributor ArtOfCode
|
2019-09-30 20:55:04 +00:00
|
|
|
// @updateURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/pronoun-assistant/pronoun-assistant.user.js
|
|
|
|
// @downloadURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/pronoun-assistant/pronoun-assistant.user.js
|
2019-10-03 13:33:41 +00:00
|
|
|
// @supportURL https://stackapps.com/questions/8440/pronoun-assistant
|
|
|
|
// @version 1.0
|
|
|
|
// @match *://chat.stackexchange.com/rooms/*
|
|
|
|
// @match *://chat.stackoverflow.com/rooms/*
|
|
|
|
// @match *://chat.meta.stackexchange.com/rooms/*
|
2019-10-01 09:52:09 +00:00
|
|
|
// @grant GM_addStyle
|
2019-09-30 20:55:04 +00:00
|
|
|
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
|
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
/* global $ */
|
|
|
|
|
2019-10-03 13:33:41 +00:00
|
|
|
// NICETOHAVE: on main/meta sites as well; right now, there are very few users
|
|
|
|
// who have indicated their pronouns in their main/meta profile.
|
|
|
|
|
2019-10-01 09:52:09 +00:00
|
|
|
GM_addStyle(`
|
|
|
|
.tiny-signature {
|
|
|
|
display: inline-flex;
|
|
|
|
flex-direction: row-reverse;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.username {
|
|
|
|
height: unset !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.pronouns {
|
|
|
|
color: #777;
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
// List of pronouns to look out for
|
|
|
|
let allPronouns = [
|
2019-09-30 20:55:04 +00:00
|
|
|
"he", "him", "his",
|
|
|
|
"she", "her",
|
2019-10-01 09:52:09 +00:00
|
|
|
"they", "them", "their",
|
2019-09-30 20:55:04 +00:00
|
|
|
"ze", "hir", "zir",
|
|
|
|
"xey", "xem", "xyr"
|
|
|
|
];
|
2019-10-01 09:52:09 +00:00
|
|
|
// Regex to detect instances where users put the pronouns inside parentheses etc.,
|
|
|
|
// e.g. (he/him)
|
2019-09-30 20:55:04 +00:00
|
|
|
let pronounsComponent = /^[^\w\/]*([\w\/]+)[^\w\/]*$/;
|
2019-10-01 09:52:09 +00:00
|
|
|
|
|
|
|
// Keys: user IDs
|
|
|
|
// Values: either a list of DOM elements (specifically, the anchors to chat profiles)
|
|
|
|
// or a string with pronouns.
|
2019-09-30 20:55:04 +00:00
|
|
|
var users = {};
|
2019-10-02 09:15:24 +00:00
|
|
|
// TODO: cache in local storage
|
2019-09-30 20:55:04 +00:00
|
|
|
|
2019-10-02 09:15:24 +00:00
|
|
|
// Adds pronoun information to a user's 'signature' in chat.
|
|
|
|
function showPronouns(element, pronouns) {
|
2019-10-01 09:52:09 +00:00
|
|
|
if (pronouns == "")
|
|
|
|
return;
|
2019-10-02 09:15:24 +00:00
|
|
|
// the element might contain both a tiny and a full signature
|
|
|
|
element.find("div.username").each(function (index, usernameElement) {
|
|
|
|
usernameElement.innerHTML = '<span class="name">' + usernameElement.innerHTML + '</span><br/>'
|
|
|
|
+ '<span class="pronouns">' + pronouns + '</span>';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the user's 'about' in their chat profile for pronouns (see above for the list)
|
|
|
|
// joined by a forward slash.
|
|
|
|
//
|
|
|
|
// Examples of what would be detected:
|
|
|
|
// - "They/them"
|
|
|
|
// - "(he/him/his)"
|
|
|
|
// Examples of what would *not* be detected (yet):
|
|
|
|
// - "they / them"
|
|
|
|
function getPronouns(aboutMe) {
|
|
|
|
for (let component of aboutMe.split(/\s+/)) {
|
|
|
|
if (!component.includes("/"))
|
|
|
|
continue;
|
|
|
|
let match = pronounsComponent.exec(component);
|
|
|
|
if (match == null)
|
|
|
|
continue;
|
|
|
|
var isOnlyPronouns = true;
|
|
|
|
for (let word of match[1].split("/")) {
|
|
|
|
if (!allPronouns.includes(word.toLowerCase())) {
|
|
|
|
isOnlyPronouns = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (isOnlyPronouns)
|
|
|
|
return match[1];
|
|
|
|
}
|
2019-10-03 13:33:41 +00:00
|
|
|
return "";
|
2019-09-30 20:55:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 13:33:41 +00:00
|
|
|
waitForKeyElements("a.signature", function(jNode) {
|
|
|
|
let userID = jNode.attr("href").split("/users/")[1];
|
|
|
|
if (!users.hasOwnProperty(userID)) {
|
|
|
|
users[userID] = [];
|
|
|
|
users[userID].push(jNode);
|
|
|
|
// Read chat profile
|
|
|
|
$.get("https://chat.stackexchange.com/users/thumbs/" + userID + "?showUsage=true", function(data) {
|
|
|
|
let pronouns = data.user_message == null ? "" : getPronouns(data.user_message);
|
|
|
|
users[userID].forEach(function (element) {
|
|
|
|
showPronouns(element, pronouns);
|
2019-09-30 20:55:04 +00:00
|
|
|
});
|
2019-10-03 13:33:41 +00:00
|
|
|
users[userID] = pronouns;
|
|
|
|
});
|
|
|
|
} else if (typeof users[userID] == 'string') {
|
|
|
|
showPronouns(jNode, users[userID]);
|
|
|
|
} else {
|
|
|
|
users[userID].push(jNode);
|
|
|
|
}
|
|
|
|
});
|