Created
May 9, 2013 21:26
-
-
Save agentlame/5550748 to your computer and use it in GitHub Desktop.
multibanbuttontest
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 reddit.com/r/agentlame | |
// @description universal ban button - <3 greenduch | |
// @include http://www.reddit.com/* | |
// @include http://reddit.com/* | |
// @include http://*.reddit.com/* | |
// @downloadURL https://github.com/agentlame/multiban/raw/master/multiban.user.js | |
// @version 1.2 | |
// Fixed NER bug. | |
// If can't find name or current sub, don't add button. | |
// Only shows current sub if you're a mod there. | |
// Can name save/cancel buttons | |
// If you don't mod a sub, and have no subs saved: no button. | |
// ==/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'; | |
if (localStorage['Toolbox.GlobalBan.sublist']) { | |
banSubs = JSON.parse(localStorage['Toolbox.GlobalBan.sublist']); | |
} | |
getSubs(modMineURL); | |
function getSubs(URL) { | |
$.getJSON(URL, function (json) { | |
getSubsResult(json.data.children, json.data.after); | |
}); | |
} | |
// Callback because mod/mine is pageanated. | |
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. | |
banSubs.sort(); | |
mySubs.sort(); | |
addlinks(); | |
} | |
} | |
/* | |
( | |
function(a, b) { | |
if (a.toLowerCase() < b.toLowerCase()) return -1; | |
if (a.toLowerCase() > b.toLowerCase()) return 1; | |
return 0; | |
}); | |
*/ | |
// 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')) { | |
var banneduser = $(this).find('.author:first').text(); | |
var currentsub = $('.titlebox h1.redditname a').text(); | |
// Add the class even if we don't add the button. No reason to fail twice. | |
$(this).addClass('ban-button'); | |
if (!currentsub) { | |
currentsub = $(this).parent().find('.subreddit').text(); | |
} | |
// If we still don't have a sub, we're in mod mail | |
if (!currentsub) { | |
currentsub = $(this).closest('.message-parent').find('.correspondent a:last').text().replace('/r/', ''); | |
} | |
// Not a mod, reset current sub. | |
if ($.inArray(currentsub, mySubs) === -1) { | |
currentsub = ''; | |
} | |
// 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(); | |
} | |
}); | |
// Ban button clicked | |
$('body').delegate('.global-ban-button', 'click', function (event) { | |
var banauthor = $(this).attr('user'); | |
var currentsub = $(this).attr('currentsub'); | |
var display = (banSubs.length < 1) ? 'none' : ''; | |
// Make box & add subreddit radio buttons | |
var popup = $('\ | |
<div class="ban-popup">\ | |
<div><h2 class="left"">/u/' + banauthor + '</h2><h2 class="right">\ | |
<a href="javascript:;" style="display:'+ display +'" title="Remove subreddits" class="remove-sub-link">[-]</a>\ | |
<a href="javascript:;" title="Add subreddits" class="add-sub-link">[+]</a>\ | |
</h2></div><span>\ | |
<label id="user" style="display:none">' + banauthor + '</label> \ | |
<table><tbody />\ | |
</table>\ | |
<div class="buttons">\ | |
<label for="type-ban"><input class="ban-type" type="radio" id="type-ban" value="ban" action="friend" name="type-ban" checked>ban</label> / \ | |
<label 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>\ | |
<span class="status error"><br><br>saving...</span>\ | |
<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="radio" class="ban-sub" name="ban-sub" value="' + currentsub + '" id="ban-' + currentsub + '" checked></th><td><label for="ban-' + currentsub + '">/r/' + currentsub + ' (current)</label></td></tr>'); | |
} | |
$(banSubs).each(function () { | |
popup.find('tbody').append('<tr><th><input type="radio" class="ban-sub" name="ban-sub" value="' + this + '" id="ban-' + this + '"></th><td><label for="ban-' + this + '">/r/' + this + '</label></td></tr>'); | |
}); | |
return false; | |
}); | |
// 'cancel' button clicked | |
$('body').delegate('.ban-popup .cancel', 'click', function () { | |
$(this).parents('.ban-popup').remove(); | |
}); | |
// 'edit' button clicked | |
$('body').delegate('.add-sub-link', 'click', function () { | |
var editDropDown = $('.edit-dropdown'); | |
$(editDropDown).find('option').remove().end(); | |
$(mySubs).each(function () { | |
$(editDropDown) | |
.append($('<option>', { | |
value: this | |
}) | |
.text(this)); | |
}); | |
$('.edit-subreddits').show(); | |
$('.edit-save').text('Add'); | |
$('.edit-save').addClass('add-sub'); | |
}); | |
// 'edit' button clicked | |
$('body').delegate('.remove-sub-link', 'click', function () { | |
var editDropDown = $('.edit-dropdown'); | |
$(editDropDown).find('option').remove().end(); | |
$(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.sort(); | |
localStorage['Toolbox.GlobalBan.sublist'] = JSON.stringify(banSubs); | |
$(this).parents('.ban-popup').remove(); | |
}); | |
// 'save' button clicked | |
$('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(), | |
subreddit = popup.find('.ban-sub:radio:checked').val(), | |
user = popup.find('#user').text(); | |
if (!subreddit) return status.text('error, no subreddit selected'); | |
if (!banType) return status.text('error, no action selected'); | |
$.post('/api/' + action, { | |
uh: reddit.modhash, | |
type: 'banned', | |
name: user, | |
r: subreddit, | |
api_type: 'json' | |
}) | |
.done(function () { | |
$('.ban-popup').remove(); | |
}); | |
}); | |
} | |
// Add scripts/style to page | |
(function () { | |
var css = '\ | |
.ban-popup { max-width:500px;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: 250px; 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