Skip to content

Instantly share code, notes, and snippets.

@Archomeda
Last active May 11, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Archomeda/fd776c1484005c95682ae59e48c11e08 to your computer and use it in GitHub Desktop.
Save Archomeda/fd776c1484005c95682ae59e48c11e08 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Gitter Highlight ArenaNet API CDI
// @namespace https://gist.github.com/Archomeda/fd776c1484005c95682ae59e48c11e08
// @version 1.14
// @description Highlights various messages in ArenaNet's API CDI Gitter chat
// @author Archomeda
// @match https://gitter.im/arenanet/api-cdi*
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @downloadURL https://gist.github.com/Archomeda/fd776c1484005c95682ae59e48c11e08/raw/gitter-highlight-anet.user.js
// ==/UserScript==
(function() {
'use strict';
var anet = {
'@Aonwy': 'Ylan Salsbury',
'@apoch': 'Mike Lewis',
'@dsnider-anet': 'Daniel Snider'
};
var anetOther = {
'@cliffspradlin': {
name: 'Cliff Spradlin (former Lead Developer, includes API)',
end: new Date(2013, 8, 1)
},
'@lye': {
name: 'Lawton Campbell (former API Developer)',
end: new Date(2018, 2, 1)
},
'@SCDubs': {
name: 'Stephen Clarke-Willson (Studio Technical Director)'
},
'@StephaneWithAnE_twitter': {
name: 'Stephane Lo Presti (French Community Manager)'
},
'@tivac': {
name: 'Pat Cavit (former Lead Web Developer)',
end: new Date(2017, 5, 5)
}
};
var bots = ['@QuagganBooOOoot'];
var $ = window.jQuery;
addStyles();
var targetNodes = $('#chat-container');
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var myObserver = new MutationObserver(mutationObserved);
var obsConfig = { childList: true, characterData: false, attributes: false, subtree: true };
targetNodes.each(function () {
myObserver.observe(this, obsConfig);
});
function mutationObserved(recs) {
recs.forEach(handleMutation);
}
function handleMutation(rec) {
rec.addedNodes.forEach(nodeAdded);
}
function addStyles() {
$(`<style>
.chat-item__container {
padding-left: 34px;
padding-right: 12px;
margin-left: 0;
margin-right: 0;
}
.chat-item__user-icon {
display: inline-block;
margin-left: 3px;
vertical-align: middle;
}
.anet-hl .chat-item__user-icon,
.anet-other-hl .chat-item__user-icon {
height: 14px;
font-size: 11px;
padding: 3px 25px 1px 2px;
background-position: right 2px center;
background-repeat: no-repeat;
}
.anet-mention .chat-item__user-icon,
.anet-other-mention .chat-item__user-icon {
width: 20px;
height: 12px;
padding: 3px 2px 1px 2px;
background-position: center;
background-repeat: no-repeat;
}
.anet-hl {
background-color: rgba(255, 0, 0, 0.08);
border-right: 2px solid #ff0000 !important;
}
.anet-hl:hover {
background-color: rgba(255, 0, 0, 0.16) !important;
}
.anet-hl .chat-item__user-icon,
.anet-mention .chat-item__user-icon {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAMAAABV0m3JAAAAllBMVEUAAAD////////////////////+/v7////////+//7////+///////////////////////////////////////+///+//7//v7///////////////////////////////////7////+/v7///7///////////////////7+/v7//v7///////7+//7+//7+/v/+/v7////1Ssi7AAAAMXRSTlMA4vPazjgg67NHLyXHxLmnlI14bFk/NBz49vDu5t/VvmBRLAHPsKCchXNhXU9NFhER6SHb9QAAAKVJREFUCNdFjFcSg0AMQ2XKUgOEGiD09O77Xy67JAP6sEZvJGPVZ5yAwjbIX1Aatlq0tXWWUtkTKNJShlrvztd+cPAeTpZhW6UR9aYnhJs97gkC2aiCJBfjMw590uY58zH1hHPZ1LwISV440f6ftOoHvfggrZlDl7u2NAutvHr2uqlnDmDumA2o5RbAhnlnAq6moLVCzmRTDeETUQxMIVGjmjpR8AWG6xZ3FHFGxAAAAABJRU5ErkJggg==");
}
.anet-hl .chat-item__user-icon {
color: #fff;
background-color: #ef3434;
}
.anet-mention .chat-item__user-icon {
background-color: #ef3434;
}
.anet-mention {
background-color: #ef3434;
color: #fff;
}
.anet-other-hl .chat-item__user-icon,
.anet-other-mention .chat-item__user-icon {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAMAAABV0m3JAAAAZlBMVEUAAAD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwD/BwBV0AHPAAAAIXRSTlMA4joe2c4u87P37+t3X09HJcfEuaeUjWxZNBLmvgGgnIW4qDcuAAAAmElEQVQI10WOWQ6DMBBDnT2QlbJTaJv7X7IZWoF/LD/Zo8GtTR+AsIp1F1oGacbJylJFOQuIpamhlfvbh5iwxZdT1jVqDDwLoXkMM3pq9PMqdPRDx8w5L8UuWaTPoy2XUDtpfP6TaX8we1WNnWFfta3mQD9IvgU6lgBev1Cg5QTgUUrDAW0IuhuWCjkN0THGPHAM1akpGeu/KBoPYMLBoGcAAAAASUVORK5CYII=");
}
.anet-other-hl .chat-item__user-icon {
color: #ef3434;
background-color: #fff;
border: 1px solid #ef3434;
}
.anet-other-mention {
color: #ef3434;
}
.bot-hl {
background-color: rgba(0, 100, 200, 0.08);
border-right: 2px solid #0064c8 !important;
}
.bot-hl:hover {
background-color: rgba(0, 100, 200, 0.16) !important;
}
.bot-mention {
background-color: #0064c8;
color: #fff;
}
.anet-hl.unread .chat-item__container,
.bot-hl.unread .chat-item__container {
background-color: rgb(236, 249, 242) !important;
}
.anet-hl.unread.mentioned .chat-item__container,
.bot-hl.unread.mentioned .chat-item__container {
background-color: rgb(251, 243, 235) !important;
}
.anet-hl-unread .chat-item__container,
.bot-hl.unread .chat-item__container {
border-right: none !important;
}
</style>`).appendTo('head');
}
function nodeAdded(node) {
if (!$(node).hasClass('chat-item')) {
return;
}
// Handle author
var $startingNode = $(node);
var user = $('.chat-item__username', $startingNode);
if (user.length) {
if (!user.next().hasClass('chat-item__user-icon')) {
$('<div>').addClass('chat-item__user-icon').insertAfter(user);
}
} else {
do {
$startingNode = $($startingNode).prev();
} while ($startingNode.length > 0 && !$startingNode.hasClass('burstStart'));
user = $('.chat-item__username', $startingNode);
}
user = user.text();
if (user) {
if (isAnet(user)) {
$startingNode.addClass('anet-hl');
$startingNode.nextUntil('.burstStart').addClass('anet-hl');
var $icon = $('.chat-item__user-icon', $startingNode);
$icon.text(anet[user]);
}
if (isAnetOther(user)) {
var messageTime = getMessageDate($startingNode);
if (anetOther[user].end !== undefined && messageTime < anetOther[user].end) {
$startingNode.addClass('anet-hl');
$startingNode.nextUntil('.burstStart').addClass('anet-hl');
} else {
$startingNode.addClass('anet-other-hl');
$startingNode.nextUntil('.burstStart').addClass('anet-other-hl');
}
var $icon = $('.chat-item__user-icon', $startingNode);
$icon.text(anetOther[user].name);
if (anetOther[user].end !== undefined) {
$icon.attr('title', `Until ${anetOther[user].end.toLocaleDateString(undefined, { day: 'numeric', month: 'long', year: 'numeric' })}`);
}
}
if (isBot(user)) {
$startingNode.addClass('bot-hl');
$startingNode.nextUntil('.burstStart').addClass('bot-hl');
}
}
// Handle mentions in message
var mentions = $('.chat-item__text .mention', node);
mentions.each(function () {
var user = $(this).text();
if (isAnet(user)) {
$(this).addClass('anet-mention');
$('<span>').addClass('chat-item__user-icon').appendTo(this);
}
if (isAnetOther(user)) {
var messageTime = getMessageDate($startingNode);
if (anetOther[user].end !== undefined && messageTime < anetOther[user].end) {
$(this).addClass('anet-mention');
} else {
$(this).addClass('anet-other-mention');
}
$('<span>').addClass('chat-item__user-icon').appendTo(this);
}
if (isBot(user)) {
$(this).addClass('bot-mention');
}
});
}
function getMessageDate($node) {
return new Date(parseInt($node.attr('class').split(' ').map(c => c.match(/model-id-([0-9a-f]{8})/i)).find(c => c)[1], 16) * 1000);
}
function isAnet(user) {
return anet[user] !== undefined;
}
function isAnetOther(user) {
return anetOther[user] !== undefined;
}
function isBot(user) {
return bots.some(n => user.includes(n));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment