|
| 1 | +const { nativeImage } = require("electron"); |
| 2 | + |
| 3 | +const fetch = require("node-fetch"); |
| 4 | + |
| 5 | +// This selects the song title |
| 6 | +const titleSelector = ".title.style-scope.ytmusic-player-bar"; |
| 7 | + |
| 8 | +// This selects the song image |
| 9 | +const imageSelector = |
| 10 | + "#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > img"; |
| 11 | + |
| 12 | +// This selects the song subinfo, this includes artist, views, likes |
| 13 | +const subInfoSelector = |
| 14 | + "#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > div.content-info-wrapper.style-scope.ytmusic-player-bar > span"; |
| 15 | + |
| 16 | +// Grab the title using the selector |
| 17 | +const getTitle = (win) => { |
| 18 | + return win.webContents |
| 19 | + .executeJavaScript( |
| 20 | + "document.querySelector('" + titleSelector + "').innerText" |
| 21 | + ) |
| 22 | + .catch((error) => { |
| 23 | + console.log(error); |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +// Grab the image src using the selector |
| 28 | +const getImageSrc = (win) => { |
| 29 | + return win.webContents |
| 30 | + .executeJavaScript("document.querySelector('" + imageSelector + "').src") |
| 31 | + .catch((error) => { |
| 32 | + console.log(error); |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +// Grab the subinfo using the selector |
| 37 | +const getSubInfo = async (win) => { |
| 38 | + // Get innerText of subinfo element |
| 39 | + const subInfoString = await win.webContents.executeJavaScript( |
| 40 | + 'document.querySelector("' + subInfoSelector + '").innerText' |
| 41 | + ); |
| 42 | + |
| 43 | + // Split and clean the string |
| 44 | + const splittedSubInfo = subInfoString.replaceAll("\n", "").split(" • "); |
| 45 | + |
| 46 | + // Make sure we always return 3 elements in the aray |
| 47 | + const subInfo = []; |
| 48 | + for (let i = 0; i < 3; i++) { |
| 49 | + // Fill array with empty string if not defined |
| 50 | + subInfo.push(splittedSubInfo[i] || ""); |
| 51 | + } |
| 52 | + |
| 53 | + return subInfo; |
| 54 | +}; |
| 55 | + |
| 56 | +// Grab the native image using the src |
| 57 | +const getImage = async (src) => { |
| 58 | + const result = await fetch(src); |
| 59 | + const buffer = await result.buffer(); |
| 60 | + return nativeImage.createFromBuffer(buffer); |
| 61 | +}; |
| 62 | + |
| 63 | +const getPausedStatus = async (win) => { |
| 64 | + const title = await win.webContents.executeJavaScript("document.title"); |
| 65 | + return !title.includes("-"); |
| 66 | +}; |
| 67 | + |
| 68 | +// Fill songInfo with empty values |
| 69 | +const songInfo = { |
| 70 | + title: "", |
| 71 | + artist: "", |
| 72 | + views: "", |
| 73 | + likes: "", |
| 74 | + imageSrc: "", |
| 75 | + image: null, |
| 76 | + isPaused: true, |
| 77 | +}; |
| 78 | + |
| 79 | +const registerProvider = (win) => { |
| 80 | + // This variable will be filled with the callbacks once they register |
| 81 | + const callbacks = []; |
| 82 | + |
| 83 | + // This function will allow plugins to register callback that will be triggered when data changes |
| 84 | + const registerCallback = (callback) => { |
| 85 | + callbacks.push(callback); |
| 86 | + }; |
| 87 | + |
| 88 | + win.on("page-title-updated", async () => { |
| 89 | + // Save the old title temporarily |
| 90 | + const oldTitle = songInfo.title; |
| 91 | + // Get and set the new data |
| 92 | + songInfo.title = await getTitle(win); |
| 93 | + songInfo.isPaused = await getPausedStatus(win); |
| 94 | + |
| 95 | + // If title changed then we do need to update other info |
| 96 | + if (oldTitle !== songInfo.title) { |
| 97 | + const subInfo = await getSubInfo(win); |
| 98 | + songInfo.artist = subInfo[0]; |
| 99 | + songInfo.views = subInfo[1]; |
| 100 | + songInfo.likes = subInfo[2]; |
| 101 | + songInfo.imageSrc = await getImageSrc(win); |
| 102 | + songInfo.image = await getImage(songInfo.imageSrc); |
| 103 | + } |
| 104 | + |
| 105 | + // Trigger the callbacks |
| 106 | + callbacks.forEach((c) => { |
| 107 | + c(songInfo); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + return registerCallback; |
| 112 | +}; |
| 113 | + |
| 114 | +module.exports = registerProvider; |
0 commit comments