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 //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.
@@ -54,11 +53,8 @@ chrome.runtime.onInstalled.addListener(function (object) {
//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) { if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
sponsorTimes = sponsorTimesStorage; sponsorTimes = sponsorTimesStorage;
} }
callback(sponsorTimes);
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,9 +127,8 @@ 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) => {
@@ -172,20 +164,12 @@ function submitTimes(videoID, callback) {
if (xmlhttp.status == 200) { if (xmlhttp.status == 200) {
//add these to the storage log //add these to the storage log
chrome.storage.sync.get(["sponsorTimesContributed"], function(result) { currentContributionAmount = SB.config.sponsorTimesContributed;
let currentContributionAmount = 0;
if (result.sponsorTimesContributed != undefined) {
//current contribution amount is known
currentContributionAmount = result.sponsorTimesContributed;
}
//save the amount contributed //save the amount contributed
if (!increasedContributionAmount) { if (!increasedContributionAmount) {
increasedContributionAmount = true; increasedContributionAmount = true;
SB.config.sponsorTimesContributed = currentContributionAmount + sponsorTimes.length;
chrome.storage.sync.set({"sponsorTimesContributed": currentContributionAmount + sponsorTimes.length});
} }
});
} }
} else if (error) { } else if (error) {
callback({ callback({
@@ -195,7 +179,6 @@ function submitTimes(videoID, callback) {
}); });
} }
} }
});
} }
function sendRequestToServer(type, address, callback) { function sendRequestToServer(type, address, callback) {