I am trying to delete 800 automatic edit entries at Last.fm (https://www.last.fm/settings/subscription/automatic-edits) and the site does not offer bulk edit/remove functionality.
So far I managed to remove a single entry by running the following 3 commands in that specific order in the browser console
jQuery('.disclose-trigger').each(function(_, b) {
b.click();
});
jQuery('.btn-delete').each(function(_, b) {
b.click();
});
jQuery('.js-close').each(function(_, b) {
b.click();
});
Now I am trying to figure out how to merge them to 1 single command that also repeats to automate things.
As every command triggers an action, there has to be some kind of delay between running them.
Should I be looking at function delay and/or async function?
Is this the right direction?
function delay(milliseconds){
return new Promise(resolve => {
setTimeout(resolve, milliseconds);
});
}
async function init(){
jQuery('.disclose-trigger').each(function(_, b) {
b.click();
});
await delay(2000);
jQuery('.btn-delete').each(function(_, b) {
b.click();
});
await delay(2000);
jQuery('.js-close').each(function(_, b) {
b.click();
});
}
init();
2
Answers
It is a hacky way. The problems with it are as follows:
The approach you would need to perform is to find out what functions are triggered by the click, build an array of promises for all those function calls and use
Promise.all()
to wait for all your promises to complete and add a.then()
call, which will be resolved then the next collection of promises complete and so on.If you are incapable of doing that for some reason, then you will end up with delays, but you should do everything within your power to make this more elegant. The key question is: how can you detect whether all the jobs you are waiting for were completed.
If the API doesn’t support deleting automatic edits then a quick botch would be fine for your personal goal.
The delete action is a form submit so it might be easier to just look up what query selector you need to find the delete form and submit it with
form.submit();
A
setInterval
could be used to wait until the delete form is rendered on the page. Using MutationObserver to detect the form being rendered on the page goes a bit far for this use case but would be a neater solution.You could write a userscript to automatically run the code each time the page refreshes until you’re done.