1- const { app, BrowserWindow, Notification , ipcMain, TouchBar, nativeImage} = require ( 'electron' ) ;
1+ const { app, BrowserWindow, session , ipcMain, TouchBar, nativeImage} = require ( 'electron' ) ;
22const { TouchBarButton} = TouchBar
33const urlLib = require ( 'url' ) ;
44const https = require ( 'https' ) ;
@@ -13,8 +13,8 @@ const timeFormat = require('hh-mm-ss');
1313const UpdateController = require ( './update-controller' ) ;
1414
1515const playerUrl = 'https://www.xiami.com/play' ;
16- const playlistUrl = 'https://www.xiami.com/song/playlist' ;
17- const getLyricUrl = 'https://img.xiami.net/lyric/' ;
16+ const playlistUrlPrefix = 'https://www.xiami.com/song/playlist* ' ;
17+ const getLyricUrlPrefix = 'https://img.xiami.net/lyric/* ' ;
1818
1919const language = fs . existsSync ( `${ app . getPath ( 'userData' ) } /Settings` ) ? settings . get ( 'language' , 'en' ) : 'en' ;
2020const Locale = language === 'en' ? require ( '../locale/locale_en' ) : require ( '../locale/locale_sc' ) ;
@@ -127,7 +127,7 @@ class XiamiPlayer {
127127 } ) ;
128128
129129 // intercept the ajax call response
130- this . window . webContents . on ( 'did-get-response-details' , ( ( event , status , newURL , originalURL ) => this . handleResponse ( originalURL ) ) ) ;
130+ session . defaultSession . webRequest . onCompleted ( { urls : [ playlistUrlPrefix , getLyricUrlPrefix ] } , ( details ) => this . handleResponse ( details ) )
131131
132132 ipcMain . on ( 'playtime' , ( event , value ) => {
133133 const timeline = this . lyrics . select ( timeFormat . toS ( value ) ) ;
@@ -230,19 +230,20 @@ class XiamiPlayer {
230230
231231 /**
232232 * Handle the received response after the web content make a request.
233- * @param {* } requestUrl the request URL for the event
233+ * @param {* } details the response details
234234 */
235- handleResponse ( requestUrl ) {
236- requestUrl . startsWith ( playlistUrl ) && this . updatePlaylist ( requestUrl ) ;
235+ handleResponse ( details ) {
236+ const url = details . url
237+ RegExp ( playlistUrlPrefix ) . test ( url ) && this . updatePlaylist ( url ) ;
237238
238- if ( requestUrl . startsWith ( getLyricUrl ) ) {
239+ if ( RegExp ( getLyricUrlPrefix ) . test ( url ) ) {
239240 // Load Lyrics.
240- this . loadLyrics ( requestUrl ) ;
241+ this . loadLyrics ( url ) ;
241242
242243 // Load track change notification.
243244 const showNotification = settings . get ( 'showNotification' , 'check' ) ;
244245 if ( 'check' === showNotification ) {
245- const lyricPath = urlLib . parse ( requestUrl ) . pathname ;
246+ const lyricPath = urlLib . parse ( url ) . pathname ;
246247 const songId = lyricPath . match ( / \/ ( \d * ) _ / ) [ 1 ] ;
247248 this . notifyTrackChange ( songId ) ;
248249 }
@@ -251,7 +252,7 @@ class XiamiPlayer {
251252
252253 /**
253254 * Update the playlist if the request URL is for playlist update.
254- * @param {* } requestUrl the request URL for the event
255+ * @param {string } requestUrl the request URL for the event
255256 */
256257 updatePlaylist ( requestUrl ) {
257258 let urlWithPath = urlLib . parse ( requestUrl , false ) ;
@@ -289,9 +290,27 @@ class XiamiPlayer {
289290 } ) ;
290291 }
291292
293+ /**
294+ * Load the lyrics into the application
295+ * @param {string } url the lyrics url
296+ */
297+ loadLyrics ( url ) {
298+ https . get ( url , ( response ) => {
299+ let lyricContent = '' ;
300+
301+ response . on ( 'data' , ( chunk ) => {
302+ lyricContent += chunk ;
303+ } ) ;
304+
305+ response . on ( 'end' , ( ) => {
306+ this . lyrics . load ( lyricContent )
307+ } ) ;
308+ } )
309+ }
310+
292311 /**
293312 * Handle the track changed.
294- * @param {* } songId the changed song ID
313+ * @param {string } songId the changed song ID
295314 */
296315 notifyTrackChange ( songId ) {
297316 // console.log(songId)
@@ -315,10 +334,6 @@ ${Locale.NOTIFICATION_ALBUM}: ${trackInfo.album_name}`;
315334 }
316335 } ) ;
317336 }
318-
319- loadLyrics ( url ) {
320- download ( url ) . then ( buffer => this . lyrics . load ( buffer ) ) ;
321- }
322337}
323338
324339module . exports = XiamiPlayer ;
0 commit comments