mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-09 13:07:05 +03:00
Made it work when multiple tabs are opened in the background.
Now uses the focused tab instead of last tab updated.
This commit is contained in:
@@ -3,32 +3,38 @@ var previousVideoID = null
|
|||||||
//the id of this user, randomly generated once per install
|
//the id of this user, randomly generated once per install
|
||||||
var userID = null;
|
var userID = null;
|
||||||
|
|
||||||
chrome.tabs.onUpdated.addListener( // On tab update
|
//when a new tab is highlighted
|
||||||
function(tabId, changeInfo, tab) {
|
chrome.tabs.onActivated.addListener(
|
||||||
if (changeInfo != undefined && changeInfo.url != undefined) {
|
function(activeInfo) {
|
||||||
let id = getYouTubeVideoID(changeInfo.url);
|
chrome.tabs.get(activeInfo.tabId, function(tab) {
|
||||||
if (changeInfo.url && id) { // If URL changed and is youtube video message contentScript the video id
|
let id = getYouTubeVideoID(tab.url);
|
||||||
videoIDChange(id);
|
|
||||||
|
|
||||||
chrome.tabs.sendMessage( tabId, {
|
//if this even is a YouTube tab
|
||||||
message: 'ytvideoid',
|
if (id) {
|
||||||
id: 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 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) {
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
||||||
if (request.message == "submitTimes") {
|
if (request.message == "submitTimes") {
|
||||||
submitTimes(request.videoID, callback);
|
submitTimes(request.videoID, callback);
|
||||||
|
|
||||||
//this allows the callback to be called later by the submitTimes function
|
//this allows the callback to be called later by the submitTimes function
|
||||||
return true;
|
return true;
|
||||||
} else if (request.message == "ytvideoid") {
|
|
||||||
if (previousVideoID != request.videoID) {
|
|
||||||
videoIDChange(request.videoID);
|
|
||||||
}
|
|
||||||
} else if (request.message == "addSponsorTime") {
|
} else if (request.message == "addSponsorTime") {
|
||||||
addSponsorTime(request.time);
|
addSponsorTime(request.time);
|
||||||
} else if (request.message == "getSponsorTimes") {
|
} 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
|
//warn them if they had unsubmitted times
|
||||||
if (previousVideoID != null) {
|
if (previousVideoID != null) {
|
||||||
//get the sponsor times from storage
|
//get the sponsor times from storage
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
|
if(id = getYouTubeVideoID(document.URL)){ // Direct Links
|
||||||
videoIDChange(id);
|
videoIDChange(id);
|
||||||
|
|
||||||
//tell background.js about this
|
|
||||||
chrome.runtime.sendMessage({
|
|
||||||
message: "ytvideoid",
|
|
||||||
videoID: id
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//was sponsor data found when doing SponsorsLookup
|
//was sponsor data found when doing SponsorsLookup
|
||||||
|
|||||||
Reference in New Issue
Block a user