From 039c1a178fb314a2d5ac59582893ad0b64f1e0eb Mon Sep 17 00:00:00 2001 From: Official Noob <31563761+OfficialNoob@users.noreply.github.com> Date: Wed, 1 Jan 2020 14:44:39 +0000 Subject: [PATCH] Update background.js --- background.js | 155 ++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 86 deletions(-) diff --git a/background.js b/background.js index f2759f1f..0d1c4696 100644 --- a/background.js +++ b/background.js @@ -19,7 +19,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) { case "getSponsorTimes": getSponsorTimes(request.videoID, function(sponsorTimes) { callback({ - sponsorTimes: sponsorTimes + sponsorTimes: sponsorTimes }) }); @@ -43,22 +43,18 @@ 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; + const userID = result.userID; - // If there is no userID, then it is the first install. - if (!userID){ - //open up the install page - chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")}); + // If there is no userID, then it is the first install. + if (!userID){ + //open up the install page + chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")}); - //generate a userID - const newUserID = generateUserID(); - //save this UUID - chrome.storage.sync.set({ - "userID": newUserID - }); - } - }); + //generate a userID + const newUserID = generateUserID(); + //save this UUID + 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]; - if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) { - sponsorTimes = sponsorTimesStorage; - } - - callback(sponsorTimes) - }); + let sponsorTimesStorage = SB.config.sponsorTimeKey[sponsorTimeKey]; + + if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) { + sponsorTimes = sponsorTimesStorage; + } + 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,68 +127,58 @@ 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; - - if (sponsorTimes != undefined && sponsorTimes.length > 0) { - let durationResult = await new Promise((resolve, reject) => { - chrome.tabs.query({ - active: true, - currentWindow: true - }, function(tabs) { - chrome.tabs.sendMessage(tabs[0].id, { - message: "getVideoDuration" - }, (response) => resolve(response)); - }); + let sponsorTimes = SB.config.sponsorTimeKey[sponsorTimeKey]; + let userID = SB.config.userID; + + if (sponsorTimes != undefined && sponsorTimes.length > 0) { + let durationResult = await new Promise((resolve, reject) => { + chrome.tabs.query({ + active: true, + currentWindow: true + }, function(tabs) { + chrome.tabs.sendMessage(tabs[0].id, { + message: "getVideoDuration" + }, (response) => resolve(response)); }); + }); - //check if a sponsor exceeds the duration of the video - for (let i = 0; i < sponsorTimes.length; i++) { - if (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 - }); - } - }); + //check if a sponsor exceeds the duration of the video + for (let i = 0; i < sponsorTimes.length; i++) { + if (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 + 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) { @@ -215,4 +198,4 @@ function sendRequestToServer(type, address, callback) { //submit this request xmlhttp.send(); -} \ No newline at end of file +}