Skip to content

Instantly share code, notes, and snippets.

@fredley
Forked from WillSullivan/AlwaysBeClosing
Last active August 29, 2015 14:03
Show Gist options
  • Save fredley/332ea76e5629f00ba513 to your computer and use it in GitHub Desktop.
Save fredley/332ea76e5629f00ba513 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Always Be Closing Dupes Everywhere
// @version 0.1
// @description ABC
// @include http://stackoverflow.com/questions/*
// @include http://stackoverflow.com/review/close/*
// @include http://*.stackexchange.com/questions/*
// @include http://*.stackexchange.com/review/close/*
// @copyright No
// ==/UserScript==
function exec(fn) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + fn + ')();';
document.body.appendChild(script);
document.body.removeChild(script);
}
window.addEventListener("load", function () {
exec(function () {
function GetId()
{
return document.location.href.match(/\d+/);
}
// this is the button definition
var txt = '{txt}', tit = '{title}', btn = '<button style="margin-right:10px" title="' + tit + '">' + txt + '</button>';
// this creates the button
function Button(text, title){
return btn.replace(txt, text).replace(tit, title);
}
// this creates a form data object used when handling an off topic flag
function OTForm(id){
var form = CloseFormMe('OffTopic');
form.closeAsOffTopicReasonId = id;
form.offTopicOtherText = 'This question appears to be off-topic because it is not within the bounds of discussion as described in the help center.';
return form;
}
// this is used in every form. identifies the mod handling the flag.
function FormMe(){
return { fkey: StackExchange.options.user.fkey, };
}
// this creates a form data object used when handling other close flags
function CloseFormMe(reason){
var form = FormMe();
form.closeReasonId = reason;
return form;
}
function Success(){
$('#btnresult').css('background', 'lime').text("ABC'd");
}
function Fail(){
$('#btnresult').css('background', 'red').text("Nope.jpg");
}
// just a simple ajax submit, fire and forget, no need to execute anything on completion
function PostAndForget(url, form, onsuccess, onfail){
$.ajax({
type: "POST",
url: url,
data: form,
error: onfail,
success: onsuccess
});
}
// 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.
function OffTopicButton(element, text, title, reasonId){
element.prepend($(Button(text, title))).click(function ()
{
var id = GetId(), form = OTForm(reasonId);
PostAndForget("/flags/questions/{id}/close/add".replace("{id}", id), form,
Success,
Fail);
});
}
// same as above, but for other reasons not "off topic"
function CloseQuestionButton(element, text, title, reason){
element.prepend($(Button(text, title))).click(function ()
{
var id = GetId(), form = CloseFormMe(reason);
PostAndForget("/flags/questions/{id}/close/add".replace("{id}", id), form,
Success,
Fail);
});
}
// off topic, we expect you to tell us what you tried and what you did
function NE(element)/**/
{
OffTopicButton(element, 'NE', 'No effort', 6);
}
// off topic, forgot which one this is, but its the one that most closely resembled Too Localized.
function OTTL(element){/**/
OffTopicButton(element, 'TL', 'fix my website, NPR', 5);
}
// close as Unclear
function UC(element)/**/
{
CloseQuestionButton(element, 'UC', 'Unclear', 'Unclear');
}
// close as Too broad
function TB(element)/**/
{
CloseQuestionButton(element, 'TB', 'Too Broad', 'Toobroad');
}
// close as Opinion
function OB(element){/**/
CloseQuestionButton(element, 'OB', 'Opinion based', 'OpinionBased');
}
// off topic, we expect you to tell us what you tried and what you did
function OT(element)/**/
{
OffTopicButton(element, 'OT', 'Off Topic', 3);
}
var container = $('<div><span id="btnresult" /></div>');
if($('.review-bar-anchor').length)
{
$('.review-bar-anchor').after(container);
}else{
$('.post-taglist').after(container);
}
NE(container);
OTTL(container);
UC(container);
TB(container);
OB(container);
OT(container);
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment