Skip to content

Instantly share code, notes, and snippets.

@ravexina
Forked from sk22/lastfm-remove-duplicates.js
Last active March 13, 2020 11:44
Show Gist options
  • Select an option

  • Save ravexina/e790ae120fec623c108960f99da0cd0c to your computer and use it in GitHub Desktop.

Select an option

Save ravexina/e790ae120fec623c108960f99da0cd0c to your computer and use it in GitHub Desktop.
Last.fm duplicate scrobble deleter
var max = 20; var num = 5; var counter = 1;
var sections = Array.from(document.getElementsByTagName("tbody"));
sections.forEach(function (section) {
var elements = Array.from(section.rows);
var titles = elements.map(function (element) {
var nameElement = element.querySelector('.chartlist-name');
var artistElement = element.querySelector('.chartlist-artist');
return nameElement && artistElement && nameElement.textContent.replace(/\s+/g, ' ').trim()+':'+artistElement.textContent.replace(/\s+/g, ' ').trim();
});
titles.forEach(function (title, i, titles) {
if (counter > max) { throw new Error("Max Reached!"); } else {counter++};
if (!titles.slice(i + 1, i + 1 + num).includes(title)) return;
var deleteButton = elements[i].querySelector('[data-ajax-form-sets-state="deleted"]');
if (deleteButton) deleteButton.click();
});
})

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.

How-to (create a bookmarklet)

  1. Copy the following script URL into your clipboard
javascript:var max = 20; var num = 5; var counter = 1; var sections = Array.from(document.getElementsByTagName("tbody")); sections.forEach(function (section) {     var elements = Array.from(section.rows);     var titles = elements.map(function (element) {       var nameElement = element.querySelector('.chartlist-name');       var artistElement = element.querySelector('.chartlist-artist');       return nameElement && artistElement && nameElement.textContent.replace(/\s+/g, ' ').trim()+':'+artistElement.textContent.replace(/\s+/g, ' ').trim();     });       titles.forEach(function (title, i, titles) {       if (counter > max) { throw new Error("Max Reached!"); } else {counter++};       if (!titles.slice(i + 1, i + 1 + num).includes(title)) return;       var deleteButton = elements[i].querySelector('[data-ajax-form-sets-state="deleted"]');       if (deleteButton) deleteButton.click();     }); })
  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 max = 20; var num = 5; var counter = 1; var sections = Array.from(document.getElementsByTagName("tbody")); sections.forEach(function (section) {     var elements = Array.from(section.rows);     var titles = elements.map(function (element) {       var nameElement = element.querySelector('.chartlist-name');       var artistElement = element.querySelector('.chartlist-artist');       return nameElement && artistElement && nameElement.textContent.replace(/\s+/g, ' ').trim()+':'+artistElement.textContent.replace(/\s+/g, ' ').trim();     });       titles.forEach(function (title, i, titles) {       if (counter > max) { throw new Error("Max Reached!"); } else {counter++};       if (!titles.slice(i + 1, i + 1 + num).includes(title)) return;       var deleteButton = elements[i].querySelector('[data-ajax-form-sets-state="deleted"]');       if (deleteButton) deleteButton.click();     }); })
  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.

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