* Added date filter
* Added export action
* Made the processing prettier.
This commit is contained in:
j0be 2017-02-08 04:47:21 -05:00 committed by GitHub
parent 725fcef486
commit 277b73f7fd

View file

@ -1,5 +1,5 @@
var pd = {
version: '1.3.1',
version: '1.4.0',
bookmarkver: '1.1',
init : function() {
pd.checks.versions();
@ -98,6 +98,9 @@ var pd = {
if ($('#pd__stlye').html() === '') {
$(this).hide();
}
if (pd.debugging) {
$(this).find('.debugging').removeClass('debugging');
}
$(this).find('h2').first().text('Power Delete Suite v'+pd.version);
pd.setup.applySubList();
pd.setup.bindUI();
@ -125,6 +128,8 @@ var pd = {
},
createProcessStream: function () {
window.pd_processing = true;
pd.exportItems = [];
pd.exportIds = [];
pd.task = {
after: '',
info: {
@ -143,24 +148,27 @@ var pd = {
deleted: 0,
errors: 0,
ignored: 0,
exported: 0,
ignoreReasons: {
subs: 0,
gold: 0,
saved: 0,
mod: 0,
score: 0,
date: 0,
}
},
config: {
isExporting: $('#pd__export').is(':checked'),
isRemovingPosts: $('#pd__submissions').is(':checked'),
isRemovingComments: $('#pd__comments').is(':checked'),
isEditing: $('#pd__comments-edit').is(':checked'),
editText: $('#pd__comments-edit-text').val(),
},
paths: {
sections: $('#pd__submissions').is(':checked') ?
['comments','submissions','search'] :
['comments','search','submissions'], /* Search is actually more efficient than submissions if we're not handling submissions (`self:1`) */
sections: ! $('#pd__submissions').is(':checked') && ! $('#pd__export').is(':checked') ?
['comments','search','submissions'] : /* Search is actually more efficient than submissions if we're not handling submissions (`self:1`) */
['comments','submissions','search'],
sorts : ['new','hot','top','controversial']
}
};
@ -174,6 +182,11 @@ var pd = {
gt: $('#pd__score-dirtoggle').is(':checked'),
num: parseFloat($('#pd__score-num').val())
},
date: {
enabled: $('#pd__date').is(':checked'),
gt: $('#pd__date-dirtoggle').is(':checked'),
num: Math.floor((new Date).getTime()/1000)-(parseFloat($('#pd__date-num').val())*60)
},
gilded: $('#pd__gilded').is(':checked'),
saved: $('#pd__saved').is(':checked'),
mod: $('#pd__mod').is(':checked'),
@ -206,13 +219,15 @@ var pd = {
});
$('.gt-toggle').change(function() {
var greaterThan = $(this).hasClass('greater');
$(this).next('label').text(greaterThan ? 'Less than' : 'Greater than');
$(this).attr('class','gt-toggle hidden ' + (greaterThan ? 'less' : 'greater'));
});
$('.num-only').blur(function() {
$(this).val($(this).val().replace(/[^\d-]/g,''));
$(this).change();
});
$('.pd__insert').click(function() {
$($(this).attr('data-target')).val($(this).attr('data-value')).change();
});
},
},
helpers: {
@ -221,7 +236,7 @@ var pd = {
return {valid:false,reason:'Please enter something to edit your comments / self posts to.'};
} else if (pd.filters.score && $('#pd_score-num').val() === '') {
return {valid:false,reason:'Please enter a score to filter with.'};
} else if (!(pd.task.config.isRemovingPosts || pd.task.config.isEditing || pd.task.config.isEditing)) {
} else if (!(pd.task.config.isRemovingPosts || pd.task.config.isEditing || pd.task.config.isEditing || pd.task.config.isExporting)) {
return {valid:false,reason:'There are no actions chosen, so we\'ve got nothing to do. Please select an action.'};
}
return {valid:true,reason:'valid'};
@ -237,12 +252,24 @@ var pd = {
(pd.filters.score.enabled && (
(pd.filters.score.gt === true && parseFloat(item.data.score) > pd.filters.score.num) ||
(pd.filters.score.gt === false && parseFloat(item.data.score) < pd.filters.score.num)
)),
date: !pd.filters.date.enabled ||
(pd.filters.date.enabled && (
(pd.filters.date.gt === true && parseFloat(item.data.created_utc) > pd.filters.date.num) ||
(pd.filters.date.gt === false && parseFloat(item.data.created_utc) < pd.filters.date.num)
))
};
for(var key in check) {if (! check[key]) {
pd.task.info.ignoreReasons[key] ++;
pd.task.items[0].pdIgnoreReasons = check;
}}
return check.subs && check.gold && check.saved && check.mod && check.score;
return check.subs && check.gold && check.saved && check.mod && check.score && check.date;
},
csvEscape: function(str) {
return str.replace(/'/g,'"').replace(/"/g,'""');
},
csvCell: function(str) {
return '"'+str+'",';
},
getSettings: function() {
return localStorage.getItem('pd_storage') ? JSON.parse(localStorage.getItem('pd_storage')) : false;
@ -278,6 +305,9 @@ var pd = {
actions: {
page: {
next: function() {
if (pd.debugging && pd.task.info.donePages % 5 == 3) {
pd.actions.page.shift();
}
if (pd.task.paths.sections.length > 0) {
pd.ui.updateDisplay();
pd.actions.page.handle();
@ -298,17 +328,17 @@ var pd = {
url: pd.endpoints[pd.task.paths.sections[0]],
data: {
q: pd.task.paths.sections[0] == 'search' ?
'author:'+pd.config.user + (! pd.task.isRemovingPosts ? ' self:1' : '') :
'author:'+pd.config.user + (! pd.task.config.isRemovingPosts && !pd.task.config.isExporting ? ' self:1' : '') :
null,
after: pd.task.after,
sort: pd.task.paths.sorts[0]
sort: pd.task.paths.sorts[0],
t: 'all',
}
}).then(function(resp) {
if (resp.data) {
var children = resp.data.children;
pd.task.info.donePages ++;
if (children.length > 0) {
pd.task.info.numPages ++;
pd.task.info.doneItems = 0;
pd.task.info.numItems = children.length;
pd.task.items = children;
@ -348,9 +378,18 @@ var pd = {
}
},
handleSingle: function () {
var item = pd.task.items[0];
pd.ui.updateDisplay();
if (pd.helpers.shouldBeActedOn(item)) {
var item = pd.task.items[0],
shouldBeActedOn = pd.helpers.shouldBeActedOn(item),
earlyExitNewItems = pd.task.paths.sorts[0] == 'new' && pd.filters.date.gt === true && pd.task.items[0].pdIgnoreReasons && !pd.task.items[0].pdIgnoreReasons.date;
if (earlyExitNewItems) {
console.log('Skipping the rest of the things sorted by new');
pd.task.items[0].pdIgnored = true;
pd.actions.children.finishItem();
pd.actions.page.shift();
pd.actions.page.next();
} else if (shouldBeActedOn) {
if (!item.pdEdited && ((item.data.is_self || item.kind == 't1') && pd.task.config.isEditing)) {
pd.actions.edit(item);
} else if (!item.pdDeleted && ((item.kind == 't3' && pd.task.config.isRemovingPosts) || (item.kind == 't1' && pd.task.config.isRemovingComments))) {
@ -371,7 +410,34 @@ var pd = {
pd.task.info.deleted += pd.task.items[0].pdDeleted ? 1 : 0;
pd.task.info.edited += pd.task.items[0].pdEdited ? 1 : 0;
pd.task.info.ignored += pd.task.items[0].pdIgnored ? 1 : 0;
if (pd.task.config.isExporting && !pd.task.items[0].pdIgnored) {
pd.actions.children.exportItem(pd.task.items[0]);
}
pd.task.items.splice(0,1);
},
exportItem: function(item) {
var str = '';
if (pd.exportItems.length == 0) {
str += pd.helpers.csvCell('Title');
str += pd.helpers.csvCell('Body');
str += pd.helpers.csvCell('Permalink');
str+= pd.helpers.csvCell('Actions');
pd.exportItems.push(str);
}
if (pd.exportIds.indexOf(item.data.id) == -1) {
str = '';
str += pd.helpers.csvCell(pd.helpers.csvEscape(item.data.title ? item.data.title : ''));
str += pd.helpers.csvCell(pd.helpers.csvEscape(item.data.body ? item.data.body : ''));
str += pd.helpers.csvCell(item.data.permalink ?
'http://reddit.com'+item.data.permalink :
'http://reddit.com/r/'+item.data.subreddit+'/comments/'+(item.data.link_id.replace(/^t\d_/,''))+'/x/'+item.data.id+'?context=3'
);
str+= pd.helpers.csvCell((item.pdEdited ? 'edited ' : '') + (item.pdDeleted ? 'deleted ' : ''));
pd.exportItems.push(str);
pd.exportIds.push(item.data.id);
pd.task.info.exported ++;
}
}
},
delete: function (item) {
@ -435,65 +501,65 @@ var pd = {
ui: {
updateDisplay: function () {
$('#pd__central h2').first().html('Power Delete Suite v'+pd.version+' <br/><small>'+pd.task.paths.sections[0]+'/'+pd.task.paths.sorts[0]+'</small>');
pd.task.info.numPages = pd.task.info.donePages + ((pd.task.paths.sections.length - 1)*4) + (pd.task.paths.sorts.length);
$('#progress_page .bar').css('width',(Math.round(1000*pd.task.info.donePages/pd.task.info.numPages)/10)+'%');
$('#progress_page .text').text(pd.task.info.donePages + ' / ' + pd.task.info.numPages);
$('#progress_page .text').attr('data-top', pd.task.info.donePages).attr('data-bottom',pd.task.info.numPages);
if (pd.task.info.numItems > 0) {
$('#progress_item .bar').css('width',(Math.round(1000*pd.task.info.doneItems/pd.task.info.numItems)/10)+'%');
$('#progress_item .text').text(pd.task.info.doneItems + ' / ' + pd.task.info.numItems);
$('#progress_item .text').attr('data-top', pd.task.info.doneItems).attr('data-bottom',pd.task.info.numItems);
}
$('#progress_desc').html(
(pd.task.info.edited > 0 ? '<div>'+pd.task.info.edited + ' edited</div>' : '')+
(pd.task.info.deleted > 0 ? '<div>'+pd.task.info.deleted + ' deleted</div>' : '')+
(pd.task.info.errors > 0 ? '<div>'+pd.task.info.errors + ' errors</div>' : '')+
(pd.task.info.ignored > 0 ? '<div>'+pd.task.info.ignored + ' ignored</div>' : '')
);
$('.progress__byline .edited').addClass(pd.task.info.edited > 0 ? 'visible' : '').find('.num').attr('data-num',pd.task.info.edited);
$('.progress__byline .deleted').addClass(pd.task.info.deleted > 0 ? 'visible' : '').find('.num').attr('data-num',pd.task.info.deleted);
$('.progress__byline .errors').addClass(pd.task.info.errors > 0 ? 'visible' : '').find('.num').attr('data-num',pd.task.info.errors);
$('.progress__byline .exported').addClass(pd.task.info.exported > 0 ? 'visible' : '').find('.num').attr('data-num',pd.task.info.exported);
$('.progress__byline .ignored').addClass(pd.task.info.ignored > 0 ? 'visible' : '').find('.num').attr('data-num',pd.task.info.ignored);
for (var key in pd.task.info.ignoreReasons) {
if (!!pd.task.info.ignoreReasons[key]) {
$('#progress_desc')[0].innerHTML += '<div class="ind">'+key+':' + pd.task.info.ignoreReasons[key];
if ($('.progress__byline .ignored .reasons .'+key).length == 0) {
$('.progress__byline .ignored .reasons').prepend('<div class="'+key+'">'+key+': </div>');
}
$('.progress__byline .ignored .reasons .'+key).attr('data-num',pd.task.info.ignoreReasons[key]);
}
}
pd.task.info.ajaxCalls = pd.task.info.errors + pd.task.info.edited + pd.task.info.deleted + pd.task.info.pageCalls;
$('#progress__item-output').attr('class',
pd.task.info.ignored > 0 && (pd.task.info.deleted > 0 || pd.task.info.edited > 0) ?
'twocol': 'onecol');
pd.task.info.ajaxCalls = pd.task.info.errors + pd.task.info.edited + pd.task.info.deleted + pd.task.info.donePages;
document.title = pd.config.user + ' | ' + pd.task.info.ajaxCalls;
},
done: function () {
pd.ui.updateDisplay();
window.pd_processing = false;
document.title = $('#header-bottom-right .user a').first().text()+' | Power Delete Suite';
$('#pd__central h2').first().text('Power Delete Suite v'+pd.version);
var el = $('#pd__central .complete')[0];
if (pd.task.info.edited + pd.task.info.deleted > 0) {
el.innerHTML =
'<p>Completed after making '+pd.task.ajaxCalls+' calls to the reddit servers.</p>'+
(pd.task.info.edited > 0 ? '<div class="ind">'+pd.task.info.edited + ' edited</div>' : '')+
(pd.task.info.deleted > 0 ? '<div class="ind">'+pd.task.info.deleted + ' deleted</div>' : '')+
(pd.task.info.errors > 0 ? '<div class="ind">'+pd.task.info.errors + ' errors</div>' : '') +
(pd.task.info.ignored > 0 ? '<div class="ind">'+pd.task.info.ignored + ' ignored</div>' : '');
if (pd.task.info.edited + pd.task.info.deleted > 0 || pd.task.config.isExporting) {
$('#pd__central .complete .summary').html('<p>Completed after making '+pd.task.info.ajaxCalls+' calls to the reddit servers.</p> <p>If you need to re run the script, <a class="restart">click here to go back to the beginning!</a></p>');
} else {
el.innerHTML =
'<p>All Done! It seems like all '+pd.task.info.ignored+' items we came across were ignored.</p>';
}
if (pd.task.info.ignored > 0) {
var str = '<div class="ind">';
for (var key in pd.task.info.ignoreReasons) {
if (!!pd.task.info.ignoreReasons[key]) {
str += '<div class="ind">'+key+':' + pd.task.info.ignoreReasons[key] + '</div>';
}
}
str += '</div>';
el.innerHTML += str;
$('#pd__central .complete .summary').html('<p>All Done! It seems like all '+pd.task.info.ignored+' items we came across were ignored.</p> <p>If you need to re run the script, <a class="restart">click here to go back to the beginning!</a></p>');
}
$('#pd__central .complete .summary .restart').click(function() {
pd.init();
});
var numSubs = $('#pd__sub-list input:checked').length;
$('#pd__sub-list input').prop('checked',false);
var debugInfo = JSON.stringify($('#pd__form').serializeArray()) + ' number of subreddits: '+numSubs;
el.innerHTML +=
'<p>If you need to re run the script, just click the bookmarklet again!</p>' +
'<hr style="margin-top:2em;"/><h3 class="submit-bug">'+
$('#pd__central .complete .goodbye').html(
'<hr/><h3 class="submit-bug">'+
'<div>Having trouble?</div>'+
'<div><a href="https://www.reddit.com/message/compose?to=j0be&subject=PowerDeleteSuite%20Config&message='+encodeURIComponent(debugInfo)+'" target="_blank">Send /u/j0be a message with your current settings.</a></div>'+
'<div><small>(for privacy, subreddit list is not included)</small></div>'+
'</h3>';
'</h3>');
if (pd.task.config.isExporting && pd.exportItems.length > 0) {
$('#pd__central .complete .goodbye').prepend('<hr/><a class="export-button" href=\'data:text/csv;charset=utf-8,'+pd.exportItems.join("%0A")+'\' download="PowerDeleteSuiteExport.csv">Download Exported Items</a>');
}
$('#pd__central .processing, #pd__form').hide();
$('#pd__central .complete').show();