Skip to content

Instantly share code, notes, and snippets.

@kms70847
Last active November 23, 2016 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kms70847/b600792c78b0790c3c4d to your computer and use it in GitHub Desktop.
Save kms70847/b600792c78b0790c3c4d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Common Comments Box (Stack Overflow)
// @namespace about:blank
// @include http*://stackoverflow.com/questions/*
// @version 1
// @grant none
// ==/UserScript==
var question_comments = [
"Are you getting an error? If so, what is the error and stack trace? Is your code running, but producing unexpected output? If so, what output are you getting, and what output did you expect to get?",
"Something went wrong with your code formatting. Consult [Markdown help - Code and Preformatted Text](http://stackoverflow.com/editing-help#code) and [edit] your post."
];
var answer_comments = [
"Glad to have been of help! Feel free to [accept my answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answe‌​r-work) if you feel it was useful to you. :-)"
];
function getElementsByExactClassName(element, name){
var candidates = element.getElementsByClassName(name);
var ret = [];
for(var i = 0; i < candidates.length; i+=1){
if (candidates[i].className.trim() == name){
ret.push(candidates[i]);
}
}
return ret;
}
function get_comment_links(){
return getElementsByExactClassName(document, "js-add-link comments-link");
}
function create_common_popup(form, comments){
var box = document.createElement("div");
box.className = "popup";
box.style.width = "800px";
box.style.display = "block";
box.style.left = "calc(50% - 416px)";
box.style.position = "fixed";
box.style.top = "159.5px";
var checkboxes = [];
for(var i = 0; i < comments.length; i+=1){
var input = document.createElement("input");
input.type = "checkbox";
checkboxes.push(input);
box.appendChild(input);
var text = document.createElement("span");
text.innerHTML = comments[i];
box.appendChild(text);
box.appendChild(document.createElement("br"));
}
button = document.createElement("input");
button.type = "button";
button.value = "Add";
button.onclick = function(){
var text_to_add = "";
for(var i = 0; i < checkboxes.length; i+=1){
if (checkboxes[i].checked){
if (text_to_add.length > 0){
text_to_add += " ";
}
text_to_add += comments[i];
}
}
var textarea = form.getElementsByTagName("TEXTAREA")[0];
textarea.value = text_to_add;
box.parentNode.removeChild(box);
textarea.focus();
}
box.appendChild(button);
document.body.appendChild(box);
}
function create_common_link(form, comments){
var button_title = "common";
//don't create the link if it already exists. This can happen if the user clicked "add comment" more than once.
var existing_buttons = form.getElementsByTagName("A");
for (var i = 0; i < existing_buttons.length; i+=1){
if (existing_buttons[i].innerHTML == button_title){
return;
}
}
var add_comment_button = form.getElementsByTagName("INPUT")[0];
var link = document.createElement("a");
link.innerHTML = button_title;
link.className = "comment-help-link";
add_comment_button.parentNode.insertBefore(link, add_comment_button.nextSibling);
link.onclick = function(){
create_common_popup(form, comments);
}
}
function register_click_behavior(link, comments){
link.onclick = function(){
var containing_td = link.parentNode.parentNode;
var form = containing_td.getElementsByClassName("comment-form")[0];
//wait for the page's native onclick trigger to finish, or else the form elements won't be present
setTimeout(
function(){
create_common_link(form, comments);
},
100
);
}
}
var links = get_comment_links();
for (var i = 0; i < links.length; i +=1){
register_click_behavior(links[i], i == 0 ? question_comments : answer_comments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment