@@ -4,32 +4,6 @@ const fetch = require("node-fetch");
4
4
5
5
const config = require ( "../config" ) ;
6
6
7
- // Grab the progress using the selector
8
- const getProgress = async ( win ) => {
9
- // Get current value of the progressbar element
10
- return win . webContents . executeJavaScript (
11
- 'document.querySelector("#progress-bar").value'
12
- ) ;
13
- } ;
14
-
15
- // Grab the native image using the src
16
- const getImage = async ( src ) => {
17
- const result = await fetch ( src ) ;
18
- const buffer = await result . buffer ( ) ;
19
- const output = nativeImage . createFromBuffer ( buffer ) ;
20
- if ( output . isEmpty ( ) && ! src . endsWith ( ".jpg" ) && src . includes ( ".jpg" ) ) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
21
- return getImage ( src . slice ( 0 , src . lastIndexOf ( ".jpg" ) + 4 ) ) ;
22
- } else {
23
- return output ;
24
- }
25
- } ;
26
-
27
- // To find the paused status, we check if the title contains `-`
28
- const getPausedStatus = async ( win ) => {
29
- const title = await win . webContents . executeJavaScript ( "document.title" ) ;
30
- return ! title . includes ( "-" ) ;
31
- } ;
32
-
33
7
// Fill songInfo with empty values
34
8
/**
35
9
* @typedef {songInfo } SongInfo
@@ -47,21 +21,48 @@ const songInfo = {
47
21
url : "" ,
48
22
} ;
49
23
24
+ // Grab the native image using the src
25
+ const getImage = async ( src ) => {
26
+ const result = await fetch ( src ) ;
27
+ const buffer = await result . buffer ( ) ;
28
+ const output = nativeImage . createFromBuffer ( buffer ) ;
29
+ if ( output . isEmpty ( ) && ! src . endsWith ( ".jpg" ) && src . includes ( ".jpg" ) ) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
30
+ return getImage ( src . slice ( 0 , src . lastIndexOf ( ".jpg" ) + 4 ) ) ;
31
+ } else {
32
+ return output ;
33
+ }
34
+ } ;
35
+
50
36
const handleData = async ( responseText , win ) => {
51
- let data = JSON . parse ( responseText ) ;
52
- songInfo . title = cleanupName ( data ?. videoDetails ?. title ) ;
53
- songInfo . artist = cleanupName ( data ?. videoDetails ?. author ) ;
54
- songInfo . views = data ?. videoDetails ?. viewCount ;
55
- songInfo . imageSrc = data ?. videoDetails ?. thumbnail ?. thumbnails ?. pop ( ) ?. url . split ( "?" ) [ 0 ] ;
56
- songInfo . songDuration = data ?. videoDetails ?. lengthSeconds ;
57
- songInfo . image = await getImage ( songInfo . imageSrc ) ;
58
- songInfo . uploadDate = data ?. microformat ?. microformatDataRenderer ?. uploadDate ;
59
- songInfo . url = data ?. microformat ?. microformatDataRenderer ?. urlCanonical ?. split ( "&" ) [ 0 ] ;
60
-
61
- // used for options.resumeOnStart
62
- config . set ( "url" , data ?. microformat ?. microformatDataRenderer ?. urlCanonical ) ;
63
-
64
- win . webContents . send ( "update-song-info" , JSON . stringify ( songInfo ) ) ;
37
+ const data = JSON . parse ( responseText ) ;
38
+ if ( ! data ) return ;
39
+
40
+ const microformat = data . microformat ?. microformatDataRenderer ;
41
+ if ( microformat ) {
42
+ songInfo . uploadDate = microformat . uploadDate ;
43
+ songInfo . url = microformat . urlCanonical ?. split ( "&" ) [ 0 ] ;
44
+
45
+ // used for options.resumeOnStart
46
+ config . set ( "url" , microformat . urlCanonical ) ;
47
+ }
48
+
49
+ const videoDetails = data . videoDetails ;
50
+ if ( videoDetails ) {
51
+ songInfo . title = cleanupName ( videoDetails . title ) ;
52
+ songInfo . artist = cleanupName ( videoDetails . author ) ;
53
+ songInfo . views = videoDetails . viewCount ;
54
+ songInfo . songDuration = videoDetails . lengthSeconds ;
55
+ songInfo . elapsedSeconds = videoDetails . elapsedSeconds ;
56
+ songInfo . isPaused = videoDetails . isPaused ;
57
+
58
+ const oldUrl = songInfo . imageSrc ;
59
+ songInfo . imageSrc = videoDetails . thumbnail ?. thumbnails ?. pop ( ) ?. url . split ( "?" ) [ 0 ] ;
60
+ if ( oldUrl !== songInfo . imageSrc ) {
61
+ songInfo . image = await getImage ( songInfo . imageSrc ) ;
62
+ }
63
+
64
+ win . webContents . send ( "update-song-info" , JSON . stringify ( songInfo ) ) ;
65
+ }
65
66
} ;
66
67
67
68
// This variable will be filled with the callbacks once they register
@@ -81,19 +82,6 @@ const registerCallback = (callback) => {
81
82
} ;
82
83
83
84
const registerProvider = ( win ) => {
84
- win . on ( "page-title-updated" , async ( ) => {
85
- // Get and set the new data
86
- songInfo . isPaused = await getPausedStatus ( win ) ;
87
-
88
- const elapsedSeconds = await getProgress ( win ) ;
89
- songInfo . elapsedSeconds = elapsedSeconds ;
90
-
91
- // Trigger the callbacks
92
- callbacks . forEach ( ( c ) => {
93
- c ( songInfo ) ;
94
- } ) ;
95
- } ) ;
96
-
97
85
// This will be called when the song-info-front finds a new request with song data
98
86
ipcMain . on ( "song-info-request" , async ( _ , responseText ) => {
99
87
await handleData ( responseText , win ) ;
@@ -114,7 +102,7 @@ const suffixesToRemove = [
114
102
115
103
function cleanupName ( name ) {
116
104
if ( ! name ) return name ;
117
- const lowCaseName = name . toLowerCase ( ) ;
105
+ const lowCaseName = name . toLowerCase ( ) ;
118
106
for ( const suffix of suffixesToRemove ) {
119
107
if ( lowCaseName . endsWith ( suffix ) ) {
120
108
return name . slice ( 0 , - suffix . length ) ;
0 commit comments