diff --git a/background.js b/background.js index e167dcd..e037256 100644 --- a/background.js +++ b/background.js @@ -13,52 +13,50 @@ chrome.runtime.onInstalled.addListener(function() { }); }); -chrome.tabs.onUpdated.addListener( - function(tabId, changeInfo, tab) { - chrome.storage.sync.get('switchValue', function(data) { - value = data.switchValue; - if(value){ - const code = `(function getUrls(){ - const songName = document.querySelector('yt-formatted-string[class="title style-scope ytmusic-player-bar"]') - ? document.querySelector('yt-formatted-string[class="title style-scope ytmusic-player-bar"]').title - : undefined; - const songInfo = document.querySelector('yt-formatted-string[class="byline style-scope ytmusic-player-bar complex-string"]') - ? document.querySelector('yt-formatted-string[class="byline style-scope ytmusic-player-bar complex-string"]').title - : undefined; - return { songName, songInfo }; - })()`; +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { + chrome.storage.sync.get('switchValue', function(data) { + value = data.switchValue; + if(value){ + const code = `(function getUrls(){ + const songName = document.querySelector('yt-formatted-string[class="title style-scope ytmusic-player-bar"]') + ? document.querySelector('yt-formatted-string[class="title style-scope ytmusic-player-bar"]').title + : undefined; + const songInfo = document.querySelector('yt-formatted-string[class="byline style-scope ytmusic-player-bar complex-string"]') + ? document.querySelector('yt-formatted-string[class="byline style-scope ytmusic-player-bar complex-string"]').title + : undefined; + return { songName, songInfo }; + })()`; - const ytmusicURLs = ['www.music.youtube.com','music.youtube.com']; - if(tab.status == 'complete' && ytmusicURLs.some(url => tab.url.includes(url))){ - setTimeout(() => { - chrome.tabs.executeScript(tabId, { code }, function(result) { - const { songName, songInfo } = result[0]; - notHandle = true; - chrome.storage.sync.get('oldSongInfo', function(songData) { - if(songData.oldSongInfo.localeCompare(`${songName}\n${songInfo}`)===0){ - notHandle = false; + const ytmusicURLs = ['www.music.youtube.com','music.youtube.com']; + if(tab.status == 'complete' && ytmusicURLs.some(url => tab.url.includes(url))){ + setTimeout(() => { + chrome.tabs.executeScript(tabId, { code }, function(result) { + const { songName, songInfo } = result[0]; + notHandle = true; + chrome.storage.sync.get('oldSongInfo', function(songData) { + if(songData.oldSongInfo.localeCompare(`${songName}\n${songInfo}`)===0){ + notHandle = false; + } + else{ + notHandle = true; + } + if(songName!=undefined && songInfo!=undefined && notHandle){ + chrome.storage.sync.set({'oldSongInfo': `${songName}\n${songInfo}`}); + function getNotificationId() { + var id = Math.floor(Math.random() * 9007199254740992) + 1; + return id.toString(); } - else{ - notHandle = true; - } - if(songName!=undefined && songInfo!=undefined && notHandle){ - chrome.storage.sync.set({'oldSongInfo': `${songName}\n${songInfo}`}); - function getNotificationId() { - var id = Math.floor(Math.random() * 9007199254740992) + 1; - return id.toString(); - } - chrome.notifications.create(getNotificationId(), { - title: 'Now Playing', - iconUrl: 'icon128.png', - type: 'basic', - message: `${songName}\n${songInfo}` - }, function() {}); - } - }); + chrome.notifications.create(getNotificationId(), { + title: 'Now Playing', + iconUrl: 'icon128.png', + type: 'basic', + message: `${songName}\n${songInfo}` + }, function() {}); + } }); - }, 2000); - } + }); + }, 2000); } - }); - } -); \ No newline at end of file + } + }); +}); \ No newline at end of file