diff --git a/plugins/quality-changer/front.js b/plugins/quality-changer/front.js new file mode 100644 index 00000000..d167776c --- /dev/null +++ b/plugins/quality-changer/front.js @@ -0,0 +1,41 @@ +const { ElementFromFile, templatePath } = require("../utils"); +const dialog = require('electron').remote.dialog + +function $(selector) { return document.querySelector(selector); } + +const qualitySettingsButton = ElementFromFile( + templatePath(__dirname, "qualitySettingsTemplate.html") +); + + +module.exports = () => { + document.addEventListener('apiLoaded', setup); +} + +function setup(event) { + const api = event.detail; + + $('.top-row-buttons.ytmusic-player').prepend(qualitySettingsButton); + + qualitySettingsButton.onclick = function chooseQuality() { + if (api.getPlayerState() === 2) api.playVideo(); + else if (api.getPlayerState() === 1) api.pauseVideo(); + + const currentIndex = api.getAvailableQualityLevels().indexOf(api.getPlaybackQuality()) + + dialog.showMessageBox({ + type: "question", + buttons: api.getAvailableQualityLabels(), + defaultId: currentIndex, + title: "Choose Video Quality", + message: "Choose Video Quality:", + detail: `Current Quality: ${api.getAvailableQualityLabels()[currentIndex]}`, + cancelId: -1 + }).then((promise) => { + if (promise.response === -1) return; + const newQuality = api.getAvailableQualityLevels()[promise.response]; + api.setPlaybackQualityRange(newQuality); + api.setPlaybackQuality(newQuality) + }) + } +} diff --git a/plugins/quality-changer/templates/qualitySettingsTemplate.html b/plugins/quality-changer/templates/qualitySettingsTemplate.html new file mode 100644 index 00000000..64c012fe --- /dev/null +++ b/plugins/quality-changer/templates/qualitySettingsTemplate.html @@ -0,0 +1,13 @@ + + + + + + + + \ No newline at end of file diff --git a/preload.js b/preload.js index 5a09c845..67ae8f7c 100644 --- a/preload.js +++ b/preload.js @@ -10,6 +10,8 @@ const setupSongInfo = require("./providers/song-info-front"); const plugins = config.plugins.getEnabled(); +let api; + plugins.forEach(([plugin, options]) => { const preloadPath = path.join(__dirname, "plugins", plugin, "preload.js"); fileExists(preloadPath, () => { @@ -38,6 +40,9 @@ document.addEventListener("DOMContentLoaded", () => { }); }); + // wait for complete load of youtube api + listenForApiLoad(); + // inject song-info provider setupSongInfo(); @@ -51,3 +56,25 @@ document.addEventListener("DOMContentLoaded", () => { global.reload = () => remote.getCurrentWindow().webContents.loadURL(config.get("url")); }); + +function listenForApiLoad() { + api = document.querySelector('#movie_player'); + if (api) { + onApiLoaded(); + return; + } + + const observer = new MutationObserver(() => { + api = document.querySelector('#movie_player'); + if (api) { + observer.disconnect(); + onApiLoaded(); + } + }) + + observer.observe(document.documentElement, { childList: true, subtree: true }); +} + +function onApiLoaded() { + document.dispatchEvent(new CustomEvent('apiLoaded', { detail: api })); +}