From c281b8ba987867c5533b5fd7ad7e057240bd9f40 Mon Sep 17 00:00:00 2001 From: Fermin Cirella Date: Sat, 22 Oct 2022 01:00:15 -0300 Subject: [PATCH 1/4] 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 From f58c10b02d856f36b79bceb736e872f9489f44ec Mon Sep 17 00:00:00 2001 From: Fermin Cirella Date: Sat, 22 Oct 2022 01:01:25 -0300 Subject: [PATCH 2/4] Plugin Captions Selector - Add new line --- .../captions-selector/templates/captionsSettingsTemplate.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/captions-selector/templates/captionsSettingsTemplate.html b/plugins/captions-selector/templates/captionsSettingsTemplate.html index bfa34e4d..16c1b2b6 100644 --- a/plugins/captions-selector/templates/captionsSettingsTemplate.html +++ b/plugins/captions-selector/templates/captionsSettingsTemplate.html @@ -10,4 +10,4 @@ - \ No newline at end of file + From c8a852bf2ece75a4e07039d49d6a425b03deb536 Mon Sep 17 00:00:00 2001 From: Fermin Cirella Date: Sat, 22 Oct 2022 01:13:04 -0300 Subject: [PATCH 3/4] Plugin Captions Selector - Check if there is caption tracks available, add "disable captions" --- plugins/captions-selector/front.js | 33 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/plugins/captions-selector/front.js b/plugins/captions-selector/front.js index 62324514..f8c83cb9 100644 --- a/plugins/captions-selector/front.js +++ b/plugins/captions-selector/front.js @@ -15,17 +15,32 @@ function setup(event) { 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)); + if (captionTrackList?.length) { + api.pauseVideo(); - 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 }); - }); + const currentCaptionTrack = api.getOption("captions", "track"); + const currentIndex = captionTrackList.indexOf(captionTrackList.find(track => track.languageCode === currentCaptionTrack.languageCode)); + + const captionLabels = [ + ...captionTrackList.map(track => track.displayName), + 'None' + ]; + + ipcRenderer.invoke('captionsSelector', captionLabels, currentIndex).then(promise => { + if (promise.response === -1) return; + + const newCaptions = captionTrackList[promise.response]; + if (newCaptions) { + api.loadModule("captions"); + api.setOption("captions", "track", { languageCode: newCaptions.languageCode }); + } else { + api.unloadModule("captions"); + } + + setTimeout(() => api.playVideo()); + }); + } } } From 938210e8f902fe2aae71d07787efb5ec197d4dc2 Mon Sep 17 00:00:00 2001 From: Fermin Cirella Date: Sat, 22 Oct 2022 01:49:16 -0300 Subject: [PATCH 4/4] Plugin Captions Selector - Add disable captions by default --- config/defaults.js | 4 ++++ plugins/captions-selector/back.js | 4 ++-- plugins/captions-selector/front.js | 28 +++++++++++++++++++++------- plugins/captions-selector/menu.js | 12 ++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 plugins/captions-selector/menu.js diff --git a/config/defaults.js b/config/defaults.js index 73c49649..a617c264 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -91,6 +91,10 @@ const defaultConfig = { "saveSize": false, "hotkey": "P" }, + "captions-selector": { + enabled: false, + disableCaptions: false + } }, }; diff --git a/plugins/captions-selector/back.js b/plugins/captions-selector/back.js index 533cceca..de53d85a 100644 --- a/plugins/captions-selector/back.js +++ b/plugins/captions-selector/back.js @@ -1,8 +1,8 @@ const { ipcMain, dialog } = require("electron"); module.exports = () => { - ipcMain.handle('captionsSelector', async (_, captionLabels, currentIndex) => { - return await dialog.showMessageBox({ + ipcMain.handle('captionsSelector', async (_, captionLabels, currentIndex) => { + return await dialog.showMessageBox({ type: "question", buttons: captionLabels, defaultId: currentIndex, diff --git a/plugins/captions-selector/front.js b/plugins/captions-selector/front.js index f8c83cb9..9ae9dbde 100644 --- a/plugins/captions-selector/front.js +++ b/plugins/captions-selector/front.js @@ -1,30 +1,44 @@ const { ElementFromFile, templatePath } = require("../utils"); const { ipcRenderer } = require("electron"); +function $(selector) { return document.querySelector(selector); } + const captionsSettingsButton = ElementFromFile( templatePath(__dirname, "captionsSettingsTemplate.html") ); -module.exports = () => { - document.addEventListener('apiLoaded', setup, { once: true, passive: true }); +module.exports = (options) => { + document.addEventListener('apiLoaded', (event) => setup(event, options), { once: true, passive: true }); } -function setup(event) { +/** + * If captions are disabled by default, + * unload "captions" module when video changes. + */ +const videoChanged = (api, options) => { + if (options.disableCaptions) { + setTimeout(() => api.unloadModule("captions"), 100); + } +} + +function setup(event, options) { const api = event.detail; - document.querySelector('.right-controls-buttons').append(captionsSettingsButton); + $("video").addEventListener("srcChanged", () => videoChanged(api, options)); + + $(".right-controls-buttons").append(captionsSettingsButton); captionsSettingsButton.onclick = function chooseQuality() { + api.loadModule("captions"); + const captionTrackList = api.getOption("captions", "tracklist"); if (captionTrackList?.length) { - api.pauseVideo(); - const currentCaptionTrack = api.getOption("captions", "track"); const currentIndex = captionTrackList.indexOf(captionTrackList.find(track => track.languageCode === currentCaptionTrack.languageCode)); const captionLabels = [ - ...captionTrackList.map(track => track.displayName), + ...captionTrackList.map(track => track.displayName), 'None' ]; diff --git a/plugins/captions-selector/menu.js b/plugins/captions-selector/menu.js new file mode 100644 index 00000000..439ff8b4 --- /dev/null +++ b/plugins/captions-selector/menu.js @@ -0,0 +1,12 @@ +const { setOptions } = require('../../config/plugins'); + +module.exports = (_win, options) => [ + { + label: "No captions by default", + type: "checkbox", + checked: options.disabledCaptions, + click: (item) => { + setOptions("captions-selector", { disableCaptions: item.checked }); + }, + } +];