Fix source dropdown menu

When loading a previously visited page, the website won't display the selected media source. You need to select another source and then reselect the source you wanted, in order for it to show the media. This user script fixes that bug

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        Fix source dropdown menu
// @namespace   kawa.tf
// @match       *://*.japaneseasmr.com/*
// @grant       none
// @version     1.1.0
// @author      Tilwa Qendov
// @description When loading a previously visited page, the website won't display the selected media source. You need to select another source and then reselect the source you wanted, in order for it to show the media. This user script fixes that bug
// @license     Artistic-2.0
// ==/UserScript==

const MAX_ATTEMPTS = 10
const RETRY_INTERVAL = 1000
const DROPDOWN_ID = 'selectSource'
const SOURCE_IDS = ['audioplayer', 'cleanp_audio', 'dl_links']

function checkDropdown() {
  let dropdown = document.getElementById(DROPDOWN_ID)
  if (dropdown !== null) {
    dropdown.dispatchEvent(new Event('change'))
    console.log("fix-dropdown-menu: Sent an update to the dropdown menu.")
  }
}

function aSourceIsVisible() {
  return !SOURCE_IDS.every(x => document.getElementById(x).style.display === "none")
}

console.log("fix-dropdown-menu: Started")

// Execute 10 times, once every second
let count = 0
let doIt = () => {
  count++
  checkDropdown()
  if (aSourceIsVisible()) {
    console.log("fix-dropdown-menu: The selected source should now be visible.")
    return
  }
  if (count === MAX_ATTEMPTS) {
    console.log(`fix-dropdown-menu: ${MAX_ATTEMPTS} attempts were made to update the dropdown without success.`)
    return
  }
  setTimeout(doIt, RETRY_INTERVAL)
}

doIt()