Skip to content

Instantly share code, notes, and snippets.

@kelunik
Last active August 29, 2015 14:27
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 kelunik/bc8bed513a861af2bc35 to your computer and use it in GitHub Desktop.
Save kelunik/bc8bed513a861af2bc35 to your computer and use it in GitHub Desktop.
GitHub Conversations
// ==UserScript==
// @name GitHub Conversations
// @namespace http://kelunik.com/
// @version 0.4
// @description Provides a next button to jump between conversations in large diffs on GitHub.
// @author Niklas Keller
// @match https://github.com/*/*/commit/*
// @grant none
// ==/UserScript==
var $ = function (str) {
if (str.charAt(0) == "<") {
var fragment = document.createElement("div");
fragment.innerHTML = str;
return fragment.firstChild;
} else {
return document.querySelectorAll(str);
}
};
var conversations = $('.diff-table .line-comments');
if (conversations.length) {
document.body.style.paddingBottom = "50px";
var panelCss = "background-color: #f5f5f5; position: fixed; left: 0; right: 0; bottom: 0; z-index: 2; color: #333; font-weight: bold; border-top: 1px solid #e5e5e5;";
var containerCss = "padding-left: 30px; padding-right: 30px; padding-top: 10px; padding-bottom: 10px; line-height: 27px;";
var panel = $('<div style="' + panelCss + '"><div class="container" style="' + containerCss + '">There are ' + conversations.length + ' conversations on this page.</div></<div>');
var button = $('<button type="button" style="float:right" class="btn btn-sm">next</button>');
button.addEventListener("click", function(e) {
var pos = document.body.scrollTop;
for (var i = 0; i < conversations.length; i++) {
var rect = conversations[i].getBoundingClientRect();
if (rect.top > 10) {
document.body.scrollTop += rect.top;
break;
}
}
});
panel.querySelector("div").appendChild(button);
document.body.appendChild(panel);
window.addEventListener("scroll", function() {
if (conversations[conversations.length - 1].getBoundingClientRect().top < 1) {
button.disabled = true;
} else {
button.disabled = false;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment