Update background.js

This commit is contained in:
Official Noob
2020-01-01 14:44:39 +00:00
committed by GitHub
parent 6f682baa45
commit 039c1a178f

View File

@@ -43,7 +43,6 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
//add help page on install
chrome.runtime.onInstalled.addListener(function (object) {
setTimeout(function() {
chrome.storage.sync.get(["userID"], function(result) {
const userID = result.userID;
// If there is no userID, then it is the first install.
@@ -54,11 +53,8 @@ chrome.runtime.onInstalled.addListener(function (object) {
//generate a userID
const newUserID = generateUserID();
//save this UUID
chrome.storage.sync.set({
"userID": newUserID
});
SB.config.userID = newUserID;
}
});
}, 1500);
});
@@ -66,14 +62,12 @@ chrome.runtime.onInstalled.addListener(function (object) {
function getSponsorTimes(videoID, callback) {
let sponsorTimes = [];
let sponsorTimeKey = "sponsorTimes" + videoID;
chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimesStorage = result[sponsorTimeKey];
let sponsorTimesStorage = SB.config.sponsorTimeKey[sponsorTimeKey];
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
sponsorTimes = sponsorTimesStorage;
}
callback(sponsorTimes)
});
callback(sponsorTimes);
}
function addSponsorTime(time, videoID, callback) {
@@ -92,7 +86,8 @@ function addSponsorTime(time, videoID, callback) {
//save this info
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") {
//generate one
userID = generateUserID();
chrome.storage.sync.set({
"userID": userID
});
SB.config.userID = userID;
}
//publish this vote
@@ -134,9 +127,8 @@ function submitVote(type, UUID, callback) {
function submitTimes(videoID, callback) {
//get the video times from storage
let sponsorTimeKey = 'sponsorTimes' + videoID;
chrome.storage.sync.get([sponsorTimeKey, "userID"], async function(result) {
let sponsorTimes = result[sponsorTimeKey];
let userID = result.userID;
let sponsorTimes = SB.config.sponsorTimeKey[sponsorTimeKey];
let userID = SB.config.userID;
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
let durationResult = await new Promise((resolve, reject) => {
@@ -172,20 +164,12 @@ function submitTimes(videoID, callback) {
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;
}
currentContributionAmount = SB.config.sponsorTimesContributed;
//save the amount contributed
if (!increasedContributionAmount) {
increasedContributionAmount = true;
chrome.storage.sync.set({"sponsorTimesContributed": currentContributionAmount + sponsorTimes.length});
SB.config.sponsorTimesContributed = currentContributionAmount + sponsorTimes.length;
}
});
}
} else if (error) {
callback({
@@ -195,7 +179,6 @@ function submitTimes(videoID, callback) {
});
}
}
});
}
function sendRequestToServer(type, address, callback) {