From c281b8ba987867c5533b5fd7ad7e057240bd9f40 Mon Sep 17 00:00:00 2001 From: Fermin Cirella Date: Sat, 22 Oct 2022 01:00:15 -0300 Subject: [PATCH] Plugin: Captions Selector --- plugins/captions-selector/back.js | 15 +++++++++ plugins/captions-selector/front.js | 31 +++++++++++++++++++ .../templates/captionsSettingsTemplate.html | 13 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 plugins/captions-selector/back.js create mode 100644 plugins/captions-selector/front.js create mode 100644 plugins/captions-selector/templates/captionsSettingsTemplate.html diff --git a/plugins/captions-selector/back.js b/plugins/captions-selector/back.js new file mode 100644 index 00000000..533cceca --- /dev/null +++ b/plugins/captions-selector/back.js @@ -0,0 +1,15 @@ +const { ipcMain, dialog } = require("electron"); + +module.exports = () => { + ipcMain.handle('captionsSelector', async (_, captionLabels, currentIndex) => { + return await dialog.showMessageBox({ + type: "question", + buttons: captionLabels, + defaultId: currentIndex, + title: "Choose Caption", + message: "Choose Caption:", + detail: `Current Caption: ${captionLabels[currentIndex]}`, + cancelId: -1 + }) + }) +}; diff --git a/plugins/captions-selector/front.js b/plugins/captions-selector/front.js new file mode 100644 index 00000000..62324514 --- /dev/null +++ b/plugins/captions-selector/front.js @@ -0,0 +1,31 @@ +const { ElementFromFile, templatePath } = require("../utils"); +const { ipcRenderer } = require("electron"); + +const captionsSettingsButton = ElementFromFile( + templatePath(__dirname, "captionsSettingsTemplate.html") +); + +module.exports = () => { + document.addEventListener('apiLoaded', setup, { once: true, passive: true }); +} + +function setup(event) { + const api = event.detail; + + document.querySelector('.right-controls-buttons').append(captionsSettingsButton); + + captionsSettingsButton.onclick = function chooseQuality() { + api.pauseVideo(); + + const currentCaptionTrack = api.getOption("captions", "track"); + const captionTrackList = api.getOption("captions", "tracklist"); + + const currentIndex = captionTrackList.indexOf(captionTrackList.find(track => track.languageCode === currentCaptionTrack.languageCode)); + + ipcRenderer.invoke('captionsSelector', captionTrackList.map(track => track.displayName), currentIndex).then(promise => { + if (promise.response === -1) return; + const newCaptions = captionTrackList[promise.response]; + api.setOption("captions", "track", { languageCode: newCaptions.languageCode }); + }); + } +} diff --git a/plugins/captions-selector/templates/captionsSettingsTemplate.html b/plugins/captions-selector/templates/captionsSettingsTemplate.html new file mode 100644 index 00000000..bfa34e4d --- /dev/null +++ b/plugins/captions-selector/templates/captionsSettingsTemplate.html @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file