Skip to content

Instantly share code, notes, and snippets.

@AlKach
Last active February 1, 2018 09:14
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 AlKach/a900cac3ff4045aa7d0e to your computer and use it in GitHub Desktop.
Save AlKach/a900cac3ff4045aa7d0e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Stackoverflow.Comments.User
// @author Alexander Kachanov
// @include /^https?:\/\/(.*\.)?stackoverflow\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?stackoverflow\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?serverfault\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?serverfault\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?askubuntu\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?askubuntu\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?mathoverflow\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?mathoverflow\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?stackexchange\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?stackexchange\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?superuser\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?superuser\.com/review/.*$/
// @include /^https?:\/\/(.*\.)?stackapps\.com/questions/.*$/
// @include /^https?:\/\/(.*\.)?stackapps\.com/review/.*$/
// ==/UserScript==
(function (window, undefined) {
var get = function(selector, context) {
var nodeList = (context || document).querySelectorAll(selector);
var array = [];
for (var i = 0; i < nodeList.length; i++) {
array.push(nodeList.item(i));
}
return array;
};
var parent = function(element, selector) {
if (!!selector) {
var e = element.parentElement;
while (!!e && !e.matches(selector)) {
e = e.parentElement;
}
return e;
} else {
return element.parentElement;
}
};
var updateCommentsColoring = function() {
var profiles = {}
var addProfile = function(e) {
var userName = e.innerHTML.trim();
profiles[userName] = '';
}
var myProfile = get('a.my-profile');
if (typeof myProfile[0] !== 'undefined' && myProfile[0].tagName === 'A') {
var me = myProfile[0].href.substr(myProfile[0].href.lastIndexOf('/') + 1);
profiles[me] = '';
}
get('.comment-user').forEach(addProfile);
var usersCount = Object.keys(profiles).length;
var deltaAngle = 360 / usersCount,
hue = -deltaAngle,
saturation = 100,
lightness = 75;
Object.keys(profiles).forEach(function(user) {
hue += deltaAngle;
profiles[user] = 'hsl(' + hue + ', ' + saturation + '%, ' + lightness + '%)';
});
get('.comment-body').forEach(function(e) {
var user = get('.comment-user', e)[0].innerHTML.trim(),
comment = get('.comment-actions', parent(e, '.comment'))[0];
e.style.margin = 0;
comment.style.borderLeft = '3px solid ' + profiles[user];
});
get('.comment-score').forEach(function(target) {
target.style.paddingLeft = '5px';
});
};
var watchContainerMutations = function(selector) {
var options = {
childList: true,
subtree: true
};
get(selector).forEach(function(target) {
new MutationObserver(updateCommentsColoring).observe(target, options);
});
};
watchContainerMutations('.comments-container');
watchContainerMutations('.comments');
watchContainerMutations('.review-content');
updateCommentsColoring();
})(window);
@AlKach
Copy link
Author

AlKach commented Jan 13, 2015

Revision 2

  • Переписано без использования jQuery

@AlKach
Copy link
Author

AlKach commented Jan 13, 2015

Revision 3

  • Удалена ненужная функция rgb2hsv

@AlKach
Copy link
Author

AlKach commented Jan 14, 2015

Revision 4

  • Осуществлён переход на цветовую модель HSL
  • Удалены функции конвертации цветов HSV -> RGB -> HEX
  • Исправлен баг с нераскрашивающимися новыми комментариями

@AlKach
Copy link
Author

AlKach commented Jan 14, 2015

Revision 5

  • Добавлено раскрашивание комментов на исследования
  • Удалён вывод отладочных сообщений в консоль

@AlKach
Copy link
Author

AlKach commented Apr 1, 2015

Revision 6

@AlKach
Copy link
Author

AlKach commented Jun 1, 2015

Revision 7

  • Добавлены URL других сайтов сети Stack Exchange
  • Исправлен баг с обработкой забаненых пользователей

@AlKach
Copy link
Author

AlKach commented Feb 1, 2018

Revision 8

  • Внесены исправления для совместимости с новой разметкой SO
  • Комментарии текущего пользователя теперь всегда помечаются красным цветом

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment