Skip to content

Instantly share code, notes, and snippets.

@ZhangYiJiang
Created August 20, 2012 07:24
Show Gist options
  • Save ZhangYiJiang/3401830 to your computer and use it in GitHub Desktop.
Save ZhangYiJiang/3401830 to your computer and use it in GitHub Desktop.
Glitch mass mail delete userscript
// ==UserScript==
// @name Mass delete Glitch mail messages
// @namespace yijiang
// @include http://www.glitch.com/mail/
// @include http://www.glitch.com/mail/all/
// @version 1.04
// ==/UserScript==
function init() {
function deleteMessage (message) {
var containAttachment = 0;
message.forEach(function(id){
var ele = $('#mail-message-' + id);
if (ele.find('.mail-attachments-icon').length) {
containAttachment ++;
return;
}
ele.fadeTo(300, 0.4);
$.post('/mail/' + id + '/delete/', {
done: 1,
'delete': "Delete"
}, function(){
$('#mail-message-' + id).slideUp(150);
});
});
if (containAttachment) {
$('#mail-notification-bar')
.text(containAttachment + ' mail had attachments on them and were thus not deleted.')
.hide()
.fadeIn(300);
}
}
function deleteAuction () {
$('div.mail-snippet a').each(function(){
var mailId = this.getAttribute('href').split('/')[2],
text = $(this).text(),
msg = [];
if (text.match('Auction Fulfillment and Logistics Corporation')) {
msg.push(mailId);
}
deleteMessage(msg);
});
}
function getSelectedMsg () {
return checkboxes.map(function(){
if (this.checked) {
return $(this).parent().attr('data-message-id');
}
}).get();
}
var checkboxes = $('<input>', {
type: 'checkbox',
'class': 'mail-delete-check',
click: function() {
$(this).toggleClass('mail-delete-checked', this.checked);
}
}).appendTo('.mail-message');
var buttonStyle = {
marginTop: '10px',
padding: '6px 11px 5px',
fontSize: '12px',
width: '156px'
};
var deleteSelectedBtn = $('<a>', {
'class': 'button',
text: 'Delete selected messages',
css: buttonStyle,
click: function() {
deleteMessage(getSelectedMsg());
}
}).appendTo('.mail-sidebar');
$('<a>', {
'class': 'button',
text: 'Delete auction messages',
css: buttonStyle,
click: deleteAuction
}).appendTo('.mail-sidebar');
var w = $(window),
lastCallTime = Date.now(),
btnPosition = deleteSelectedBtn.offset().top;
$(window).scroll(function(){
if (Date.now() - lastCallTime > 100) {
lastCallTime = Date.now();
var toggle = (btnPosition - w.scrollTop()) < 20;
deleteSelectedBtn
.toggleClass('fixed-btn', toggle);
}
})
}
function inject () {
var script = document.createElement('script');
script.innerHTML = '(' + init.toString() + ')();';
document.body.appendChild(script);
var style = '.mail-delete-check { display: none; position: absolute; margin-top: 25px; } .mail-message:hover .mail-delete-check, .mail-delete-check.mail-delete-checked { display: block; } .fixed-btn {position: fixed; top: 19px; } ';
var styleEle = document.createElement('style');
styleEle.innerHTML = style;
document.getElementsByTagName('head')[0].appendChild(styleEle);
}
inject();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment