2019-07-26 00:25:50 +02:00
|
|
|
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
2019-08-08 21:32:18 +02:00
|
|
|
chrome.tabs.sendMessage(tabId, {
|
|
|
|
message: 'update',
|
|
|
|
});
|
2019-07-26 00:25:50 +02:00
|
|
|
});
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2019-07-09 21:55:33 +02:00
|
|
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
2019-08-12 18:41:26 +02:00
|
|
|
switch(request.message) {
|
|
|
|
case "submitTimes":
|
|
|
|
submitTimes(request.videoID, callback);
|
|
|
|
return true; //this allows the callback to be called later by the submitTimes function
|
|
|
|
case "addSponsorTime":
|
|
|
|
addSponsorTime(request.time, request.videoID, callback);
|
|
|
|
return true; //this allows the callback to be called later
|
|
|
|
case "getSponsorTimes":
|
|
|
|
getSponsorTimes(request.videoID, function(sponsorTimes) {
|
|
|
|
callback({sponsorTimes: sponsorTimes})
|
|
|
|
});
|
|
|
|
return true; //this allows the callback to be called later
|
|
|
|
case "submitVote":
|
|
|
|
submitVote(request.type, request.UUID, callback);
|
|
|
|
return true;
|
|
|
|
case "alertPrevious":
|
|
|
|
chrome.notifications.create("stillThere" + Math.random(), {
|
|
|
|
type: "basic",
|
|
|
|
title: "Do you want to submit the sponsor times for video id " + request.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"
|
|
|
|
});
|
|
|
|
}
|
2019-07-09 21:55:33 +02:00
|
|
|
});
|
|
|
|
|
2019-07-31 06:12:02 +02:00
|
|
|
//add help page on install
|
|
|
|
chrome.runtime.onInstalled.addListener(function (object) {
|
2019-08-08 20:29:09 +02:00
|
|
|
// TODO (shownInstallPage): remove shownInstallPage logic after sufficient amount of time,
|
|
|
|
// so that people have time to upgrade and move to shownInstallPage-free code.
|
|
|
|
chrome.storage.sync.get(["userID", "shownInstallPage"], function(result) {
|
2019-08-08 07:24:30 +02:00
|
|
|
const userID = result.userID;
|
2019-08-08 20:29:09 +02:00
|
|
|
// TODO (shownInstallPage): delete row below
|
|
|
|
const shownInstallPage = result.shownInstallPage;
|
|
|
|
|
2019-08-08 07:24:30 +02:00
|
|
|
// If there is no userID, then it is the first install.
|
|
|
|
if (!userID){
|
2019-08-08 20:29:09 +02:00
|
|
|
// Show install page, if there is no user id
|
|
|
|
// and there is no shownInstallPage.
|
|
|
|
// TODO (shownInstallPage): remove this if statement, but leave contents
|
|
|
|
if (!shownInstallPage){
|
|
|
|
//open up the install page
|
|
|
|
chrome.tabs.create({url: chrome.extension.getURL("/help/index.html")});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO (shownInstallPage): delete if statement and contents
|
|
|
|
// If shownInstallPage is set, remove it.
|
|
|
|
if (!!shownInstallPage){
|
|
|
|
chrome.storage.sync.remove("shownInstallPage");
|
|
|
|
}
|
2019-07-31 06:12:02 +02:00
|
|
|
|
2019-08-08 07:24:30 +02:00
|
|
|
//generate a userID
|
|
|
|
const newUserID = generateUUID();
|
|
|
|
//save this UUID
|
|
|
|
chrome.storage.sync.set({
|
|
|
|
"userID": newUserID,
|
|
|
|
//the last video id loaded, to make sure it is a video id change
|
|
|
|
"sponsorVideoID": null,
|
|
|
|
"previousVideoID": null
|
|
|
|
});
|
2019-07-31 06:12:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-07-13 00:28:41 +02:00
|
|
|
|
|
|
|
//gets the sponsor times from memory
|
|
|
|
function getSponsorTimes(videoID, callback) {
|
2019-07-12 22:44:50 +02:00
|
|
|
let sponsorTimes = [];
|
2019-07-13 00:28:41 +02:00
|
|
|
let sponsorTimeKey = "sponsorTimes" + videoID;
|
2019-07-22 22:42:57 +02:00
|
|
|
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
2019-07-12 22:44:50 +02:00
|
|
|
let sponsorTimesStorage = result[sponsorTimeKey];
|
|
|
|
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
|
|
|
sponsorTimes = sponsorTimesStorage;
|
|
|
|
}
|
|
|
|
|
2019-07-13 00:28:41 +02:00
|
|
|
callback(sponsorTimes)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-02 06:54:50 +02:00
|
|
|
function addSponsorTime(time, videoID, callback) {
|
2019-07-26 22:20:07 +02:00
|
|
|
getSponsorTimes(videoID, function(sponsorTimes) {
|
2019-07-13 00:28:41 +02:00
|
|
|
//add to sponsorTimes
|
2019-07-12 22:44:50 +02:00
|
|
|
if (sponsorTimes.length > 0 && sponsorTimes[sponsorTimes.length - 1].length < 2) {
|
|
|
|
//it is an end time
|
2019-07-27 03:44:39 +02:00
|
|
|
sponsorTimes[sponsorTimes.length - 1][1] = time;
|
2019-07-12 22:44:50 +02:00
|
|
|
} else {
|
|
|
|
//it is a start time
|
|
|
|
let sponsorTimesIndex = sponsorTimes.length;
|
|
|
|
sponsorTimes[sponsorTimesIndex] = [];
|
|
|
|
|
2019-07-27 03:44:39 +02:00
|
|
|
sponsorTimes[sponsorTimesIndex][0] = time;
|
2019-07-12 22:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//save this info
|
2019-07-26 22:20:07 +02:00
|
|
|
let sponsorTimeKey = "sponsorTimes" + videoID;
|
2019-08-02 06:54:50 +02:00
|
|
|
chrome.storage.sync.set({[sponsorTimeKey]: sponsorTimes}, callback);
|
2019-07-12 22:44:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-20 04:24:59 +02:00
|
|
|
function submitVote(type, UUID, callback) {
|
2019-08-11 23:09:04 +02:00
|
|
|
chrome.storage.sync.get(["userID"], function(result) {
|
|
|
|
let userID = result.userID;
|
2019-08-08 07:24:30 +02:00
|
|
|
|
2019-07-16 01:13:09 +02:00
|
|
|
//publish this vote
|
2019-08-11 23:09:04 +02:00
|
|
|
sendRequestToServer("GET", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
|
2019-07-20 04:24:59 +02:00
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
|
|
|
callback({
|
|
|
|
successType: 1
|
|
|
|
});
|
|
|
|
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 405) {
|
|
|
|
//duplicate vote
|
|
|
|
callback({
|
2019-07-30 03:06:15 +02:00
|
|
|
successType: 0,
|
|
|
|
statusCode: xmlhttp.status
|
2019-07-20 04:24:59 +02:00
|
|
|
});
|
2019-07-20 19:26:55 +02:00
|
|
|
} else if (error) {
|
|
|
|
//error while connect
|
|
|
|
callback({
|
2019-07-30 03:06:15 +02:00
|
|
|
successType: -1,
|
|
|
|
statusCode: xmlhttp.status
|
2019-07-20 19:26:55 +02:00
|
|
|
});
|
2019-07-20 04:24:59 +02:00
|
|
|
}
|
|
|
|
})
|
2019-07-16 01:13:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-20 22:03:42 +02:00
|
|
|
function submitTimes(videoID, callback) {
|
2019-07-09 21:55:33 +02:00
|
|
|
//get the video times from storage
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimeKey = 'sponsorTimes' + videoID;
|
2019-07-22 22:42:57 +02:00
|
|
|
chrome.storage.sync.get([sponsorTimeKey], function(result) {
|
2019-07-10 01:46:55 +02:00
|
|
|
let sponsorTimes = result[sponsorTimeKey];
|
2019-08-11 23:09:04 +02:00
|
|
|
let userID = result.userID;
|
2019-07-09 21:55:33 +02:00
|
|
|
|
2019-07-10 04:23:35 +02:00
|
|
|
if (sponsorTimes != undefined && sponsorTimes.length > 0) {
|
2019-07-09 21:55:33 +02:00
|
|
|
//submit these times
|
2019-07-10 01:46:55 +02:00
|
|
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
2019-07-11 01:25:29 +02:00
|
|
|
//submit the sponsorTime
|
2019-08-11 23:09:04 +02:00
|
|
|
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
|
2019-08-08 07:24:30 +02:00
|
|
|
+ "&userID=" + userID, function(xmlhttp, error) {
|
2019-07-20 22:03:42 +02:00
|
|
|
if (xmlhttp.readyState == 4 && !error) {
|
|
|
|
callback({
|
|
|
|
statusCode: xmlhttp.status
|
|
|
|
});
|
2019-08-01 05:47:04 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
chrome.storage.sync.set({"sponsorTimesContributed": currentContributionAmount + sponsorTimes.length});
|
|
|
|
});
|
|
|
|
}
|
2019-07-20 22:03:42 +02:00
|
|
|
} else if (error) {
|
|
|
|
callback({
|
|
|
|
statusCode: -1
|
|
|
|
});
|
|
|
|
}
|
2019-07-11 01:25:29 +02:00
|
|
|
});
|
2019-07-09 21:55:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-20 21:33:52 +02:00
|
|
|
function sendRequestToServer(type, address, callback) {
|
2019-07-20 04:24:59 +02:00
|
|
|
let xmlhttp = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xmlhttp.open(type, serverAddress + address, true);
|
|
|
|
|
2019-07-20 21:33:52 +02:00
|
|
|
if (callback != undefined) {
|
|
|
|
xmlhttp.onreadystatechange = function () {
|
|
|
|
callback(xmlhttp, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
xmlhttp.onerror = function(ev) {
|
|
|
|
callback(xmlhttp, true);
|
|
|
|
};
|
|
|
|
}
|
2019-07-20 04:24:59 +02:00
|
|
|
|
|
|
|
//submit this request
|
|
|
|
xmlhttp.send();
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:34:08 +02:00
|
|
|
function generateUUID(length = 36) {
|
2019-08-12 05:07:27 +02:00
|
|
|
let charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
let result = "";
|
|
|
|
let isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
|
2019-08-04 21:01:39 +02:00
|
|
|
if (window.crypto && window.crypto.getRandomValues) {
|
|
|
|
values = new Uint32Array(length);
|
|
|
|
window.crypto.getRandomValues(values);
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
result += charset[values[i] % charset.length];
|
|
|
|
}
|
|
|
|
return result;
|
2019-08-09 12:34:08 +02:00
|
|
|
} else {
|
2019-08-12 05:07:27 +02:00
|
|
|
for (let i = 0; i < length; i++) {
|
2019-08-09 12:34:08 +02:00
|
|
|
result += charset[Math.floor(Math.random() * charset.length)];
|
2019-08-04 21:01:39 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|