hosmanadam / youtube-playlist-plus

Userscript for extending YouTube's playlist functionality
MIT License
4 stars 0 forks source link

Some videos reappear when playlist page is reloaded after batch removal #2

Open hosmanadam opened 4 years ago

hosmanadam commented 4 years ago

Describe the bug

During batch removal, some videos, when their remove button is clicked, seem to get removed from the playlist but reappear later when the page is reloaded. Additionally, this leaves the batch removal script in an infinite loop and prevents the success confirmation from being displayed — it knows that not all videos have been removed, but can't find any of the videos it wanted to remove. An early indicator that this is going to happen is that the video count doesn't get decremented each time a video disappears.

To reproduce

  1. Open a playlist with more than 100+ videos
  2. Click on 'Batch remove videos' and follow the instructions
  3. Observe that the video count doesn't change in sync with the removals
  4. Observe that YouTube Playlist+ doesn't display the success alert
  5. Reload the page
  6. Observe that some of the removed videos reappear

Expected behavior

  1. When videos are being removed, video count changes in sync with the removals
  2. When batch removal is finished, success alert is displayed
  3. When page is reloaded, removed videos don't reappear

Setup

Additional context

This is more likely to happen with higher clicksPerSecond values and larger (100+) playlists. It doesn't happen, for example, on a single-page playlist with clicksPerSecond = 1.

hosmanadam commented 4 years ago

There's a discrepancy between YouTube's handling of the clicks on the front-end and back-end, and we don't know where the issue occurs. Some part of the system can't keep up with the speed of the clicks and encounters a race condition, db lock, or similar. The details for why this happens aren't of much interest, let's just say that YouTube wasn't designed to be clicked so fast.

The following solutions come to mind:

Proper fix: dynamically find the click timing that's suitable for the situation

The way this could work is if click timing wasn't based on a fixed interval, but on an event triggered by the successful completion of the previous removal. Successful removals are signaled, for example, by:

This would fully achieve the expected behavior outlined in the ticket.

Proper fix: drop the clicks and make API requests instead

YouTube does have a video delete API which could possibly be accessed by a userscript

This would probably exceed the expected behavior outlined in the ticket, plus provide additional benefits. However, it would make the application way more complicated and take considerably more time to implement. Or, it may not even work since many API requests may be even worse than many clicks (API only allows individual removals).

Workaround: detect this, reload page, run again

  1. Detect: After clicking all buttons we wanted to click, compare number of visible videos vs. video count at the top: if they don't match, we have a situation.
  2. Reload: Save options to query params or localStorage, and go.
  3. Run again: Load options on init(), load settings and start working without requiring user interaction.

This would partially achieve the expected behavior outlined in the ticket, with the end result being the same as in the "Proper fix".