diff options
author | Ajay Ramachandran <[email protected]> | 2019-07-25 18:25:50 -0400 |
---|---|---|
committer | Ajay Ramachandran <[email protected]> | 2019-07-25 18:25:50 -0400 |
commit | 725ab783e3ad9ee626d30063c09b46ee2fafc1e9 (patch) | |
tree | 29ff05e9c8bc0e0d25682c4eb8eefff54895ecb1 | |
parent | 30e322217750169bf84650360c3e67bf27061bb6 (diff) | |
download | SponsorBlock-725ab783e3ad9ee626d30063c09b46ee2fafc1e9.tar.gz SponsorBlock-725ab783e3ad9ee626d30063c09b46ee2fafc1e9.zip |
Made it work when multiple tabs are opened in the background.
Now uses the focused tab instead of last tab updated.
-rw-r--r-- | background.js | 42 | ||||
-rw-r--r-- | content.js | 6 |
2 files changed, 27 insertions, 21 deletions
diff --git a/background.js b/background.js index 07743435..04e644af 100644 --- a/background.js +++ b/background.js @@ -3,21 +3,31 @@ var previousVideoID = null //the id of this user, randomly generated once per install var userID = null; -chrome.tabs.onUpdated.addListener( // On tab update - function(tabId, changeInfo, tab) { +//when a new tab is highlighted +chrome.tabs.onActivated.addListener( + function(activeInfo) { + chrome.tabs.get(activeInfo.tabId, function(tab) { + let id = getYouTubeVideoID(tab.url); + + //if this even is a YouTube tab + if (id) { + videoIDChange(id, activeInfo.tabId); + } + }) + } +); + +//when a tab changes URLs +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if (changeInfo != undefined && changeInfo.url != undefined) { let id = getYouTubeVideoID(changeInfo.url); - if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id - videoIDChange(id); - chrome.tabs.sendMessage( tabId, { - message: 'ytvideoid', - id: id - }); + //if URL changed and is youtube video message contentScript the video id + if (changeInfo.url && id) { + videoIDChange(id, tabId); } } - } -); +}); chrome.runtime.onMessage.addListener(function (request, sender, callback) { if (request.message == "submitTimes") { @@ -25,10 +35,6 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { //this allows the callback to be called later by the submitTimes function return true; - } else if (request.message == "ytvideoid") { - if (previousVideoID != request.videoID) { - videoIDChange(request.videoID); - } } else if (request.message == "addSponsorTime") { addSponsorTime(request.time); } else if (request.message == "getSponsorTimes") { @@ -147,7 +153,13 @@ function submitTimes(videoID, callback) { }); } -function videoIDChange(currentVideoID) { +function videoIDChange(currentVideoID, tabId) { + //send a message to the content script + chrome.tabs.sendMessage(tabId, { + message: 'ytvideoid', + id: currentVideoID + }); + //warn them if they had unsubmitted times if (previousVideoID != null) { //get the sponsor times from storage @@ -1,11 +1,5 @@ if(id = getYouTubeVideoID(document.URL)){ // Direct Links videoIDChange(id); - - //tell background.js about this - chrome.runtime.sendMessage({ - message: "ytvideoid", - videoID: id - }); } //was sponsor data found when doing SponsorsLookup |