Refactor: make background non-persistent

This commit is contained in:
Anton Bershanskiy
2019-08-08 00:24:30 -05:00
parent 18909ffef6
commit 7e2925a1e3
3 changed files with 63 additions and 78 deletions

View File

@@ -1,11 +1,3 @@
var previousVideoID = null
//the id of this user, randomly generated once per install
var userID = null;
//the last video id loaded, to make sure it is a video id change
var sponsorVideoID = null;
//when a new tab is highlighted //when a new tab is highlighted
chrome.tabs.onActivated.addListener( chrome.tabs.onActivated.addListener(
function(activeInfo) { function(activeInfo) {
@@ -62,14 +54,23 @@ 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) {
chrome.storage.sync.get(["shownInstallPage"], function(result) { chrome.storage.sync.get(["userID"], function(result) {
let shownInstallPage = result.shownInstallPage; const userID = result.userID;
if (shownInstallPage == undefined || !shownInstallPage) { // If there is no userID, then it is the first install.
if (!userID){
//open up the install page //open up the install page
chrome.tabs.create({url: chrome.extension.getURL("/help/index.html")}); chrome.tabs.create({url: chrome.extension.getURL("/help/index.html")});
//save that this happened //generate a userID
chrome.storage.sync.set({shownInstallPage: true}); const newUserID = generateUUID();
//save this UUID
chrome.storage.sync.set({
"userID": newUserID,
"serverAddress": serverAddress,
//the last video id loaded, to make sure it is a video id change
"sponsorVideoID": null,
"previousVideoID": null
});
} }
}); });
}); });
@@ -109,9 +110,12 @@ function addSponsorTime(time, videoID, callback) {
} }
function submitVote(type, UUID, callback) { function submitVote(type, UUID, callback) {
getUserID(function(userID) { chrome.storage.sync.get(["serverAddress", "userID"], function(result) {
const serverAddress = result.serverAddress;
const userID = result.userID;
//publish this vote //publish this vote
sendRequestToServer('GET', "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) { sendRequestToServer("GET", serverAddress + "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({ callback({
successType: 1 successType: 1
@@ -136,16 +140,17 @@ 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], function(result) { chrome.storage.sync.get([sponsorTimeKey, "serverAddress"], function(result) {
let sponsorTimes = result[sponsorTimeKey]; let sponsorTimes = result[sponsorTimeKey];
const serverAddress = result.serverAddress;
const userID = result.userID;
if (sponsorTimes != undefined && sponsorTimes.length > 0) { if (sponsorTimes != undefined && sponsorTimes.length > 0) {
//submit these times //submit these times
for (let i = 0; i < sponsorTimes.length; i++) { for (let i = 0; i < sponsorTimes.length; i++) {
getUserID(function(userIDStorage) {
//submit the sponsorTime //submit the sponsorTime
sendRequestToServer('GET', "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1] sendRequestToServer("GET", serverAddress + "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ "&userID=" + userIDStorage, function(xmlhttp, error) { + "&userID=" + userID, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && !error) { if (xmlhttp.readyState == 4 && !error) {
callback({ callback({
statusCode: xmlhttp.status statusCode: xmlhttp.status
@@ -169,7 +174,6 @@ function submitTimes(videoID, callback) {
statusCode: -1 statusCode: -1
}); });
} }
});
}); });
} }
} }
@@ -183,63 +187,45 @@ function videoIDChange(currentVideoID, tabId) {
id: currentVideoID id: currentVideoID
}); });
//not a url change chrome.storage.sync.get(["sponsorVideoID", "previousVideoID"], function(result) {
if (sponsorVideoID == currentVideoID){ const sponsorVideoID = result.sponsorVideoID;
return; const previousVideoID = result.previousVideoID;
}
sponsorVideoID = currentVideoID;
//warn them if they had unsubmitted times //not a url change
if (previousVideoID != null) { if (sponsorVideoID == currentVideoID){
//get the sponsor times from storage return;
let sponsorTimeKey = 'sponsorTimes' + previousVideoID; }
chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimes = result[sponsorTimeKey];
if (sponsorTimes != undefined && sponsorTimes.length > 0) { chrome.storage.sync.set({
//warn them that they have unsubmitted sponsor times "sponsorVideoID": currentVideoID
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) { //warn them if they had unsubmitted times
if (userID != null) { if (previousVideoID != null) {
callback(userID); //get the sponsor times from storage
return; let sponsorTimeKey = 'sponsorTimes' + previousVideoID;
} chrome.storage.sync.get([sponsorTimeKey], function(result) {
let sponsorTimes = result[sponsorTimeKey];
//if it is not cached yet, grab it from storage if (sponsorTimes != undefined && sponsorTimes.length > 0) {
chrome.storage.sync.get(["userID"], function(result) { //warn them that they have unsubmitted sponsor times
let userIDStorage = result.userID; chrome.notifications.create("stillThere" + Math.random(), {
if (userIDStorage != undefined) { type: "basic",
userID = userIDStorage; title: "Do you want to submit the sponsor times for watch?v=" + previousVideoID + "?",
callback(userID); 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
chrome.storage.sync.set({
"previousVideoID": currentVideoID
});
});
} else { } else {
//double check if a UUID hasn't been created since this was first called chrome.storage.sync.set({
if (userID != null) { "previousVideoID": currentVideoID
callback(userID); });
return;
}
//generate a userID
userID = generateUUID();
//save this UUID
chrome.storage.sync.set({"userID": userID});
callback(userID);
} }
}); });
} }
@@ -247,7 +233,7 @@ function getUserID(callback) {
function sendRequestToServer(type, address, callback) { function sendRequestToServer(type, address, callback) {
let xmlhttp = new XMLHttpRequest(); let xmlhttp = new XMLHttpRequest();
xmlhttp.open(type, serverAddress + address, true); xmlhttp.open(type, address, true);
if (callback != undefined) { if (callback != undefined) {
xmlhttp.onreadystatechange = function () { xmlhttp.onreadystatechange = function () {

View File

@@ -32,9 +32,7 @@
"icons/downvote.png", "icons/downvote.png",
"icons/PlayerInfoIconSponsorBlocker256px.png", "icons/PlayerInfoIconSponsorBlocker256px.png",
"icons/PlayerDeleteIconSponsorBlocker256px.png", "icons/PlayerDeleteIconSponsorBlocker256px.png",
"popup.html", "popup.html"
"help/index.html",
"help/style.css"
], ],
"permissions": [ "permissions": [
"tabs", "tabs",
@@ -50,7 +48,8 @@
"scripts":[ "scripts":[
"config.js", "config.js",
"background.js" "background.js"
] ],
"persistent": false
}, },
"icons": { "icons": {
"16": "icons/IconSponsorBlocker16px.png", "16": "icons/IconSponsorBlocker16px.png",