Last active
May 22, 2017 13:50
-
-
Save WillSullivan/6732086 to your computer and use it in GitHub Desktop.
Always Be Closing
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 Always Be Closing | |
// @namespace http://statestreetgang.net/ | |
// @version 1.2 | |
// @description Adds easy-to-use buttons for closing crap questions. | |
// @match http*://stackoverflow.com/questions/* | |
// @copyright 2012+, You | |
// ==/UserScript== | |
var container = document.createElement('div'), resultSpan = document.createElement('span'), post, tries = 0; | |
container.appendChild(resultSpan); | |
container.style.marginBottom = '5px'; | |
function getPost() { // gets the post element so we can stick our buttons all up in it | |
return document.getElementsByClassName('post-taglist')[0]; | |
} | |
function getId() { // this is your ID. There are many like it, but actually yours is distinct, so that quote doesn't really apply. | |
return document.location.href.match(/\d+/); | |
}; | |
function button(text, title) { // helper to create each button. Alter as desired. | |
var btn = document.createElement('button'); | |
btn.title = title; | |
btn.innerText = text; | |
btn.style.marginRight = '5px'; | |
return btn; | |
} | |
function offTopicForm(id) { // this creates a form data object used when handling an off topic flag | |
var form = closeFormMe('OffTopic'); | |
form.append('closeAsOffTopicReasonId', id); | |
form.append('offTopicOtherText', 'This question appears to be off-topic because it is not within the bounds of discussion as described in the help center.');// annoying, but you must submit at least 30 characters. Between the arrows are 30 zero width unicode chars you can use to enter whatever you want --><-- oh, and ಠ_ಠ | |
form.append('originalOffTopicOtherText', "I'm voting to close this question as off-topic because"); | |
return form; | |
} | |
function closeFormMe(reason) {// this creates a form data object used when handling other close flags | |
var form = formMe(); | |
form.append('closeReasonId', reason); | |
return form; | |
} | |
function formMe() { // creates our submit form. | |
var formData = new FormData(); | |
formData.append('fkey', StackExchange.options.user.fkey); // fkey is used in every form, as it identifies the user handling the flag. | |
return formData; | |
} | |
function showSuccess() { | |
resultSpan.style.color = 'lime'; | |
resultSpan.innerText = "ABC'd"; | |
} | |
function showFail() { | |
resultSpan.style.color = 'red'; | |
resultSpan.innerText = "Nope.jpg"; | |
} | |
function postMuhForm(url, form) { // posts muh form | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', url, true); | |
xhr.onload = function (e) { | |
if (this.status === 200) { | |
var result = JSON.parse(this.response); | |
if (result.Success) { | |
showSuccess(); | |
} else { | |
showFail(); | |
console.log(result); | |
} | |
} | |
else { | |
showFail(); | |
console.log(e.responseText); | |
console.log(e); | |
} | |
}; | |
xhr.send(form); | |
} | |
function offTopicButton(text, title, reasonId) {// Basic off topic button creator. element is the parent into which we add the button, the button text and title, and the reasonId for the form. | |
innerButton(text, title, offTopicForm(reasonId)); | |
} | |
function closeQuestionButton(text, title, reason) {// same as above, but for other reasons not "off topic" | |
innerButton(text, title, closeFormMe(reason)); | |
} | |
function innerButton(text, title, form) {// support for the above two | |
var muhButton = button(text, title); | |
muhButton.onclick = function () { | |
postMuhForm('/flags/questions/' + getId() + '/close/add', form); | |
}; | |
container.insertBefore(muhButton, container.firstChild); | |
} | |
function insertAfter(newNode, referenceNode) {// props http://stackoverflow.com/a/4793630/1228 | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); | |
} | |
function apply() { // here we try to insert our buttons into the post, with some jiggery pokery to get around timing issues and stuff | |
post = getPost(); | |
if (post) { | |
insertAfter(container, post); | |
} else { | |
if (document.readyState != 'complete') { | |
window.addEventListener("load", apply); | |
} else if (tries++ < 3) { | |
setTimeout(apply, 1000); // face facts, I'm just cargo-culting here. | |
} | |
} | |
} | |
// Off topic, server fault question | |
offTopicButton('SF', 'Server question', 7); | |
// off topic, super user question | |
offTopicButton('SU', 'SuperUser', 4); | |
// off topic, we expect you to tell us what you tried and what you did | |
offTopicButton('NE', 'No effort', 13); | |
// off topic, you're asking for links or libraries | |
offTopicButton('LIB', 'Libraries, links', 8); | |
// off topic, it's a simple bug and is already fixed | |
offTopicButton('TL', 'Typo', 11); | |
// close as Unclear | |
closeQuestionButton('UC', 'Unclear', 'Unclear'); | |
// close as Too broad | |
closeQuestionButton('TB', 'Too Broad', 'Toobroad'); | |
// close as Opinion | |
closeQuestionButton('OB', 'Opinion based', 'OpinionBased'); | |
// off topic, we expect you to tell us what you tried and what you did | |
offTopicButton('OT', 'Not programming related', 3); | |
// wish us luck! | |
apply(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to remove all that jquery nonsense and do some cleanup