Skip to content

Instantly share code, notes, and snippets.

@vaultah
Forked from kms70847/common_comments_box.user.js
Created January 17, 2016 09:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vaultah/2f28b84650632e3b9f2a 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 common_comments = [
"It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask].",
"Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named [\"What topics can I ask about here?\"](http://stackoverflow.com/help/on-topic) and [\"What types of questions should I avoid asking?\"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist\](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve).",
"Please consider revising the code sample you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that. -1, don't take it the wrong way. A down vote is how we indicate a content problem around here; improve your formatting and code sample and I'll gladly revert it. Good luck with your code! Personally I think it'd be good to work on {{{list of question specific things that are poorly written.}}}",
"If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. [See here](http://meta.stackexchange.com/a/5235) for a full explanation.",
"Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions.",
"While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion."
];
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){
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 < common_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 = common_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 = "";
console.log(checkboxes);
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 += common_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){
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);
}
}
function register_click_behavior(link){
link.onclick = function(){
var containing_td = first_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);},
100
);
}
}
var first_link = get_comment_links()[0];
register_click_behavior(first_link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment