Skip to content

Instantly share code, notes, and snippets.

@meldsza
Last active July 30, 2017 12:09
Show Gist options
  • Save meldsza/e8362f4c8cba949b93c47e40ecfabe5f to your computer and use it in GitHub Desktop.
Save meldsza/e8362f4c8cba949b93c47e40ecfabe5f to your computer and use it in GitHub Desktop.
Move all topics from one category to another and delete the current category - Discourse
/**
Please Note that this script is for Discourse Forum
To move all posts in the CURRENT category to another category
For example i want to move all the topics in the current category to category number 70
Just open the category. open Developer tools, paste this in the console and type the command moveAndDeleteCategory("70")
You can get the category id by going to /site.json
*/
function whenElementExistsThenDo(selector, fn, count) {
if (!count) count = 0;
if (count > 1500) {
console.log('I did all i could');
return false;
}
if ($(selector).length > 0) {
fn();
} else {
console.log('waiting for ' + selector);
setTimeout(function(){
whenElementExistsThenDo(selector, fn, count+1);
}, 100);
}
}
// empties then deletes the category
// if category fails to delete, refreshes and you can do it again
function moveAndDeleteCategory (newCat) {
newCat = newCat+""; //convert to string if not already a string
$('button.bulk-select').trigger('click'); // select items
$('input.bulk-select').trigger('click');
whenElementExistsThenDo('#bulk-select > button', function(){
$('#bulk-select > button').trigger('click'); // bulk-process
whenElementExistsThenDo('button:contains("Set Category")', function(){
$('button:contains("Set Category")').trigger('click'); // selects move
$('.category-combobox').val(newCat).change()
$('button:contains("Set Category")').trigger('click'); // moves items
setTimeout(function(){
whenElementExistsThenDo('.edit-category', function(){
deleteCategory();
});
},500);
});
});
}
function deleteCategory () {
$('.edit-category').trigger('click'); // edit category
whenElementExistsThenDo('button:contains("Delete Category")', function(){
$('button:contains("Delete Category")').trigger('click'); // delete category
$('.modal-footer> a.btn-primary[data-handler="1"]').trigger('click');
// in a sec, let's check and make sure it didn't error with an odd modal
setTimeout(function(){
whenElementExistsThenDo('.edit-category', function(){ // if unsuccessful
$('.modal-middle-container').trigger('click'); // dismiss modal
console.log("We Failed");
});
},500);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment