Skip to content

Instantly share code, notes, and snippets.

@titangene
Created June 7, 2023 15:13
Show Gist options
  • Save titangene/3a82203db124d5015f946b8d57857087 to your computer and use it in GitHub Desktop.
Save titangene/3a82203db124d5015f946b8d57857087 to your computer and use it in GitHub Desktop.
Switch Pluralsight Subtitle (EN <--> TW)
(() => {
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