Pronoun Assistant v0.2

This commit is contained in:
Glorfindel 2019-10-01 11:52:09 +02:00
parent 0c8a516e70
commit d41b2d74f8
1 changed files with 49 additions and 19 deletions

View File

@ -1,10 +1,12 @@
// ==UserScript== // ==UserScript==
// @name Stack Exchange Pronoun Assistant // @name Stack Exchange Pronoun Assistant
// @namespace https://github.com/Glorfindel83/ // @namespace https://github.com/Glorfindel83/
// @description Displays users' pronouns // @description Displays users' pronouns (mentioned in their profiles) in chat
// @author Glorfindel
// @contributor ArtOfCode
// @updateURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/pronoun-assistant/pronoun-assistant.user.js // @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 // @downloadURL https://raw.githubusercontent.com/Glorfindel83/SE-Userscripts/master/pronoun-assistant/pronoun-assistant.user.js
// @version 0.1 // @version 0.2
// @match *://*.stackexchange.com/* // @match *://*.stackexchange.com/*
// @match *://*.stackoverflow.com/* // @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/* // @match *://*.superuser.com/*
@ -18,26 +20,51 @@
// @exclude *://elections.stackexchange.com/* // @exclude *://elections.stackexchange.com/*
// @exclude *://openid.stackexchange.com/* // @exclude *://openid.stackexchange.com/*
// @exclude *://stackexchange.com/* // @exclude *://stackexchange.com/*
// @grant none // @grant GM_addStyle
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript== // ==/UserScript==
/* global $ */ /* global $ */
let pronouns = [ 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 = [
"he", "him", "his", "he", "him", "his",
"she", "her", "she", "her",
"they", "them", "they", "them", "their",
"ze", "hir", "zir", "ze", "hir", "zir",
"xey", "xem", "xyr" "xey", "xem", "xyr"
]; ];
// Regex to detect instances where users put the pronouns inside parentheses etc.,
// e.g. (he/him)
let pronounsComponent = /^[^\w\/]*([\w\/]+)[^\w\/]*$/; let pronounsComponent = /^[^\w\/]*([\w\/]+)[^\w\/]*$/;
// Keys: user IDs
// Values: either a list of DOM elements (specifically, the anchors to chat profiles)
// or a string with pronouns.
var users = {}; var users = {};
function addInformation(element, information) { function addPronouns(element, pronouns) {
if (information != "") { if (pronouns == "")
element.append($("<span>" + information + "</span>")); return;
} let usernameElement = element.find("div.username")[0];
usernameElement.innerHTML = '<span class="name">' + usernameElement.innerHTML + '</span><br/>'
+ '<span class="pronouns">' + pronouns + '</span>';
} }
if (window.location.host.startsWith("chat.")) { if (window.location.host.startsWith("chat.")) {
@ -47,7 +74,7 @@ if (window.location.host.startsWith("chat.")) {
users[userID] = []; users[userID] = [];
users[userID].push(jNode); users[userID].push(jNode);
$.get("https://chat.stackexchange.com/users/thumbs/" + userID + "?showUsage=true", function(data) { $.get("https://chat.stackexchange.com/users/thumbs/" + userID + "?showUsage=true", function(data) {
var information = ""; var pronouns = "";
if (data.user_message != null) { if (data.user_message != null) {
// Check the user's 'about' in their chat profile for pronouns (see above for the list) // Check the user's 'about' in their chat profile for pronouns (see above for the list)
// joined by a forward slash. // joined by a forward slash.
@ -60,26 +87,29 @@ if (window.location.host.startsWith("chat.")) {
for (let component of data.user_message.split(/\s+/)) { for (let component of data.user_message.split(/\s+/)) {
if (!component.includes("/")) if (!component.includes("/"))
continue; continue;
var allPronouns = true; let match = pronounsComponent.exec(component);
for (let word of pronounsComponent.exec(component)[1].split("/")) { if (match == null)
if (!pronouns.includes(word.toLowerCase())) { continue;
allPronouns = false; var isAllPronouns = true;
for (let word of match[1].split("/")) {
if (!allPronouns.includes(word.toLowerCase())) {
isAllPronouns = false;
break; break;
} }
}; };
if (allPronouns) { if (isAllPronouns) {
information = component; pronouns = component;
break; break;
} }
} }
} }
users[userID].forEach(function (element) { users[userID].forEach(function (element) {
addInformation(element, information); addPronouns(element, pronouns);
}); });
users[userID] = information; users[userID] = pronouns;
}); });
} else if (typeof users[userID] == 'string') { } else if (typeof users[userID] == 'string') {
addInformation(jNode, users[userID]); addPronouns(jNode, users[userID]);
} else { } else {
users[userID].push(jNode); users[userID].push(jNode);
} }