mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 22:17:21 +03:00
Update background.js
This commit is contained in:
149
background.js
149
background.js
@@ -19,7 +19,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
|||||||
case "getSponsorTimes":
|
case "getSponsorTimes":
|
||||||
getSponsorTimes(request.videoID, function(sponsorTimes) {
|
getSponsorTimes(request.videoID, function(sponsorTimes) {
|
||||||
callback({
|
callback({
|
||||||
sponsorTimes: sponsorTimes
|
sponsorTimes: sponsorTimes
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -43,22 +43,18 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
|||||||
//add help page on install
|
//add help page on install
|
||||||
chrome.runtime.onInstalled.addListener(function (object) {
|
chrome.runtime.onInstalled.addListener(function (object) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
chrome.storage.sync.get(["userID"], function(result) {
|
const userID = result.userID;
|
||||||
const userID = result.userID;
|
|
||||||
|
|
||||||
// If there is no userID, then it is the first install.
|
// If there is no userID, then it is the first install.
|
||||||
if (!userID){
|
if (!userID){
|
||||||
//open up the install page
|
//open up the install page
|
||||||
chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")});
|
chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")});
|
||||||
|
|
||||||
//generate a userID
|
//generate a userID
|
||||||
const newUserID = generateUserID();
|
const newUserID = generateUserID();
|
||||||
//save this UUID
|
//save this UUID
|
||||||
chrome.storage.sync.set({
|
SB.config.userID = newUserID;
|
||||||
"userID": newUserID
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 1500);
|
}, 1500);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -66,14 +62,12 @@ chrome.runtime.onInstalled.addListener(function (object) {
|
|||||||
function getSponsorTimes(videoID, callback) {
|
function getSponsorTimes(videoID, callback) {
|
||||||
let sponsorTimes = [];
|
let sponsorTimes = [];
|
||||||
let sponsorTimeKey = "sponsorTimes" + videoID;
|
let sponsorTimeKey = "sponsorTimes" + videoID;
|
||||||
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
let sponsorTimesStorage = SB.config.sponsorTimeKey[sponsorTimeKey];
|
||||||
let sponsorTimesStorage = result[sponsorTimeKey];
|
|
||||||
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
|
||||||
sponsorTimes = sponsorTimesStorage;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(sponsorTimes)
|
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
||||||
});
|
sponsorTimes = sponsorTimesStorage;
|
||||||
|
}
|
||||||
|
callback(sponsorTimes);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addSponsorTime(time, videoID, callback) {
|
function addSponsorTime(time, videoID, callback) {
|
||||||
@@ -92,7 +86,8 @@ function addSponsorTime(time, videoID, callback) {
|
|||||||
|
|
||||||
//save this info
|
//save this info
|
||||||
let sponsorTimeKey = "sponsorTimes" + videoID;
|
let sponsorTimeKey = "sponsorTimes" + videoID;
|
||||||
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, callback);
|
SB.config.sponsorTimeKey[sponsorTimeKey] = sponsorTimes;
|
||||||
|
callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,9 +98,7 @@ function submitVote(type, UUID, callback) {
|
|||||||
if (userID == undefined || userID === "undefined") {
|
if (userID == undefined || userID === "undefined") {
|
||||||
//generate one
|
//generate one
|
||||||
userID = generateUserID();
|
userID = generateUserID();
|
||||||
chrome.storage.sync.set({
|
SB.config.userID = userID;
|
||||||
"userID": userID
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//publish this vote
|
//publish this vote
|
||||||
@@ -134,68 +127,58 @@ function submitVote(type, UUID, callback) {
|
|||||||
function submitTimes(videoID, callback) {
|
function submitTimes(videoID, callback) {
|
||||||
//get the video times from storage
|
//get the video times from storage
|
||||||
let sponsorTimeKey = 'sponsorTimes' + videoID;
|
let sponsorTimeKey = 'sponsorTimes' + videoID;
|
||||||
chrome.storage.sync.get([sponsorTimeKey, "userID"], async function(result) {
|
let sponsorTimes = SB.config.sponsorTimeKey[sponsorTimeKey];
|
||||||
let sponsorTimes = result[sponsorTimeKey];
|
let userID = SB.config.userID;
|
||||||
let userID = result.userID;
|
|
||||||
|
|
||||||
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
||||||
let durationResult = await new Promise((resolve, reject) => {
|
let durationResult = await new Promise((resolve, reject) => {
|
||||||
chrome.tabs.query({
|
chrome.tabs.query({
|
||||||
active: true,
|
active: true,
|
||||||
currentWindow: true
|
currentWindow: true
|
||||||
}, function(tabs) {
|
}, function(tabs) {
|
||||||
chrome.tabs.sendMessage(tabs[0].id, {
|
chrome.tabs.sendMessage(tabs[0].id, {
|
||||||
message: "getVideoDuration"
|
message: "getVideoDuration"
|
||||||
}, (response) => resolve(response));
|
}, (response) => resolve(response));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
//check if a sponsor exceeds the duration of the video
|
//check if a sponsor exceeds the duration of the video
|
||||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||||
if (sponsorTimes[i][1] > durationResult.duration) {
|
if (sponsorTimes[i][1] > durationResult.duration) {
|
||||||
sponsorTimes[i][1] = durationResult.duration;
|
sponsorTimes[i][1] = durationResult.duration;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//submit these times
|
|
||||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
|
||||||
//to prevent it from happeneing twice
|
|
||||||
let increasedContributionAmount = false;
|
|
||||||
|
|
||||||
//submit the sponsorTime
|
|
||||||
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
|
|
||||||
+ "&userID=" + userID, function(xmlhttp, error) {
|
|
||||||
if (xmlhttp.readyState == 4 && !error) {
|
|
||||||
callback({
|
|
||||||
statusCode: xmlhttp.status
|
|
||||||
});
|
|
||||||
|
|
||||||
if (xmlhttp.status == 200) {
|
|
||||||
//add these to the storage log
|
|
||||||
chrome.storage.sync.get(["sponsorTimesContributed"], function(result) {
|
|
||||||
let currentContributionAmount = 0;
|
|
||||||
if (result.sponsorTimesContributed != undefined) {
|
|
||||||
//current contribution amount is known
|
|
||||||
currentContributionAmount = result.sponsorTimesContributed;
|
|
||||||
}
|
|
||||||
|
|
||||||
//save the amount contributed
|
|
||||||
if (!increasedContributionAmount) {
|
|
||||||
increasedContributionAmount = true;
|
|
||||||
|
|
||||||
chrome.storage.sync.set({"sponsorTimesContributed": currentContributionAmount + sponsorTimes.length});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (error) {
|
|
||||||
callback({
|
|
||||||
statusCode: -1
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
//submit these times
|
||||||
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||||
|
//to prevent it from happeneing twice
|
||||||
|
let increasedContributionAmount = false;
|
||||||
|
|
||||||
|
//submit the sponsorTime
|
||||||
|
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
|
||||||
|
+ "&userID=" + userID, function(xmlhttp, error) {
|
||||||
|
if (xmlhttp.readyState == 4 && !error) {
|
||||||
|
callback({
|
||||||
|
statusCode: xmlhttp.status
|
||||||
|
});
|
||||||
|
|
||||||
|
if (xmlhttp.status == 200) {
|
||||||
|
//add these to the storage log
|
||||||
|
currentContributionAmount = SB.config.sponsorTimesContributed;
|
||||||
|
//save the amount contributed
|
||||||
|
if (!increasedContributionAmount) {
|
||||||
|
increasedContributionAmount = true;
|
||||||
|
SB.config.sponsorTimesContributed = currentContributionAmount + sponsorTimes.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (error) {
|
||||||
|
callback({
|
||||||
|
statusCode: -1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendRequestToServer(type, address, callback) {
|
function sendRequestToServer(type, address, callback) {
|
||||||
|
|||||||
Reference in New Issue
Block a user