Plugin Captions Selector - Add disable captions by default

This commit is contained in:
Fermin Cirella
2022-10-22 01:49:16 -03:00
parent c8a852bf2e
commit 938210e8f9
4 changed files with 39 additions and 9 deletions

View File

@ -91,6 +91,10 @@ const defaultConfig = {
"saveSize": false, "saveSize": false,
"hotkey": "P" "hotkey": "P"
}, },
"captions-selector": {
enabled: false,
disableCaptions: false
}
}, },
}; };

View File

@ -1,8 +1,8 @@
const { ipcMain, dialog } = require("electron"); const { ipcMain, dialog } = require("electron");
module.exports = () => { module.exports = () => {
ipcMain.handle('captionsSelector', async (_, captionLabels, currentIndex) => { ipcMain.handle('captionsSelector', async (_, captionLabels, currentIndex) => {
return await dialog.showMessageBox({ return await dialog.showMessageBox({
type: "question", type: "question",
buttons: captionLabels, buttons: captionLabels,
defaultId: currentIndex, defaultId: currentIndex,

View File

@ -1,30 +1,44 @@
const { ElementFromFile, templatePath } = require("../utils"); const { ElementFromFile, templatePath } = require("../utils");
const { ipcRenderer } = require("electron"); const { ipcRenderer } = require("electron");
function $(selector) { return document.querySelector(selector); }
const captionsSettingsButton = ElementFromFile( const captionsSettingsButton = ElementFromFile(
templatePath(__dirname, "captionsSettingsTemplate.html") templatePath(__dirname, "captionsSettingsTemplate.html")
); );
module.exports = () => { module.exports = (options) => {
document.addEventListener('apiLoaded', setup, { once: true, passive: true }); 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; 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() { captionsSettingsButton.onclick = function chooseQuality() {
api.loadModule("captions");
const captionTrackList = api.getOption("captions", "tracklist"); const captionTrackList = api.getOption("captions", "tracklist");
if (captionTrackList?.length) { if (captionTrackList?.length) {
api.pauseVideo();
const currentCaptionTrack = api.getOption("captions", "track"); const currentCaptionTrack = api.getOption("captions", "track");
const currentIndex = captionTrackList.indexOf(captionTrackList.find(track => track.languageCode === currentCaptionTrack.languageCode)); const currentIndex = captionTrackList.indexOf(captionTrackList.find(track => track.languageCode === currentCaptionTrack.languageCode));
const captionLabels = [ const captionLabels = [
...captionTrackList.map(track => track.displayName), ...captionTrackList.map(track => track.displayName),
'None' 'None'
]; ];

View File

@ -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 });
},
}
];