[captions-selector] add autoload option

This commit is contained in:
Araxeus
2023-03-16 20:36:20 +02:00
parent 7018481b1d
commit b45c628142
3 changed files with 30 additions and 12 deletions

View File

@ -1,25 +1,25 @@
const { ElementFromFile, templatePath } = require("../utils");
const { ipcRenderer } = require("electron");
const config = require("./config");
function $(selector) { return document.querySelector(selector); }
const captionsSettingsButton = ElementFromFile(
templatePath(__dirname, "captions-settings-template.html")
);
module.exports = (options) => {
document.addEventListener('apiLoaded', (event) => setup(event, options), { once: true, passive: true });
module.exports = () => {
document.addEventListener('apiLoaded', (event) => setup(event.detail), { once: true, passive: true });
}
function setup(event, options) {
const api = event.detail;
function setup(api) {
$(".right-controls-buttons").append(captionsSettingsButton);
let captionTrackList = api.getOption("captions", "tracklist");
$("video").addEventListener("srcChanged", () => {
if (options.disableCaptions) {
$("video").addEventListener("srcChanged", async () => {
if (await config.get('disableCaptions')) {
setTimeout(() => api.unloadModule("captions"), 100);
captionsSettingsButton.style.display = "none";
return;
@ -27,9 +27,15 @@ function setup(event, options) {
api.loadModule("captions");
setTimeout(() => {
setTimeout(async () => {
captionTrackList = api.getOption("captions", "tracklist");
if (await config.get("autoload") && await config.get("lastCaptionsCode")) {
api.setOption("captions", "track", {
languageCode: await config.get("lastCaptionsCode"),
});
}
captionsSettingsButton.style.display = captionTrackList?.length
? "inline-block"
: "none";
@ -52,6 +58,7 @@ function setup(event, options) {
if (currentIndex === null) return;
const newCaptions = captionTrackList[currentIndex];
config.set('lastCaptionsCode', newCaptions?.languageCode);
if (newCaptions) {
api.setOption("captions", "track", { languageCode: newCaptions.languageCode });
} else {