Created
June 7, 2023 15:13
-
-
Save titangene/3a82203db124d5015f946b8d57857087 to your computer and use it in GitHub Desktop.
Switch Pluralsight Subtitle (EN <--> TW)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
class ButtonElement { | |
constructor(selector) { | |
this.selector = selector; | |
} | |
get element() { | |
return document.querySelector(this.selector); | |
} | |
click() { | |
this.element.click(); | |
} | |
} | |
class SettingsButton extends ButtonElement { | |
constructor() { | |
const selector = '[data-text="Settings"] button'; | |
super(selector); | |
} | |
toggle() { | |
this.click(); | |
} | |
} | |
class CaptionLanguageSwitcher { | |
$registeredLanguageButtons = [] | |
constructor() { | |
const allButtonsSelector = '[aria-label="set caption language"] > button'; | |
this.$allButtons = [...document.querySelectorAll(allButtonsSelector)]; | |
} | |
registorLanguage(language) { | |
const $button = this.getButtonByLanguage(language); | |
console.log(language, $button); | |
this.$registeredLanguageButtons.push($button); | |
} | |
getButtonByLanguage(language) { | |
return this.$allButtons.find($button => $button.querySelector('span').textContent.includes(language)); | |
} | |
switchLanguage() { | |
const languageButtonCount = this.$registeredLanguageButtons.length; | |
for (let index = 0; index < languageButtonCount; index++) { | |
const $button = this.$registeredLanguageButtons[index]; | |
if (this.isCurrentLanguage($button)) { | |
this.getNextLanguageButton(index).click(); | |
break; | |
} | |
} | |
} | |
getNextLanguageButton(currentIndex) { | |
const buttonCount = this.$registeredLanguageButtons.length; | |
const nextIndex = currentIndex + 1 === buttonCount ? 0 : currentIndex + 1; | |
return this.$registeredLanguageButtons[nextIndex]; | |
} | |
isCurrentLanguage($button) { | |
return Boolean($button.querySelector('svg')); | |
} | |
} | |
const settingsButton = new SettingsButton(); | |
settingsButton.toggle(); | |
const captionLanguageSwitcher = new CaptionLanguageSwitcher(); | |
captionLanguageSwitcher.registorLanguage('English'); | |
captionLanguageSwitcher.registorLanguage('Chinese (Traditional)'); | |
captionLanguageSwitcher.switchLanguage(); | |
settingsButton.toggle(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment