Created
August 5, 2014 19:52
-
-
Save danott/5fd7f15438938c4a168e to your computer and use it in GitHub Desktop.
GitHub Labels
This file contains 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 GitHub Labels | |
// @namespace http://danott.co/ | |
// @version 0.1 | |
// @description Automatically add and remove "+1", "+2", and "NOT READY!" labels bast on comment contents. Included on all of github because pushState. | |
// @include https://github.com/* | |
// @copyright 2014+, @danott | |
// ==/UserScript== | |
var getLabelName = function() { | |
return jQuery(this).data('name'); | |
} | |
var addLabel = function(name) { | |
var node = jQuery(".sidebar-labels .select-menu-item:not(.selected) [data-name=\"" + name + "\"]"); | |
showLabelMenu(); | |
node.trigger('click'); | |
hideLabelMenu(); | |
} | |
var removeLabel = function(name) { | |
var node = jQuery(".sidebar-labels .select-menu-item.selected [data-name=\"" + name + "\"]"); | |
showLabelMenu(); | |
node.trigger('click'); | |
hideLabelMenu(); | |
} | |
var toggleLabelMenu = function() { | |
jQuery(".sidebar-labels .octicon-gear").trigger("click"); | |
} | |
var isLabelMenuVisible = function() { | |
return jQuery(".sidebar-labels .select-menu-modal-holder:visible").length > 0; | |
} | |
var showLabelMenu = function() { | |
if (!isLabelMenuVisible()) { | |
toggleLabelMenu(); | |
} | |
} | |
var hideLabelMenu = function() { | |
if (isLabelMenuVisible()) { | |
toggleLabelMenu(); | |
} | |
} | |
var emojiPlusOnes = function() { | |
return jQuery(".comment-body:visible:has(.emoji[title=':+1:'])").length; | |
} | |
var rawPlusOnes = function() { | |
return jQuery(".comment-body:visible:contains('+1')").length; | |
} | |
var plusOnesReceived = function() { | |
return emojiPlusOnes() + rawPlusOnes(); | |
} | |
var applyPlusOnesLabel = function() { | |
switch (plusOnesReceived()) { | |
case 0: | |
removeLabel("+1"); | |
removeLabel("+2"); | |
break; | |
case 1: | |
addLabel("+1"); | |
removeLabel("+2"); | |
break; | |
default: | |
removeLabel("+1"); | |
addLabel("+2"); | |
} | |
} | |
var hasNotReadyComment = function() { | |
return jQuery(".comment-body:visible:contains('NOT READY')").length > 0; | |
} | |
var applyNotReadyLabel = function() { | |
if (hasNotReadyComment()) { | |
addLabel("NOT READY!"); | |
} else { | |
removeLabel("NOT READY!"); | |
} | |
} | |
jQuery(function(){ | |
jQuery(document).on('ajaxComplete', function() { | |
window.setTimeout(applyPlusOnesLabel, 1); | |
window.setTimeout(applyNotReadyLabel, 1); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment