mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-12 22:47:18 +03:00
Update background.js
This commit is contained in:
@@ -10,24 +10,17 @@ var sponsorVideoID = null;
|
|||||||
chrome.tabs.onActivated.addListener(
|
chrome.tabs.onActivated.addListener(
|
||||||
function(activeInfo) {
|
function(activeInfo) {
|
||||||
chrome.tabs.get(activeInfo.tabId, function(tab) {
|
chrome.tabs.get(activeInfo.tabId, function(tab) {
|
||||||
let id = getYouTubeVideoID(tab.url);
|
TabUpdate(activeInfo.tabId);
|
||||||
|
|
||||||
//if this even is a YouTube tab
|
|
||||||
if (id) {
|
|
||||||
videoIDChange(id, activeInfo.tabId);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
//when a tab changes URLs
|
//when a tab changes URLs
|
||||||
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
||||||
if (changeInfo != undefined && changeInfo.url != undefined) {
|
if (changeInfo != undefined) {
|
||||||
let id = getYouTubeVideoID(changeInfo.url);
|
//if URL changed
|
||||||
|
if (tabId) {
|
||||||
//if URL changed and is youtube video message contentScript the video id
|
TabUpdate(tabId);
|
||||||
if (changeInfo.url && id) {
|
|
||||||
videoIDChange(id, tabId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -176,42 +169,11 @@ function submitTimes(videoID, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function videoIDChange(currentVideoID, tabId) {
|
function TabUpdate(tabId) {
|
||||||
//send a message to the content script
|
//send a message to the content script
|
||||||
chrome.tabs.sendMessage(tabId, {
|
chrome.tabs.sendMessage(tabId, {
|
||||||
message: 'ytvideoid',
|
message: 'update'
|
||||||
id: currentVideoID
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//not a url change
|
|
||||||
if (sponsorVideoID == currentVideoID){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
sponsorVideoID = currentVideoID;
|
|
||||||
|
|
||||||
//warn them if they had unsubmitted times
|
|
||||||
if (previousVideoID != null) {
|
|
||||||
//get the sponsor times from storage
|
|
||||||
let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
|
|
||||||
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
|
||||||
let sponsorTimes = result[sponsorTimeKey];
|
|
||||||
|
|
||||||
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
|
||||||
//warn them that they have unsubmitted sponsor times
|
|
||||||
chrome.notifications.create("stillThere" + Math.random(), {
|
|
||||||
type: "basic",
|
|
||||||
title: "Do you want to submit the sponsor times for watch?v=" + previousVideoID + "?",
|
|
||||||
message: "You seem to have left some sponsor times unsubmitted. Go back to that page to submit them (they are not deleted).",
|
|
||||||
iconUrl: "./icons/LogoSponsorBlocker256px.png"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//set the previous video id to the currentID
|
|
||||||
previousVideoID = currentVideoID;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
previousVideoID = currentVideoID;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUserID(callback) {
|
function getUserID(callback) {
|
||||||
@@ -263,11 +225,5 @@ function sendRequestToServer(type, address, callback) {
|
|||||||
xmlhttp.send();
|
xmlhttp.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getYouTubeVideoID(url) { // Return video id or false
|
|
||||||
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
|
|
||||||
var match = url.match(regExp);
|
|
||||||
return (match && match[7].length == 11) ? match[7] : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//uuid generator function from https://gist.github.com/jed/982883
|
//uuid generator function from https://gist.github.com/jed/982883
|
||||||
function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)}
|
function generateUUID(a){return a?(a^Math.random()*16>>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,generateUUID)}
|
||||||
|
|||||||
Reference in New Issue
Block a user