gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
Make code fully async with Promise.all [code]
- User
Make code fully async with Promise.all
js - Assistant
Make code fully async with Promise.all
initUI() {
// VOT Button
{
this.votButton = ui.createVOTButton(
localizationProvider.get("translateVideo"),
);
this.votButton.container.style.opacity = 0;
// use an additional check because sometimes this.video.clientWidth = 0
if (
this.data?.buttonPos &&
this.data?.buttonPos !== "default" &&
this.container.clientWidth &&
this.container.clientWidth > 550
) {
this.votButton.container.dataset.direction = "column";
this.votButton.container.dataset.position = this.data?.buttonPos;
} else {
this.votButton.container.dataset.direction = "row";
this.votButton.container.dataset.position = "default";
}
this.container.appendChild(this.votButton.container);
this.votButton.pipButton.hidden =
!isPiPAvailable() || !this.data?.showPiPButton;
this.votButton.separator2.hidden =
!isPiPAvailable() || !this.data?.showPiPButton;
this.votButton.container.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
});
}
// VOT Menu
{
this.votMenu = ui.createVOTMenu(localizationProvider.get("VOTSettings"));
this.votMenu.container.dataset.position =
this.container.clientWidth && this.container.clientWidth > 550
? this.data?.buttonPos
: "default";
this.container.appendChild(this.votMenu.container);
this.votDownloadButton = ui.createIconButton(
svg`<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="100%"
viewBox="0 -960 960 960"
>
<path
d="M480-337q-8 0-15-2.5t-13-8.5L308-492q-12-12-11.5-28t11.5-28q12-12 28.5-12.5T365-549l75 75v-286q0-17 11.5-28.5T480-800q17 0 28.5 11.5T520-760v286l75-75q12-12 28.5-11.5T652-548q11 12 11.5 28T652-492L508-348q-6 6-13 8.5t-15 2.5ZM240-160q-33 0-56.5-23.5T160-240v-80q0-17 11.5-28.5T200-360q17 0 28.5 11.5T240-320v80h480v-80q0-17 11.5-28.5T760-360q17 0 28.5 11.5T800-320v80q0 33-23.5 56.5T720-160H240Z"
/>
</svg>`,
);
this.votDownloadButton.hidden = true;
this.votMenu.headerContainer.appendChild(this.votDownloadButton);
this.votDownloadSubtitlesButton = ui.createIconButton(
svg`<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="100%"
viewBox="0 0 24 24"
>
<path
d="M4 20q-.825 0-1.413-.588T2 18V6q0-.825.588-1.413T4 4h16q.825 0 1.413.588T22 6v12q0 .825-.588 1.413T20 20H4Zm2-4h8v-2H6v2Zm10 0h2v-2h-2v2ZM6 12h2v-2H6v2Zm4 0h8v-2h-8v2Z"
/>
</svg>`,
);
this.votDownloadSubtitlesButton.hidden = true;
this.votMenu.headerContainer.appendChild(this.votDownloadSubtitlesButton);
this.votSettingsButton = ui.createIconButton(
svg`<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="100%"
viewBox="0 -960 960 960"
>
<path
d="M555-80H405q-15 0-26-10t-13-25l-12-93q-13-5-24.5-12T307-235l-87 36q-14 5-28 1t-22-17L96-344q-8-13-5-28t15-24l75-57q-1-7-1-13.5v-27q0-6.5 1-13.5l-75-57q-12-9-15-24t5-28l74-129q7-14 21.5-17.5T220-761l87 36q11-8 23-15t24-12l12-93q2-15 13-25t26-10h150q15 0 26 10t13 25l12 93q13 5 24.5 12t22.5 15l87-36q14-5 28-1t22 17l74 129q8 13 5 28t-15 24l-75 57q1 7 1 13.5v27q0 6.5-2 13.5l75 57q12 9 15 24t-5 28l-74 128q-8 13-22.5 17.5T738-199l-85-36q-11 8-23 15t-24 12l-12 93q-2 15-13 25t-26 10Zm-73-260q58 0 99-41t41-99q0-58-41-99t-99-41q-59 0-99.5 41T342-480q0 58 40.5 99t99.5 41Zm0-80q-25 0-42.5-17.5T422-480q0-25 17.5-42.5T482-540q25 0 42.5 17.5T542-480q0 25-17.5 42.5T482-420Zm-2-60Zm-40 320h79l14-106q31-8 57.5-23.5T639-327l99 41 39-68-86-65q5-14 7-29.5t2-31.5q0-16-2-31.5t-7-29.5l86-65-39-68-99 42q-22-23-48.5-38.5T533-694l-13-106h-79l-14 106q-31 8-57.5 23.5T321-633l-99-41-39 68 86 64q-5 15-7 30t-2 32q0 16 2 31t7 30l-86 65 39 68 99-42q22 23 48.5 38.5T427-266l13 106Z"
/>
</svg>`,
);
this.votMenu.headerContainer.appendChild(this.votSettingsButton);
this.votTranslationLanguageSelect = ui.createVOTLanguageSelect({
fromTitle:
localizationProvider.get("langs")[this.video.detectedLanguage],
fromDialogTitle: localizationProvider.get("videoLanguage"),
fromItems: genOptionsByOBJ(
availableLangs,
this.videoData.detectedLanguage,
),
fromOnSelectCB: async (e) => {
debug.log(
"[fromOnSelectCB] select from language",
e.target.dataset.votValue,
);
this.videoData = await this.getVideoData();
this.setSelectMenuValues(
e.target.dataset.votValue,
this.videoData.responseLanguage,
);
},
toTitle: localizationProvider.get("langs")[this.video.responseLanguage],
toDialogTitle: localizationProvider.get("translationLanguage"),
toItems: genOptionsByOBJ(availableTTS, this.videoData.responseLanguage),
toOnSelectCB: async (e) => {
const newLang = e.target.dataset.votValue;
debug.log("[toOnSelectCB] select to language", newLang);
this.data.responseLanguage = this.translateToLang = newLang;
await votStorage.set("responseLanguage", this.data.responseLanguage);
debug.log(
"Response Language value changed. New value: ",
this.data.responseLanguage,
);
this.videoData = await this.getVideoData();
this.setSelectMenuValues(
this.videoData.detectedLanguage,
this.data.responseLanguage,
);
},
});
this.votMenu.bodyContainer.appendChild(
this.votTranslationLanguageSelect.container,
);
this.votSubtitlesSelect = ui.createVOTSelect(
localizationProvider.get("VOTSubtitlesDisabled"),
localizationProvider.get("VOTSubtitles"),
[
{
label: localizationProvider.get("VOTSubtitlesDisabled"),
value: "disabled",
selected: true,
disabled: false,
},
],
{
onSelectCb: async (e) => {
await this.changeSubtitlesLang(e.target.dataset.votValue);
},
labelElement: ui.createVOTSelectLabel(
localizationProvider.get("VOTSubtitles"),
),
},
);
this.votMenu.bodyContainer.appendChild(this.votSubtitlesSelect.container);
this.votVideoVolumeSlider = ui.createSlider(
html`${localizationProvider.get("VOTVolume")}:
<strong>${this.getVideoVolume() * 100}%</strong>`,
this.getVideoVolume() * 100,
);
this.votVideoVolumeSlider.container.hidden =
this.data.showVideoSlider !== 1 ||
this.votButton.container.dataset.status !== "success";
this.votMenu.bodyContainer.appendChild(
this.votVideoVolumeSlider.container,
);
this.votVideoTranslationVolumeSlider = ui.createSlider(
html`${localizationProvider.get("VOTVolumeTranslation")}:
<strong>${this.data?.defaultVolume ?? 100}%</strong>`,
this.data?.defaultVolume ?? 100,
0,
this.data.audioBooster ? maxAudioVolume : 100,
);
this.votVideoTranslationVolumeSlider.container.hidden =
this.votButton.container.dataset.status !== "success";
this.votMenu.bodyContainer.appendChild(
this.votVideoTranslationVolumeSlider.container,
);
this.votMenu.container.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
});
}
// VOT Settings
{
this.votSettingsDialog = ui.createDialog(
localizationProvider.get("VOTSettings"),
);
document.documentElement.appendChild(this.votSettingsDialog.container);
this.votTranslationHeader = ui.createHeader(
localizationProvider.get("translationSettings"),
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votTranslationHeader,
);
this.votAutoTranslateCheckbox = ui.createCheckbox(
localizationProvider.get("VOTAutoTranslate"),
this.data?.autoTranslate ?? false,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votAutoTranslateCheckbox.container,
);
this.votDontTranslateYourLangSelect = ui.createVOTSelect(
localizationProvider.get("langs")[
votStorage.syncGet("dontTranslateLanguage", lang)
],
localizationProvider.get("VOTDontTranslateYourLang"),
genOptionsByOBJ(
availableLangs,
votStorage.syncGet("dontTranslateLanguage", lang),
),
{
onSelectCb: async (e) => {
this.data.dontTranslateLanguage = e.target.dataset.votValue;
await votStorage.set(
"dontTranslateLanguage",
this.data.dontTranslateLanguage,
);
},
labelElement: ui.createCheckbox(
localizationProvider.get("VOTDontTranslateYourLang"),
this.data?.dontTranslateYourLang ?? true,
).container,
},
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votDontTranslateYourLangSelect.container,
);
this.votAutoSetVolumeCheckbox = ui.createCheckbox(
`${localizationProvider.get("VOTAutoSetVolume")}`,
this.data?.autoSetVolumeYandexStyle ?? true,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votAutoSetVolumeCheckbox.container,
);
this.votAutoSetVolumeSlider = ui.createSlider(
html`<strong
>${(this.data?.autoVolume ?? defaultAutoVolume) * 100}%</strong
>`,
(this.data?.autoVolume ?? defaultAutoVolume) * 100,
0,
100,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votAutoSetVolumeSlider.container,
);
this.votShowVideoSliderCheckbox = ui.createCheckbox(
localizationProvider.get("VOTShowVideoSlider"),
this.data?.showVideoSlider ?? false,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votShowVideoSliderCheckbox.container,
);
this.votAudioBoosterCheckbox = ui.createCheckbox(
localizationProvider.get("VOTAudioBooster"),
this.data?.audioBooster ?? false,
);
this.votAudioBoosterCheckbox.container.hidden = !this.audioContext;
this.votSettingsDialog.bodyContainer.appendChild(
this.votAudioBoosterCheckbox.container,
);
this.votSyncVolumeCheckbox = ui.createCheckbox(
localizationProvider.get("VOTSyncVolume"),
this.data?.syncVolume ?? false,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votSyncVolumeCheckbox.container,
);
this.votTranslationServiceSelect = ui.createVOTSelect(
votStorage
.syncGet("translationService", defaultTranslationService)
.toUpperCase(),
localizationProvider.get("VOTTranslationService"),
genOptionsByOBJ(
translateServices,
votStorage.syncGet("translationService", defaultTranslationService),
),
{
onSelectCb: async (e) => {
this.data.translationService = e.target.dataset.votValue;
await votStorage.set(
"translationService",
this.data.translationService,
);
},
labelElement: ui.createCheckbox(
localizationProvider.get("VOTTranslateAPIErrors"),
this.data.translateAPIErrors ?? true,
).container,
},
);
this.votTranslationServiceSelect.container.hidden =
localizationProvider.lang === "ru";
this.votSettingsDialog.bodyContainer.appendChild(
this.votTranslationServiceSelect.container,
);
this.votDetectServiceSelect = ui.createVOTSelect(
votStorage.syncGet("detectService", defaultDetectService).toUpperCase(),
localizationProvider.get("VOTDetectService"),
genOptionsByOBJ(
detectServices,
votStorage.syncGet("detectService", defaultDetectService),
),
{
onSelectCb: async (e) => {
this.data.detectService = e.target.dataset.votValue;
await votStorage.set("detectService", this.data.detectService);
},
labelElement: ui.createVOTSelectLabel(
localizationProvider.get("VOTDetectService"),
),
},
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votDetectServiceSelect.container,
);
// SUBTITLES
this.votSubtitlesHeader = ui.createHeader(
localizationProvider.get("subtitlesSettings"),
);
this.votSettingsDialog.bodyContainer.appendChild(this.votSubtitlesHeader);
this.votSubtitlesDetails = ui.createDetails(
localizationProvider.get("VOTSubtitlesDesign"),
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votSubtitlesDetails.container,
);
// PROXY
this.votProxyHeader = ui.createHeader(
localizationProvider.get("proxySettings"),
);
this.votSettingsDialog.bodyContainer.appendChild(this.votProxyHeader);
this.votM3u8ProxyHostTextfield = ui.createTextfield(
localizationProvider.get("VOTM3u8ProxyHost"),
this.data?.m3u8ProxyHost,
m3u8ProxyHost,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votM3u8ProxyHostTextfield.container,
);
this.votProxyWorkerHostTextfield = ui.createTextfield(
localizationProvider.get("VOTProxyWorkerHost"),
this.data?.proxyWorkerHost,
proxyWorkerHost,
);
// this.votProxyWorkerHostTextfield.container.hidden =
// this.data.translateProxyEnabled !== 1;
this.votSettingsDialog.bodyContainer.appendChild(
this.votProxyWorkerHostTextfield.container,
);
this.votAudioProxyCheckbox = ui.createCheckbox(
localizationProvider.get("VOTAudioProxy"),
this.data?.audioProxy ?? false,
);
// this.votAudioProxyCheckbox.container.hidden =
// this.data.translateProxyEnabled !== 1;
this.votSettingsDialog.bodyContainer.appendChild(
this.votAudioProxyCheckbox.container,
);
// ABOUT
this.votAboutHeader = ui.createHeader(localizationProvider.get("about"));
this.votSettingsDialog.bodyContainer.appendChild(this.votAboutHeader);
this.votLanguageSelect = ui.createVOTSelect(
localizationProvider.get("langs")[
// eslint-disable-next-line sonarjs/no-duplicate-string
votStorage.syncGet("locale-lang-override", "auto")
],
localizationProvider.get("VOTMenuLanguage"),
genOptionsByOBJ(
availableLocales,
votStorage.syncGet("locale-lang-override", "auto"),
),
{
onSelectCb: async (e) => {
await votStorage.set(
"locale-lang-override",
e.target.dataset.votValue,
);
},
labelElement: ui.createVOTSelectLabel(
localizationProvider.get("VOTMenuLanguage"),
),
},
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votLanguageSelect.container,
);
this.votShowPiPButtonCheckbox = ui.createCheckbox(
localizationProvider.get("VOTShowPiPButton"),
this.data?.showPiPButton ?? false,
);
this.votShowPiPButtonCheckbox.container.hidden = !isPiPAvailable();
this.votSettingsDialog.bodyContainer.appendChild(
this.votShowPiPButtonCheckbox.container,
);
this.votVersionInfo = ui.createInformation(
`${localizationProvider.get("VOTVersion")}:`,
GM_info.script.version,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votVersionInfo.container,
);
this.votAuthorsInfo = ui.createInformation(
`${localizationProvider.get("VOTAuthors")}:`,
GM_info.script.author,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votAuthorsInfo.container,
);
this.votLoaderInfo = ui.createInformation(
`${localizationProvider.get("VOTLoader")}:`,
`${GM_info.scriptHandler} v${GM_info.version}`,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votLoaderInfo.container,
);
this.votBrowserInfo = ui.createInformation(
`${localizationProvider.get("VOTBrowser")}:`,
`${browserInfo.browser.name} ${browserInfo.browser.version} (${browserInfo.os.name} ${browserInfo.os.version})`,
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votBrowserInfo.container,
);
this.votResetSettingsButton = ui.createButton(
localizationProvider.get("resetSettings"),
);
this.votSettingsDialog.bodyContainer.appendChild(
this.votResetSettingsButton,
);
}
}
initUIEvents() {
// VOT Button
{
this.votButton.translateButton.addEventListener("click", () => {
(async () => {
if (this.audio.src) {
debug.log("[click translationBtn] audio.src is not empty");
this.stopTranslate();
return;
}
if (this.hls.url) {
debug.log("[click translationBtn] hls is not empty");
this.stopTranslate();
return;
}
try {
debug.log("[click translationBtn] trying execute translation");
if (!this.videoData.videoId) {
throw new VOTLocalizedError("VOTNoVideoIDFound");
}
// при скролле ленты клипов в вк сохраняется старый айди видео для перевода,
// но для субтитров используется новый, поэтому перед запуском перевода необходимо получить актуальный айди
if (
this.site.host === "vk" &&
this.site.additionalData === "clips"
) {
this.videoData = await this.getVideoData();
}
await this.translateExecutor(this.videoData.videoId);
} catch (err) {
console.error("[VOT]", err);
if (err?.name === "VOTLocalizedError") {
this.transformBtn("error", err.localizedMessage);
} else {
this.transformBtn("error", err);
}
}
})();
});
this.votButton.pipButton.addEventListener("click", () => {
(async () => {
if (this.video !== document.pictureInPictureElement) {
await this.video.requestPictureInPicture();
} else {
await document.exitPictureInPicture();
}
})();
});
this.votButton.menuButton.addEventListener("click", () => {
this.votMenu.container.hidden = !this.votMenu.container.hidden;
});
this.votButton.container.addEventListener("mousedown", () => {
this.dragging = true;
});
this.container.addEventListener("mouseup", () => {
this.dragging = false;
});
this.container.addEventListener("mousemove", async (e) => {
if (this.dragging) {
e.preventDefault();
const percentX = (e.clientX / this.container.clientWidth) * 100;
// const percentY = (e.clientY / this.video.clientHeight) * 100;
this.data.buttonPos =
this.container.clientWidth && this.container.clientWidth > 550
? percentX <= 44
? "left"
: percentX >= 66
? "right"
: "default"
: "default";
this.votButton.container.dataset.direction =
this.data.buttonPos === "default" ? "row" : "column";
this.votButton.container.dataset.position = this.data.buttonPos;
this.votMenu.container.dataset.position = this.data.buttonPos;
if (this.container.clientWidth && this.container.clientWidth > 550) {
await votStorage.set("buttonPos", this.data.buttonPos);
}
}
});
}
// VOT Menu
{
this.votDownloadButton.addEventListener("click", () => {
if (this.downloadTranslationUrl) {
window.open(this.downloadTranslationUrl, "_blank").focus();
}
});
this.votDownloadSubtitlesButton.addEventListener("click", async () => {
const srtContent = convertSubs(this.yandexSubtitles, "srt");
const blob = new Blob([srtContent], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `subtitles_${this.videoData.videoId}.srt`;
a.click();
URL.revokeObjectURL(url);
});
this.votSettingsButton.addEventListener("click", () => {
this.votSettingsDialog.container.hidden =
!this.votSettingsDialog.container.hidden;
if (document.fullscreenElement || document.webkitFullscreenElement) {
document.webkitExitFullscreen && document.webkitExitFullscreen();
document.exitFullscreen && document.exitFullscreen();
}
});
this.votVideoVolumeSlider.input.addEventListener("input", (e) => {
const value = Number(e.target.value);
this.votVideoVolumeSlider.label.querySelector("strong").textContent =
`${value}%`;
this.setVideoVolume(value / 100);
if (this.data.syncVolume) {
this.syncVolumeWrapper("video", value);
}
});
this.votVideoTranslationVolumeSlider.input.addEventListener(
"input",
(e) => {
(async () => {
this.data.defaultVolume = Number(e.target.value);
await votStorage.set("defaultVolume", this.data.defaultVolume);
this.votVideoTranslationVolumeSlider.label.querySelector(
"strong",
).textContent = `${this.data.defaultVolume}%`;
this.setAudioVolume(this.data.defaultVolume / 100);
if (!this.data.syncVolume) {
return;
}
this.syncVolumeWrapper("translation", this.data.defaultVolume);
if (
["youtube", "googledrive"].includes(this.site.host) &&
this.site.additionalData !== "mobile"
) {
// fix update youtube volume slider
this.setVideoVolume(this.tempOriginalVolume / 100);
}
})();
},
);
}
// VOT Settings
{
this.votAutoTranslateCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.autoTranslate = Number(e.target.checked);
await votStorage.set("autoTranslate", this.data.autoTranslate);
await this.autoTranslate();
debug.log(
"autoTranslate value changed. New value: ",
this.data.autoTranslate,
);
})();
});
this.votDontTranslateYourLangSelect.labelElement.addEventListener(
"change",
(e) => {
(async () => {
this.data.dontTranslateYourLang = Number(e.target.checked);
await votStorage.set(
"dontTranslateYourLang",
this.data.dontTranslateYourLang,
);
debug.log(
"dontTranslateYourLang value changed. New value: ",
this.data.dontTranslateYourLang,
);
})();
},
);
this.votAutoSetVolumeCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.autoSetVolumeYandexStyle = Number(e.target.checked);
await votStorage.set(
"autoSetVolumeYandexStyle",
this.data.autoSetVolumeYandexStyle,
);
debug.log(
"autoSetVolumeYandexStyle value changed. New value: ",
this.data.autoSetVolumeYandexStyle,
);
})();
});
this.votAutoSetVolumeSlider.input.addEventListener("input", (e) => {
(async () => {
const presetAutoVolume = Number(e.target.value);
this.data.autoVolume = (presetAutoVolume / 100).toFixed(2);
await votStorage.set("autoVolume", this.data.autoVolume);
this.votAutoSetVolumeSlider.label.querySelector(
"strong",
).textContent = `${presetAutoVolume}%`;
})();
});
this.votShowVideoSliderCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.showVideoSlider = Number(e.target.checked);
await votStorage.set("showVideoSlider", this.data.showVideoSlider);
debug.log(
"showVideoSlider value changed. New value: ",
this.data.showVideoSlider,
);
this.votVideoVolumeSlider.container.hidden =
this.data.showVideoSlider !== 1 ||
this.votButton.container.dataset.status !== "success";
})();
});
this.votAudioBoosterCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.audioBooster = Number(e.target.checked);
await votStorage.set("audioBooster", this.data.audioBooster);
debug.log(
"audioBooster value changed. New value: ",
this.data.audioBooster,
);
const currentAudioVolume =
this.votVideoTranslationVolumeSlider.input.value;
this.votVideoTranslationVolumeSlider.input.max = this.data
.audioBooster
? maxAudioVolume
: 100;
if (!this.data.audioBooster) {
this.votVideoTranslationVolumeSlider.input.value =
currentAudioVolume > 100 ? 100 : currentAudioVolume;
this.votVideoTranslationVolumeSlider.input.dispatchEvent(
new Event("input"),
);
}
})();
});
this.votSyncVolumeCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.syncVolume = Number(e.target.checked);
await votStorage.set("syncVolume", this.data.syncVolume);
debug.log(
"syncVolume value changed. New value: ",
this.data.syncVolume,
);
})();
});
this.votTranslationServiceSelect.labelElement.addEventListener(
"change",
(e) => {
(async () => {
this.data.translateAPIErrors = Number(e.target.checked);
await votStorage.set(
"translateAPIErrors",
this.data.translateAPIErrors,
);
debug.log(
"translateAPIErrors value changed. New value: ",
this.data.translateAPIErrors,
);
})();
},
);
// SUBTITLES
this.votSubtitlesDetails.container.addEventListener("click", () => {
this.votSubtitlesDialog = ui.createDialog(
localizationProvider.get("VOTSubtitlesDesign"),
);
this.votSubtitlesDialog.container.classList.add("vot-dialog-temp");
this.votSubtitlesDialog.container.hidden = false;
// remove the modal so that they do not accumulate
this.votSubtitlesDialog.backdrop.onclick =
this.votSubtitlesDialog.closeButton.onclick = () => {
this.votSubtitlesDialog.container.remove();
};
// subtitles elements
this.votSubtitlesHighlightWordsCheckbox = ui.createCheckbox(
localizationProvider.get("VOTHighlightWords"),
this.data?.highlightWords ?? false,
);
this.votSubtitlesDialog.bodyContainer.appendChild(
this.votSubtitlesHighlightWordsCheckbox.container,
);
this.votSubtitlesMaxLengthSlider = ui.createSlider(
html`${localizationProvider.get("VOTSubtitlesMaxLength")}:
<strong>${this.data?.subtitlesMaxLength ?? 300}</strong>`,
this.data?.subtitlesMaxLength ?? 300,
50,
300,
);
this.votSubtitlesDialog.bodyContainer.appendChild(
this.votSubtitlesMaxLengthSlider.container,
);
this.votSubtitlesFontSizeSlider = ui.createSlider(
html`${localizationProvider.get("VOTSubtitlesFontSize")}:
<strong>${this.data?.subtitlesFontSize ?? 20}</strong>`,
this.data?.subtitlesFontSize ?? 20,
8,
50,
);
this.votSubtitlesDialog.bodyContainer.appendChild(
this.votSubtitlesFontSizeSlider.container,
);
this.votSubtitlesOpacitySlider = ui.createSlider(
html`${localizationProvider.get("VOTSubtitlesOpacity")}:
<strong>${this.data?.subtitlesOpacity ?? 20}</strong>`,
this.data?.subtitlesOpacity ?? 20,
0,
100,
);
this.votSubtitlesDialog.bodyContainer.appendChild(
this.votSubtitlesOpacitySlider.container,
);
// subtitles events
this.votSubtitlesHighlightWordsCheckbox.input.addEventListener(
"change",
(e) => {
(async () => {
this.data.highlightWords = Number(e.target.checked);
await votStorage.set("highlightWords", this.data.highlightWords);
debug.log(
"highlightWords value changed. New value: ",
this.data.highlightWords,
);
this.subtitlesWidget.setHighlightWords(this.data.highlightWords);
})();
},
);
this.votSubtitlesMaxLengthSlider.input.addEventListener(
"input",
(e) => {
(async () => {
this.data.subtitlesMaxLength = Number(e.target.value);
await votStorage.set(
"subtitlesMaxLength",
this.data.subtitlesMaxLength,
);
this.votSubtitlesMaxLengthSlider.label.querySelector(
"strong",
).textContent = `${this.data.subtitlesMaxLength}`;
this.subtitlesWidget.setMaxLength(this.data.subtitlesMaxLength);
})();
},
);
this.votSubtitlesFontSizeSlider.input.addEventListener("input", (e) => {
(async () => {
this.data.subtitlesFontSize = Number(e.target.value);
await votStorage.set(
"subtitlesFontSize",
this.data.subtitlesFontSize,
);
this.votSubtitlesFontSizeSlider.label.querySelector(
"strong",
).textContent = `${this.data.subtitlesFontSize}`;
this.subtitlesWidget.setFontSize(this.data.subtitlesFontSize);
})();
});
this.votSubtitlesOpacitySlider.input.addEventListener("input", (e) => {
(async () => {
this.data.subtitlesOpacity = Number(e.target.value);
await votStorage.set(
"subtitlesOpacity",
this.data.subtitlesOpacity,
);
this.votSubtitlesOpacitySlider.label.querySelector(
"strong",
).textContent = `${this.data.subtitlesOpacity}`;
this.subtitlesWidget.setOpacity(this.data.subtitlesOpacity);
})();
});
document.documentElement.appendChild(this.votSubtitlesDialog.container);
});
// OTHER
this.votShowPiPButtonCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.showPiPButton = Number(e.target.checked);
await votStorage.set("showPiPButton", this.data.showPiPButton);
debug.log(
"showPiPButton value changed. New value: ",
this.data.showPiPButton,
);
this.votButton.pipButton.hidden =
!isPiPAvailable() || !this.data.showPiPButton;
this.votButton.separator2.hidden =
!isPiPAvailable() || !this.data.showPiPButton;
})();
});
// PROXY
this.votM3u8ProxyHostTextfield.input.addEventListener("change", (e) => {
(async () => {
this.data.m3u8ProxyHost = e.target.value || m3u8ProxyHost;
await votStorage.set("m3u8ProxyHost", this.data.m3u8ProxyHost);
debug.log(
"m3u8ProxyHost value changed. New value: ",
this.data.m3u8ProxyHost,
);
})();
});
this.votProxyWorkerHostTextfield.input.addEventListener("change", (e) => {
(async () => {
this.data.proxyWorkerHost = e.target.value || proxyWorkerHost;
await votStorage.set("proxyWorkerHost", this.data.proxyWorkerHost);
debug.log(
"proxyWorkerHost value changed. New value: ",
this.data.proxyWorkerHost,
);
this.votClient.host = this.data.proxyWorkerHost;
})();
});
this.votAudioProxyCheckbox.input.addEventListener("change", (e) => {
(async () => {
this.data.audioProxy = Number(e.target.checked);
await votStorage.set("audioProxy", this.data.audioProxy);
debug.log(
"audioProxy value changed. New value: ",
this.data.audioProxy,
);
})();
});
this.votResetSettingsButton.addEventListener("click", () => {
(async () => {
localizationProvider.reset();
const valuesForClear = await votStorage.list();
for (let i = 0; i < valuesForClear.length; i++) {
const v = valuesForClear[i];
if (!localizationProvider.gmValues.includes(v)) {
votStorage.syncDelete(v);
}
}
window.location.reload();
})();
});
}
}
To make your code fully asynchronous and leverage Promise.all, you need to look for all the places in your code where you are performing asynchronous operations (e.g., await or Promise based calls) and then run these operations in parallel if they don't depend on each other. Below is a refactored version of your code to make it fully async with Promise.all.
async initUI() {
const [
translateVideo,
VOTSettings,
langs,
videoLanguage,
translationLanguage,
VOTSubtitlesDisabled,
VOTSubtitles,
VOTVolume,
VOTVolumeTranslation,
VOTAutoTranslate,
VOTDontTranslateYourLang,
VOTSyncVolume,
VOTTranslationService,
VOTDetectService,
VOTSubtitlesDesign,
VOTM3u8ProxyHost,
VOTProxyWorkerHost,
VOTAudioProxy,
VOTVersion,
VOTAuthors,
VOTLoader,
VOTBrowser,
localizationLang,
resetSettings,
] = await Promise.all([
localizationProvider.get("translateVideo"),
localizationProvider.get("VOTSettings"),
localizationProvider.get("langs"),
localizationProvider.get("videoLanguage"),
localizationProvider.get("translationLanguage"),
localizationProvider.get("VOTSubtitlesDisabled"),
localizationProvider.get("VOTSubtitles"),
localizationProvider.get("VOTVolume"),
localizationProvider.get("VOTVolumeTranslation"),
localizationProvider.get("VOTAutoTranslate"),
localizationProvider.get("VOTDontTranslateYourLang"),
localizationProvider.get("VOTSyncVolume"),
localizationProvider.get("VOTTranslationService"),
localizationProvider.get("VOTDetectService"),
localizationProvider.get("VOTSubtitlesDesign"),
localizationProvider.get("VOTM3u8ProxyHost"),
localizationProvider.get("VOTProxyWorkerHost"),
localizationProvider.get("VOTAudioProxy"),
localizationProvider.get("VOTVersion"),
localizationProvider.get("VOTAuthors"),
localizationProvider.get("VOTLoader"),
localizationProvider.get("VOTBrowser"),
localizationProvider.get("VOTMenuLanguage"),
localizationProvider.get("resetSettings"),
]);
// VOT Button
{
this.votButton = ui.createVOTButton(translateVideo);
this.votButton.container.style.opacity = 0;
if (this.data?.buttonPos && this.data?.buttonPos !== "default" && this.container.clientWidth && this.container.clientWidth > 550) {
this.votButton.container.dataset.direction = "column";
this.votButton.container.dataset.position = this.data?.buttonPos;
} else {
this.votButton.container.dataset.direction = "row";
this.votButton.container.dataset.position = "default";
}
this.container.appendChild(this.votButton.container);
this.votButton.pipButton.hidden = !isPiPAvailable() || !this.data?.showPiPButton;
this.votButton.separator2.hidden = !isPiPAvailable() || !this.data?.showPiPButton;
this.votButton.container.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
});
}
// VOT Menu
{
this.votMenu = ui.createVOTMenu(VOTSettings);
this.votMenu.container.dataset.position =
this.container.clientWidth && this.container.clientWidth > 550
? this.data?.buttonPos
: "default";
this.container.appendChild(this.votMenu.container);
this.votDownloadButton = ui.createIconButton(
svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="100%" viewBox="0 -960 960 960"><path d="M480-337q-8 0-15-2.5t-13-8.5L308-492q-12-12-11.5-28t11.5-28q12-12 28.5-12.5T365-549l75 75v-286q0-17 11.5-28.5T480-800q17 0 28.5 11.5T520-760v286l75-75q12-12 28.5-11.5T652-548q11 12 11.5 28T652-492L508-348q-6 6-13 8.5t-15 2.5ZM240-160q-33 0-56.5-23.5T160-240v-80q0-17 11.5-28.5T200-360q17 0 28.5 11.5T240-320v80h480v-80q0-17 11.5-28.5T760-360q17 0 28.5 11.5T800-320v80q0 33-23.5 56.5T720-160H240Z"/></svg>`
);
this.votDownloadButton.hidden = true;
this.votMenu.headerContainer.appendChild(this.votDownloadButton);
this.votDownloadSubtitlesButton = ui.createIconButton(
svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="100%" viewBox="0 0 24 24"><path d="M4 20q-.825 0-1.413-.588T2 18V6q0-.825.588-1.413T4 4h16q.825 0 1.413.588T22 6v12q0 .825-.588 1.413T20 20H4Zm2-4h8v-2H6v2Zm10 0h2v-2h-2v2ZM6 12h2v-2H6v2Zm4 0h8v-2h-8v2Z"/></svg>`
);
this.votDownloadSubtitlesButton.hidden = true;
this.votMenu.headerContainer.appendChild(this.votDownloadSubtitlesButton);
this.votSettingsButton = ui.createIconButton(
svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="100%" viewBox="0 -960 960 960"><path d="M555-80H405q-15 0-26-10t-13-25l-12-93q-13-5-24.5-12t-22.5-15l-87 36q-14 5-28 1t-22-17L96-344q-8-13-5-28t15-24l75-57q-1-7-1-13.5v-27q0-6.5 1-13.5l-75-57q-12-9-15-24t5-28l74-129q7-14 21.5-17.5T220-761l87 36q11-8 23-15t24-12l12-93q2-15 13-25t26-10h150q15 0 26 10t13 25l12 93q13 5 24.5 12t22.5 15l87-36q14-5 28-1t22 17l74 129q8 13 5 28t-15 24l-75 57q1 7 1 13.5v27q0 6.5-2 13.5l75 57q12 9 15 24t-5 28l-74 128q-8 13-22.5 17.5T738-199l-85-36q-11 8-23 15t-24 12l-12 93q-2 15-13 25t-26 10Zm-73-260q58 0 99-41t41-99q0-58-41-99t-99-41q-59 0-99.5 41T342-480q0 58 40.5 99t99.5 41Zm0-80q-25 0-42.5-17.5T422-480q0-25 17.5-42.5T482-540q25 0 42.5 17.5T542-480q0 25-17.5 42.5T482-420Zm-2-60Zm-40 320h79l14-106q31-8 57.5-23.5T639-327l99 41 39-68-86-65q5-14 7-29.5t2-31.5q0-16-2-31.5t-7-29.5l86-65-39-68-99 42q-22-23-48.5-38.5T533-694l-13-106h-79l-14 106q-31 8-57.5 23.5T321-633l-99-41-39 68 86 64q-5 15-7 30t-2 32q0 16 2 31t7 30l-86 65 39 68 99-42q22 23 48.5 38.5T427-266l13 106Z"/></svg>`
);
this.votMenu.headerContainer.appendChild(this.votSettingsButton);
this.votTranslationLanguageSelect = ui.createVOTLanguageSelect({
fromTitle: langs[this.video.detectedLanguage],
fromDialogTitle: videoLanguage,
fromItems: genOptionsByOBJ(availableLangs, this.videoData.detectedLanguage),
fromOnSelectCB: async (e) => {
debug.log("[fromOnSelectCB] select from language", e.target.dataset.votValue);
this.videoData = await this.getVideoData();
this.setSelectMenuValues(e.target.dataset.votValue, this.videoData.responseLanguage);
},
toTitle: langs[this.video.responseLanguage],
toDialogTitle: translationLanguage,
toItems: genOptionsByOBJ(availableTTS, this.videoData.responseLanguage),
toOnSelectCB: async (e) => {
const newLang = e.target.dataset.votValue;
debug.log("[toOnSelectCB] select to language", newLang);
this.data.responseLanguage = this.translateToLang = newLang;
await votStorage.set("responseLanguage", this.data.responseLanguage);
debug.log("Response Language value changed. New value: ", this.data.responseLanguage);
this.videoData = await this.getVideoData();
this.setSelectMenuValues(this.videoData.detectedLanguage, this.data.responseLanguage);
},
});
this.votMenu.bodyContainer.appendChild(this.votTranslationLanguageSelect.container);
this.votSubtitlesSelect = ui.createVOTSelect(
VOTSubtitlesDisabled,
VOTSubtitles,
[
{
label: VOTSubtitlesDisabled,
value: "disabled",
selected: true,
disabled: false,
},
],
{
onSelectCb: async (e) => {
await this.changeSubtitlesLang(e.target.dataset.votValue);
},
labelElement: ui.createVOTSelectLabel(VOTSubtitles),
},
);
this.votMenu.bodyContainer.appendChild(this.votSubtitlesSelect.container);
this.votVideoVolumeSlider = ui.createSlider(html`${VOTVolume}:<strong>${this.getVideoVolume() * 100}%</strong>`, this.getVideoVolume() * 100);
this.votVideoVolumeSlider.container.hidden =
this.data.showVideoSlider !== 1 || this.votButton.container.dataset.status !== "success";
this.votMenu.bodyContainer.appendChild(this.votVideoVolumeSlider.container);
this.votVideoTranslationVolumeSlider = ui.createSlider(
html`${VOTVolumeTranslation}:<strong>${this.data?.defaultVolume ?? 100}%</strong>`,
this.data?.defaultVolume ?? 100,
0,
this.data.audioBooster ? 300 : 100,
);
this.votVideoTranslationVolumeSlider.container.hidden =
this.votButton.container.dataset.status !== "success";
this.votMenu.bodyContainer.appendChild(this.votVideoTranslationVolumeSlider.container);
this.votMenu.container.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
});
}
// VOT Settings
{
this.votSettingsDialog = ui.createDialog(VOTSettings);
document.documentElement.appendChild(this.votSettingsDialog.container);
this.votTranslationHeader = ui.createHeader(localizationProvider.get("translationSettings"));
this.votSettingsDialog.bodyContainer.appendChild(this.votTranslationHeader);
this.votAutoTranslateCheckbox = ui.createCheckbox(VOTAutoTranslate, this.data?.autoTranslate ?? false);
this.votSettingsDialog.bodyContainer.appendChild(this.votAutoTranslateCheckbox.container);
const dontTranslateLanguage = votStorage.syncGet("dontTranslateLanguage", lang);
this.votDontTranslateYourLangSelect = ui.createVOTSelect(
langs[dontTranslateLanguage],
VOTDontTranslateYourLang,
genOptionsByOBJ(availableLangs, dontTranslateLanguage),
{
onSelectCb: async (e) => {
this.data.dontTranslateLanguage = e.target.dataset.votValue;
await votStorage.set("dontTranslateLanguage", this.data.dontTranslateLanguage);
},
labelElement: ui.createCheckbox(VOTDontTranslateYourLang, this.data?.dontTranslateYourLang ?? true).container,
},
);
this.votSettingsDialog.bodyContainer.appendChild(this.votDontTranslateYourLangSelect.container);
this.votAutoSetVolumeCheckbox = ui.createCheckbox(`${VOTAutoSetVolume}`, this.data?.autoSetVolumeYandexStyle ?? true);
this.votSettingsDialog.bodyContainer.appendChild(this.votAutoSetVolumeCheckbox.container);
this.votAutoSetVolumeSlider = ui.createSlider(
html`<strong>${(this.data?.autoVolume ?? 0.7) * 100}%</strong>`,
(this.data?.autoVolume ?? 0.7) * 100,
0,
100,
);
this.votSettingsDialog.bodyContainer.appendChild(this.votAutoSetVolumeSlider.container);
this.votShowVideoSliderCheckbox = ui.createCheckbox(localizationProvider.get("VOTShowVideoSlider"), this.data?.showVideoSlider ?? false);
this.votSettingsDialog.bodyContainer.appendChild(this.votShowVideoSliderCheckbox.container);
this.votAudioBoosterCheckbox = ui.createCheckbox(localizationProvider.get("VOTAudioBooster"), this.data?.audioBooster ?? false);
this.votAudioBoosterCheckbox.container.hidden = !this.audioContext;
this.votSettingsDialog.bodyContainer.appendChild(this.votAudioBoosterCheckbox.container);
this.votSyncVolumeCheckbox = ui.createCheckbox(VOTSyncVolume, this.data?.syncVolume ?? false);
this.votSettingsDialog.bodyContainer.appendChild(this.votSyncVolumeCheckbox.container);
const translationService = votStorage.syncGet("translationService", "defaultService");
this.votTranslationServiceSelect = ui.createVOTSelect(
translationService.toUpperCase(),
VOTTranslationService,
genOptionsByOBJ(translateServices, translationService),
{
onSelectCb: async (e) => {
this.data.translationService = e.target.dataset.votValue;
await votStorage.set("translationService", this.data.translationService);
},
labelElement: ui.createCheckbox(localizationProvider.get("VOTTranslateAPIErrors"), this.data.translateAPIErrors ?? true).container,
},
);
this.votTranslationServiceSelect.container.hidden = localizationProvider.lang === "ru";
this.votSettingsDialog.bodyContainer.appendChild(this.votTranslationServiceSelect.container);
const detectService = votStorage.syncGet("detectService", "defaultDetectService");
this.votDetectServiceSelect = ui.createVOTSelect(
detectService.toUpperCase(),
VOTDetectService,
genOptionsByOBJ(detectServices, detectService),
{
onSelectCb: async (e) => {
this.data.detectService = e.target.dataset.votValue;
await votStorage.set("detectService", this.data.detectService);
},
labelElement: ui.createVOTSelectLabel(VOTDetectService),
},
);
this.votSettingsDialog.bodyContainer.appendChild(this.votDetectServiceSelect.container);
// SUBTITLES
this.votSubtitlesHeader = ui.createHeader(localizationProvider.get("subtitlesSettings"));
this.votSettingsDialog.bodyContainer.appendChild(this.votSubtitlesHeader);
this.votSubtitlesDetails = ui.createDetails(VOTSubtitlesDesign);
this.votSettingsDialog.bodyContainer.appendChild(this.votSubtitlesDetails.container);
// PROXY
this.votProxyHeader = ui.createHeader(localizationProvider.get("proxySettings"));
this.votSettingsDialog.bodyContainer.appendChild(this.votProxyHeader);
this.votM3u8ProxyHostTextfield = ui.createTextfield(VOTM3u8ProxyHost, this.data?.m3u8ProxyHost, "defaultHost");
this.votSettingsDialog.bodyContainer.appendChild(this.votM3u8ProxyHostTextfield.container);
this.votProxyWorkerHostTextfield = ui.createTextfield(VOTProxyWorkerHost, this.data?.proxyWorkerHost, "defaultWorkerHost");
this.votSettingsDialog.bodyContainer.appendChild(this.votProxyWorkerHostTextfield.container);
this.votAudioProxyCheckbox = ui.createCheckbox(VOTAudioProxy, this.data?.audioProxy ?? false);
this.votSettingsDialog.bodyContainer.appendChild(this.votAudioProxyCheckbox.container);
// ABOUT
this.votAboutHeader = ui.createHeader(localizationProvider.get("about"));
this.votSettingsDialog.bodyContainer.appendChild(this.votAboutHeader);
const localeLang = votStorage.syncGet("locale-lang-override", "auto");
this.votLanguageSelect = ui.createVOTSelect(
langs[localeLang],
localizationLang,
genOptionsByOBJ(availableLocales, localeLang),
{
onSelectCb: async (e) => {
await votStorage.set("locale-lang-override", e.target.dataset.votValue);
},
labelElement: ui.createVOTSelectLabel(localizationLang),
},
);
this.votSettingsDialog.bodyContainer.appendChild(this.votLanguageSelect.container);
this.votShowPiPButtonCheckbox = ui.createCheckbox(localizationProvider.get("VOTShowPiPButton"), this.data?.showPiPButton ?? false);
this.votShowPiPButtonCheckbox.container.hidden = !isPiPAvailable();
this
Not yet included in a published release.