Created
May 16, 2013 22:53
-
-
Save agentlame/5595729 to your computer and use it in GitHub Desktop.
dear greeduch...
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 Multi Ban | |
// @namespace http://www.reddit.com/r/agentlame | |
// @description Universal ban button - <3 greenduch | |
// @include http://www.reddit.com/* | |
// @include http://reddit.com/* | |
// @include http://*.reddit.com/* | |
// @downloadURL http://userscripts.org/scripts/source/167236.user.js | |
// @version 1.4.2 | |
// ==/UserScript== | |
function globalbanbutton() { | |
var buttonName = 'ban'; | |
var saveButton = 'Save'; | |
var cancelButton = 'Cancel'; | |
/////// Don't edit beyond this line. /////// | |
var banSubs = []; | |
var mySubs = []; | |
var modMineURL = 'http://www.reddit.com/subreddits/mine/moderator.json?count=100'; | |
var lastget = JSON.parse(localStorage['Toolbox.cache.lastget'] || -1); | |
var modpage = location.pathname.match(/\/message\/(?:moderator)\/?/); | |
// Because normal .sort() is case sensitive. | |
Array.prototype.saneSort = function () { | |
if (typeof this.sort === 'undefined') return; | |
return this.sort(function (a, b) { | |
if (a.toLowerCase() < b.toLowerCase()) return -1; | |
if (a.toLowerCase() > b.toLowerCase()) return 1; | |
return 0; | |
}); | |
}; | |
if (localStorage['Toolbox.GlobalBan.sublist']) { | |
banSubs = JSON.parse(localStorage['Toolbox.GlobalBan.sublist']); | |
} | |
if (localStorage['Toolbox.cache.moderatedsubs']) { | |
mySubs = JSON.parse(localStorage['Toolbox.cache.moderatedsubs']); | |
} | |
// If it has been more than ten minutes, refresh mod cache. | |
if (mySubs.length < 1 || (new Date().getTime() - lastget) / (1000 * 60) > 10) { | |
mySubs = []; //resent list. | |
getSubs(modMineURL); | |
} else { | |
banSubs.saneSort(); | |
mySubs.saneSort(); | |
// Go! | |
addlinks(); | |
} | |
function getSubs(URL) { | |
$.getJSON(URL, function (json) { | |
getSubsResult(json.data.children, json.data.after); | |
}); | |
} | |
// Callback because reddits/mod/mine is paginated. | |
function getSubsResult(subs, after) { | |
$(subs).each(function (sub) { | |
mySubs.push(this.data.display_name.trim()); | |
}); | |
if (after) { | |
var URL = modMineURL + '&after=' + after; | |
getSubs(URL); | |
} else { | |
// We have all our subs. Start adding ban links. | |
lastget = new Date().getTime(); | |
banSubs.saneSort(); | |
mySubs.saneSort(); | |
// Update the cache. | |
localStorage['Toolbox.cache.moderatedsubs'] = JSON.stringify(mySubs); | |
localStorage['Toolbox.cache.lastget'] = JSON.stringify(lastget); | |
// Go! | |
addlinks(); | |
} | |
} | |
function getUserAndSub(thing) { | |
var banneduser = $(thing).find('.author:first').text(); | |
var currentsub = $('.titlebox h1.redditname a').text(); | |
// Try again. | |
if (!banneduser) { | |
banneduser = $(thing).closest('.entry').find('.author:first').text(); | |
} | |
if (!currentsub) { | |
currentsub = $(thing).parent().find('.subreddit').text(); | |
} | |
// If we still don't have a sub, we're in mod mail | |
if (!currentsub) { | |
currentsub = $(thing).closest('.message-parent').find('.correspondent a:last').text().replace('/r/', ''); | |
} | |
// Not a mod, reset current sub. | |
if ($.inArray(currentsub, mySubs) === -1) { | |
currentsub = ''; | |
} | |
return { | |
'currentsub': currentsub, | |
'banneduser': banneduser | |
}; | |
} | |
// Add ban button to all users | |
function addlinks() { | |
// Not a mod, don't bother. | |
if (mySubs.length < 1) { | |
return; | |
} | |
// If this is a mod page, delay processing and just add the button. | |
if (modpage) { | |
$('.thing .entry').each(function () { | |
if (!$(this).hasClass('ban-button')) { | |
// Add the class so we don't add buttons twice. | |
$(this).addClass('ban-button'); | |
// Defer info gathering until button is clicked. | |
$(this).find('.buttons li:last').before('<li><a href="javascript:;" class="global-ban-button">' + buttonName + '</a></li>'); | |
} | |
}); | |
return; | |
} | |
$('.thing .entry').each(function () { | |
if (!$(this).hasClass('ban-button')) { | |
// Add the class even if we don't add the button. No reason to fail twice. | |
$(this).addClass('ban-button'); | |
var info = getUserAndSub(this); | |
var currentsub = info.currentsub; | |
var banneduser = info.banneduser; | |
// We don't have ban subs saved, and we don't mod the current sub. Screw this; no button. | |
if (banSubs.length < 1 && !currentsub) { | |
return; | |
} | |
// Only add the button if found a user. | |
if (banneduser) { | |
$(this).find('.buttons li:last').before('<li><a href="javascript:;" class="global-ban-button" user="' + banneduser + '" currentsub ="' + currentsub + '">' + buttonName + '</a></li>'); | |
} | |
} | |
}); | |
} | |
// RES NER support. | |
$('div.content').on('DOMNodeInserted', function (e) { | |
if (e.target.parentNode.id && e.target.parentNode.id === 'siteTable' && e.target.className.match(/sitetable/)) { | |
addlinks(); | |
} | |
// Fixes expanding bug in mod mail. | |
if ($(e.target).hasClass('clearleft')) { | |
setTimeout(function () { | |
addlinks(); | |
}, 1000); | |
} | |
}); | |
// Ban button clicked | |
$('body').delegate('.global-ban-button', 'click', function (event) { | |
var banneduser = $(this).attr('user'); | |
var currentsub = $(this).attr('currentsub'); | |
var display = (banSubs.length < 1) ? 'none' : ''; | |
// Information gathering deferred, we're in mod mail. | |
if (!banneduser && !currentsub) { | |
var info = getUserAndSub(this); | |
currentsub = info.currentsub; | |
banneduser = info.banneduser; | |
} | |
// Make box & add subreddit radio buttons | |
var popup = $('\ | |
<div class="ban-popup">\ | |
<div><h2>/u/' + banneduser + '\ | |
<a href="javascript:;" style="display:' + display + '" title="Remove subreddits" class="remove-sub-link right">[-]</a>\ | |
<a href="javascript:;" title="Add subreddits" class="add-sub-link right">[+]</a>\ | |
</h2></div><span>\ | |
<label id="user" style="display:none">' + banneduser + '</label> \ | |
<table><tbody />\ | |
</table>\ | |
<div class="buttons">\ | |
<label class="action-radios" for="type-ban"><input class="ban-type" type="radio" id="type-ban" value="ban" action="friend" name="type-ban" checked>ban</label> / \ | |
<label class="action-radios" for="type-unban"><input class="ban-type" type="radio" id="type-unban" value="unban" action="unfriend" name="type-ban">unban</label> \ | |
<span class="right">\ | |
<button class="save">' + saveButton + '</button>\ | |
<button class="cancel">' + cancelButton + '</button>\ | |
</span>\ | |
<span class="edit-subreddits" style="display:none"><br><br>\ | |
<select class="edit-dropdown"></select><button class="edit-save right"></button></span>\ | |
<div><span class="status error">saving...</span></div>\ | |
<div>\ | |
<div>') | |
.appendTo('body') | |
.css({ | |
left: event.pageX - 50, | |
top: event.pageY - 10, | |
display: 'block' | |
}); | |
// We're a mod of the current sub, add it. | |
if (currentsub) { | |
popup.find('tbody').append('<tr><th><input type="checkbox" class="ban-sub" name="ban-sub" value="' + currentsub + '" id="ban-' + currentsub + '" checked><label for="ban-' + currentsub + '"> /r/' + currentsub + ' (current)</label></th></tr>'); | |
} | |
$(banSubs).each(function () { | |
popup.find('tbody').append('<tr><th><input type="checkbox" class="ban-sub" name="ban-sub" value="' + this + '" id="ban-' + this + '"><label for="ban-' + this + '"> /r/' + this + '</label></th></tr>'); | |
}); | |
return false; | |
}); | |
// 'cancel' button clicked | |
$('body').delegate('.ban-popup .cancel', 'click', function () { | |
$(this).parents('.ban-popup').remove(); | |
}); | |
// 'edit add' button clicked | |
$('body').delegate('.add-sub-link', 'click', function () { | |
var editDropDown = $('.edit-dropdown'); | |
$(editDropDown).find('option').remove().end(); | |
$('.save').hide(); | |
$(mySubs).each(function () { | |
$(editDropDown) | |
.append($('<option>', { | |
value: this | |
}) | |
.text(this)); | |
}); | |
$('.edit-subreddits').show(); | |
$('.edit-save').text('Add'); | |
$('.edit-save').addClass('add-sub'); | |
}); | |
// 'edit remove' button clicked | |
$('body').delegate('.remove-sub-link', 'click', function () { | |
var editDropDown = $('.edit-dropdown'); | |
$(editDropDown).find('option').remove().end(); | |
$('.save').hide(); | |
$(banSubs).each(function () { | |
$(editDropDown) | |
.append($('<option>', { | |
value: this | |
}) | |
.text(this)); | |
}); | |
$('.edit-subreddits').show(); | |
$('.edit-save').text('Remove'); | |
}); | |
// Edit save button clicked. | |
$('body').delegate('.edit-save', 'click', function () { | |
var subname = $('.edit-dropdown option:selected').val(); | |
// If it's in the subs, remove it; if it's not, add it. | |
if ($(this).hasClass('add-sub')) { | |
$(this).removeClass('add-sub'); | |
banSubs.push(subname); | |
} else { | |
banSubs.splice(subname.indexOf(subname), 1); | |
} | |
banSubs.saneSort(); | |
localStorage['Toolbox.GlobalBan.sublist'] = JSON.stringify(banSubs); | |
$(this).parents('.ban-popup').remove(); | |
}); | |
// 'save' button clicked... THIS IS WHERE WE BAN PEOPLE, PEOPLE! | |
$('body').delegate('.ban-popup .save', 'click', function () { | |
var button = $(this), | |
popup = button.parents('.ban-popup'), | |
banType = popup.find('.ban-type:radio:checked').val(), | |
action = popup.find('.ban-type:radio:checked').attr('action'), | |
status = popup.find('.status').show(), | |
subreddits = [], | |
user = popup.find('#user').text(), | |
banCount = 0; | |
// Get dem ban subs. | |
popup.find('.ban-sub:checkbox:checked').each(function () { | |
subreddits.push($(this).val()); | |
}); | |
// Check dem values. | |
if (subreddits.length < 1) return status.text('error, no subreddits selected'); | |
if (!banType) return status.text('error, no action selected'); | |
// Ban dem trolls. | |
var id = setInterval(function () { | |
var sub = $(subreddits).get(banCount); | |
status.text(banType + 'ning /u/' + user + ' from /r/' + sub); | |
$.post('/api/' + action, { | |
uh: reddit.modhash, | |
type: 'banned', | |
name: user, | |
r: sub, | |
api_type: 'json' | |
}); | |
banCount++; | |
if (banCount === subreddits.length) { | |
clearInterval(id); | |
$('.ban-popup').remove(); | |
} | |
}, 1000); //ban tax. | |
}); | |
} | |
// Add scripts/style to page | |
(function () { | |
var css = '\ | |
.ban-popup { max-width:900px;padding:10px 15px;background-color: #FAFAFA;border: 1px solid #369;position:absolute;z-index:10000} \ | |
.ban-popup .right{ float:right }\ | |
.ban-popup .left{ float:left }\ | |
.ban-popup .status{ display:none; }\ | |
.ban-popup .add-sub-link{ text-decoration:none }\ | |
.ban-popup .remove-sub-link{ text-decoration:none }\ | |
.ban-popup tbody tr{ width: 200px; vertical-align:top; border-bottom:1px solid gray;display:block;padding:5px }\ | |
.ban-popup th{ padding-right:10px }\ | |
.ban-popup .buttons{ padding-top:10px }'; | |
// Add CSS | |
var style = document.createElement('style'); | |
style.type = "text/css"; | |
style.textContent = css; | |
document.head.appendChild(style); | |
// Add script | |
var script = document.createElement('script'); | |
script.textContent = "(" + globalbanbutton.toString() + ')();'; | |
document.head.appendChild(script); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment