Skip to content

Instantly share code, notes, and snippets.

@sk22
Last active January 13, 2024 16:26
Show Gist options
  • Save sk22/39cc280840f9d82df574c15d6eda6629 to your computer and use it in GitHub Desktop.
Save sk22/39cc280840f9d82df574c15d6eda6629 to your computer and use it in GitHub Desktop.
Last.fm duplicate scrobble deleter
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) deleteButton.click()
location.reload()
})

doesn't work anymore and i deleted my last.fm - maybe someone posted a working version in the comments below!


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 bookmarklet)

  1. Copy the following script URL into your clipboard
javascript:var elements=Array.from(document.querySelectorAll('.js-link-block'));elements.map(function(a){var b=a.querySelector('.chartlist-name');return b&&b.textContent.replace(/\s+/g,' ').trim()}).forEach(function(a,b,c){if(a===c[b+1]){var d=elements[b].querySelector('[data-ajax-form-sets-state="deleted"]');d&&d.click(),location.reload()}});
  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 script
var elements=Array.from(document.querySelectorAll('.js-link-block'));elements.map(function(a){var b=a.querySelector('.chartlist-name');return b&&b.textContent.replace(/\s+/g,' ').trim()}).forEach(function(a,b,c){if(a===c[b+1]){var d=elements[b].querySelector('[data-ajax-form-sets-state="deleted"]');d&&d.click(),location.reload()}});
  1. Open your Last.fm account's library while being logged in (https://www.last.fm/user/_/library).
  2. 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.
  3. Press enter. This will remove all duplicates on the current page.
  4. Let the site reload (invoked by the script).
  5. Repeat pasting the script and pressing enter if more duplicates appear at the bottom.
  6. If needed, go to the next page of your library repeat the steps as of step 3.

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 elements=Array.from(document.querySelectorAll('.js-link-block'));elements.map(function(a){var b=a.querySelector('.chartlist-name');return b&&b.textContent.replace(/\s+/g,' ').trim()}).forEach(function(a,b,c){if(a===c[b+1]){var d=elements[b].querySelector('[data-ajax-form-sets-state="deleted"]');d&&d.click(),location.reload()}});
@dgr7
Copy link

dgr7 commented Mar 13, 2021

@muhdiboy thank you!

@RoelJanssens
Copy link

But this script only removes the duplicates from the page that is currently loaded right? So I must click ALL my history pages one by one to get them all removed, that will take a looooong time.

@muhdiboy
Copy link

But this script only removes the duplicates from the page that is currently loaded right? So I must click ALL my history pages one by one to get them all removed, that will take a looooong time.

@RoelJanssens This script will load the next page (in terms of newer page) automatically if it can't find another duplicate. So you just have to start executing this script on the page you want to start deleting duplicates. Then after reload you execute it again and so forth. For an easy execution I recommend creating a bookmark as described above.

Of course it is still possible to set this script up in an environment to automate the whole process.

@RoelJanssens
Copy link

I have more than 300 pages of history and I don't have the knowlegde to automate your script to go through my whole archive.

I still think it's unbelievable that Last.FM doesn't detect duplicates automatically.

@muhdiboy
Copy link

Hello everyone,

I've saved up more than 100 pages of history and decided to implement an automation to this process. With the help of Tampermonkey (Chrome/Chromium) or Greasemonkey (Firefox) you can execute the duplicate deletion automatically on page load.

The Gist of this UserScript is on my profile and not a comment here, because it doesn't suit this Gist anymore. It is based on my latest version of the script.
Of course many thanks to all contributors on this Gist and @sk22. I've decided to include everyone involved in the description of the UserScript.

I've tested this UserScript with Tampermonkey and implemented some features to stop the script, because otherwise it will endlessly run.

Check out here: https://gist.github.com/muhdiboy/a293cbff355af750e3b8f45ec816d1f1
I'm open to any suggestions or improvements. To be clear though, I'm not a developer and quite the novice in terms of Javascript.

The Gist is currently missing a install and user guide, I will add it tomorrow. It's not rocket science, just install your choice of UserScript Manager/Tool and import the script, either manually or directly with the link: https://gist.github.com/muhdiboy/a293cbff355af750e3b8f45ec816d1f1#file-lastfm-automated-remove-duplicates-js
After that enable the UserScript, go to your last.fm library, head to the page where to start the deletion and finally reload the page to initiate the automation. At the end of the loop you will receive a pop-up to remind you to disable the script.

Kind regards to everyone and have fun!


fyi. @RoelJanssens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment