Created
May 19, 2013 21:19
-
-
Save agentlame/5609048 to your computer and use it in GitHub Desktop.
I'm firin' mah foxes
This file contains hidden or 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.9 | |
| // ==/UserScript== | |
| function globalbanbutton() { | |
| var buttonName = 'ban'; | |
| var saveButton = 'Save'; | |
| var cancelButton = 'Cancel'; | |
| /////// Don't edit beyond this line. /////// | |
| var OTHER = 'other-sub'; | |
| 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); | |
| // 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).closest('.entry').find('.subreddit').text(); | |
| } | |
| if (!currentsub) { | |
| currentsub = $(thing).closest('.thing').find('.subreddit').text(); | |
| } | |
| // If we still don't have a sub, we're in mod mail | |
| if (!currentsub) { | |
| currentsub = $(thing).closest('.entry').find('.head a:last').text().replace('/r/', '').replace('/', '').trim(); | |
| } | |
| // 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; | |
| } | |
| $('.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>'); | |
| } | |
| }); | |
| } | |
| // FIX ME! | |
| // 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 benbutton = event.target; //huehuehue | |
| $(benbutton).text('loading...'); | |
| var display = (banSubs.length < 1) ? 'none' : ''; | |
| var shownuke = (JSON.parse(localStorage["Toolbox.GlobalBan.nukebutton"] || "false")) ? '' : 'none'; | |
| var info = getUserAndSub(this); | |
| var currentsub = info.currentsub; | |
| var banneduser = info.banneduser; | |
| if (!banneduser || banneduser === '[deleted]') { | |
| $(benbutton).text('error'); | |
| $(benbutton).css('color', 'red'); | |
| return; | |
| } | |
| // Make box & add subreddit radio buttons | |
| var popup = $('\ | |
| <div class="ban-popup">\ | |
| <div><h2><label class="action-title">/u/' + banneduser + '</label>\ | |
| <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>\ | |
| <span> </span><a href="javascript:;" style="display:' + shownuke + '" title="Nuke User (global ban)" class="nuke-button right">[N]</a>\ | |
| </h2></div><span>\ | |
| <label id="user" style="display:none">' + banneduser + '</label> \ | |
| <table><tbody class="subs-body" />\ | |
| </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>\ | |
| <div class="edit-subreddits buttons" style="display:none"><br>\ | |
| <select class="edit-dropdown left"></select><button class="edit-save right"></button>\ | |
| <p style="clear:both"><label class="nuke-label" for="the-nuclear-option">\ | |
| <input class="the-nuclear-option" type="checkbox" id="the-nuclear-option" name="the-nuclear-option"> enable nuke user (global ban) button.</label></p></div>\ | |
| <div><span class="status error left"><br>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>'); | |
| }); | |
| // add all our subs. | |
| popup.find('tbody').append('<tr><th><input type="checkbox" class="ban-sub" name="ban-sub" id="' + OTHER + '-checkbox" value="' + OTHER + '">\ | |
| <select class="' + OTHER + '" for="ban-' + OTHER + '"><option value="' + OTHER + '">(select subreddit)</option></select></th></tr>'); | |
| $(mySubs).each(function () { | |
| $('.' + OTHER) | |
| .append($('<option>', { | |
| value: this | |
| }) | |
| .text('/r/' + this)); | |
| }); | |
| // custom sub changed. | |
| $('.' + OTHER).change(function () { | |
| $('#' + OTHER + '-checkbox').prop('checked', ($(this).val() !== OTHER)); | |
| }); | |
| // reset button name. | |
| $(benbutton).text(buttonName); | |
| 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(); | |
| $('.save').hide(); | |
| $('.action-radios').hide(); | |
| $('.action-title').text('add sub'); | |
| $('.subs-body').hide(); | |
| $(mySubs).each(function () { | |
| $(editDropDown) | |
| .append($('<option>', { | |
| value: this | |
| }) | |
| .text('/r/' + 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(); | |
| $('.save').hide(); | |
| $('.action-radios').hide(); | |
| $('.action-title').text('remove sub'); | |
| $('.subs-body').hide(); | |
| $(banSubs).each(function () { | |
| $(editDropDown) | |
| .append($('<option>', { | |
| value: this | |
| }) | |
| .text('/r/' + 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); | |
| // Enable/diable global ban button. | |
| localStorage['Toolbox.GlobalBan.nukebutton'] = JSON.stringify($('.the-nuclear-option').is(':checked')); | |
| $(this).parents('.ban-popup').remove(); | |
| }); | |
| // 'save' button clicked... THIS IS WHERE WE BAN PEOPLE, PEOPLE! | |
| $('body').delegate('.ban-popup .save, .nuke-button', 'click', function () { | |
| //var button = $(this), | |
| var popup = $('.ban-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; | |
| console.log(this); | |
| if (!$(this).hasClass('nuke-button')) { | |
| // Get dem ban subs. | |
| popup.find('.ban-sub:checkbox:checked').each(function () { | |
| if ($(this).val() !== OTHER) { | |
| subreddits.push($(this).val()); | |
| } else { | |
| var subname = $('.' + OTHER + ' option:selected').val(); | |
| if (subname !== OTHER) { | |
| subreddits.push(subname); | |
| } | |
| } | |
| }); | |
| } else { | |
| var confirmban = confirm("This will " + banType + " /u/" + user + " from every subreddit you moderate. Are you sure?"); | |
| if (confirmban) { | |
| subreddits = mySubs; | |
| } | |
| } | |
| // 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); | |
| console.log(banCount + ': ' + 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(); | |
| } | |
| }, 500); //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 .nuke-button { text-decoration:none }\ | |
| .ban-popup tbody tr{ width: 225px; vertical-align:top; border-bottom:1px solid gray;display:block;padding:5px }\ | |
| .ban-popup th{ padding-right:10px }\ | |
| .ban-popup .buttons{ padding-top:10px }\ | |
| .ban-popup .nuke-button {color: red}\ | |
| .ban-popup .edit-subreddits {width: 260px}\ | |
| .ban-popup .edit-dropdown {width: 190px}\ | |
| '; | |
| // 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