Skip to content

Instantly share code, notes, and snippets.

@tobek
Forked from sk22/lastfm-remove-duplicates.js
Last active March 7, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobek/a793ecb1bee8eee4e2ced279b79a2429 to your computer and use it in GitHub Desktop.
Save tobek/a793ecb1bee8eee4e2ced279b79a2429 to your computer and use it in GitHub Desktop.
Last.fm duplicate scrobble deleter
var dupesFound = 0;
var elements = Array.from(document.querySelectorAll('.js-link-block'))
elements.map(function (element) {
var nameElement = element.querySelector('.chartlist-name')
return nameElement && nameElement.textContent.replace(/\s+/g, ' ').trim()
}).forEach(function (name, i, names) {
if (name !== names[i + 1]) return
var deleteButton = elements[i].querySelector('[data-ajax-form-sets-state="deleted"]')
if (deleteButton) {
dupesFound++;
console.log('deleting dupe', name, elements[i]);
deleteButton.click()
location.reload()
}
})
if (dupesFound) {
console.log(dupesFound + ' duplicates found and deleted, reloading page...');
}
else {
console.log('no duplicates found on this page');
}

Last.fm duplicate scrobble deleter

This script serves to delete duplicate scrobbles (i.e. the same song scrobbled multiple times in a row) from your Last.fm library. To use it, paste the script into your browser's console (or the address bar, but prefix the script with javascript:) while logged in and in the own library.

Why would I need this?

Your scrobbler might just have decided to scrobble every song hundreds of times and you can't really remove those scrobbles efficiently. Or, if you're like me, you might have accidentally installed multiple scrobbler extensions at the same time - wondering why multiple scrobbles appear for every song played at a time - and you want to clear them after finding the issue.

Using this script still doesn't necessarily make the process quick, since Last.fm only shows a specific number of scrobbles which can be removed on each page in your library.

How-to (create a bookmark)

  1. Copy the following script URL into your clipboard
javascript:var dupesFound=0,elements=Array.from(document.querySelectorAll(".js-link-block"));elements.map(function(e){var o=e.querySelector(".chartlist-name");return o&&o.textContent.replace(/\s+/g," ").trim()}).forEach(function(e,o,n){if(e===n[o+1]){var t=elements[o].querySelector('[data-ajax-form-sets-state="deleted"]');t&&(dupesFound++,console.log("deleting dupe",e,elements[o]),t.click(),location.reload())}}),dupesFound?console.log(dupesFound+" duplicates found and deleted, reloading page..."):console.log("no duplicates found on this page");
  1. Right-click your browser's bookmark bar and click "Add page..."
  2. Give the bookmark a name, like "Remove duplicates"
  3. Paste the script you copied in step 1 into the bookmark's URL.
  4. Save the bookmark
  5. Open your Last.fm account's library while being logged in (https://www.last.fm/user/_/library).
  6. Opening your bookmark will trigger the script to execute.
  7. Repeat clicking the bookmark as long as there are no duplicates left.

How-to (alternative, one-time way)

  1. Copy the one-line script
  2. Open your Last.fm account's library while being logged in (https://www.last.fm/user/_/library).
  3. Type javascript: into the address bar and paste the script directly after it. Or, open the dev tools and paste the script into the console.
  4. Press enter. This will remove all duplicates on the current page.
  5. Let the site reload (invoked by the script).
  6. Repeat pasting the script and pressing enter if more duplicates appear at the bottom.
  7. If needed, go to the next page of your library repeat the steps as of step 3.

Script

var dupesFound=0,elements=Array.from(document.querySelectorAll(".js-link-block"));elements.map(function(e){var o=e.querySelector(".chartlist-name");return o&&o.textContent.replace(/\s+/g," ").trim()}).forEach(function(e,o,n){if(e===n[o+1]){var t=elements[o].querySelector('[data-ajax-form-sets-state="deleted"]');t&&(dupesFound++,console.log("deleting dupe",e,elements[o]),t.click(),location.reload())}}),dupesFound?console.log(dupesFound+" duplicates found and deleted, reloading page..."):console.log("no duplicates found on this page");

Why do I need to repeat executing the script?

The script will only remove what's visible on the current library page. After entries were deleted, more duplicates may appear at the bottom. This might happen multiple times. Once one page is finally duplicate-free, the process can be repeated for next pages.

var dupesFound=0,elements=Array.from(document.querySelectorAll(".js-link-block"));elements.map(function(e){var o=e.querySelector(".chartlist-name");return o&&o.textContent.replace(/\s+/g," ").trim()}).forEach(function(e,o,n){if(e===n[o+1]){var t=elements[o].querySelector('[data-ajax-form-sets-state="deleted"]');t&&(dupesFound++,console.log("deleting dupe",e,elements[o]),t.click(),location.reload())}}),dupesFound?console.log(dupesFound+" duplicates found and deleted, reloading page..."):console.log("no duplicates found on this page");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment