Last active
February 1, 2018 09:14
-
-
Save AlKach/a900cac3ff4045aa7d0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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); |
Revision 8
- Внесены исправления для совместимости с новой разметкой SO
- Комментарии текущего пользователя теперь всегда помечаются красным цветом
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 7