2020-01-10 04:13:00 +01:00
|
|
|
isBackgroundScript = true;
|
|
|
|
|
2020-01-01 00:13:51 +01:00
|
|
|
// Used only on Firefox, which does not support non persistent background pages.
|
|
|
|
var contentScriptRegistrations = {};
|
|
|
|
|
2020-01-10 04:13:00 +01:00
|
|
|
// Register content script if needed
|
|
|
|
if (isFirefox()) {
|
|
|
|
wait(() => SB.config !== undefined).then(function() {
|
|
|
|
if (SB.config.supportInvidious) setupExtraSiteContentScripts();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:46:42 +02:00
|
|
|
chrome.tabs.onUpdated.addListener(function(tabId) {
|
2019-08-08 21:32:18 +02:00
|
|
|
chrome.tabs.sendMessage(tabId, {
|
2019-08-20 23:47:31 +02:00
|
|
|
message: 'update',
|
2019-10-15 23:46:42 +02:00
|
|
|
}, () => void chrome.runtime.lastError ); // Suppress error on Firefox
|
2019-07-26 00:25:50 +02:00
|
|
|
});
|
2019-07-09 05:43:06 +02:00
|
|
|
|
2020-01-11 02:48:16 +01:00
|
|
|
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
|
2019-08-12 18:41:26 +02:00
|
|
|
switch(request.message) {
|
2020-01-01 00:13:51 +01:00
|
|
|
case "submitTimes":
|
|
|
|
submitTimes(request.videoID, callback);
|
|
|
|
|
|
|
|
//this allows the callback to be called later by the submitTimes function
|
|
|
|
return true;
|
|
|
|
case "addSponsorTime":
|
|
|
|
addSponsorTime(request.time, request.videoID, callback);
|
|
|
|
|
|
|
|
//this allows the callback to be called later
|
2020-01-09 20:28:06 +01:00
|
|
|
return true;
|
|
|
|
|
2020-01-01 00:13:51 +01:00
|
|
|
case "getSponsorTimes":
|
|
|
|
getSponsorTimes(request.videoID, function(sponsorTimes) {
|
|
|
|
callback({
|
|
|
|
sponsorTimes: sponsorTimes
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
//this allows the callback to be called later
|
|
|
|
return true;
|
|
|
|
case "submitVote":
|
|
|
|
submitVote(request.type, request.UUID, callback);
|
|
|
|
|
|
|
|
//this allows the callback to be called later
|
|
|
|
return true;
|
|
|
|
case "alertPrevious":
|
|
|
|
chrome.notifications.create("stillThere" + Math.random(), {
|
|
|
|
type: "basic",
|
|
|
|
title: chrome.i18n.getMessage("wantToSubmit") + " " + request.previousVideoID + "?",
|
|
|
|
message: chrome.i18n.getMessage("leftTimes"),
|
|
|
|
iconUrl: "./icons/LogoSponsorBlocker256px.png"
|
|
|
|
});
|
|
|
|
case "registerContentScript":
|
2020-01-10 04:13:00 +01:00
|
|
|
registerFirefoxContentScript(request);
|
2020-01-01 00:13:51 +01:00
|
|
|
return false;
|
|
|
|
case "unregisterContentScript":
|
|
|
|
contentScriptRegistrations[request.id].unregister();
|
|
|
|
delete contentScriptRegistrations[request.id];
|
|
|
|
|
|
|
|
return false;
|
2019-08-12 18:41:26 +02:00
|
|
|
}
|
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) {
|
2020-01-11 04:16:53 +01:00
|
|
|
// This let's the config sync to run fully before checking.
|
|
|
|
// This is required on Firefox
|
2019-11-23 18:44:38 +01:00
|
|
|
setTimeout(function() {
|
2020-01-01 15:50:05 +01:00
|
|
|
const userID = SB.config.userID;
|
2020-01-01 15:44:39 +01:00
|
|
|
|
|
|
|
// 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
|
2020-01-11 04:15:28 +01:00
|
|
|
SB.config.userID = newUserID;
|
|
|
|
|
|
|
|
//TODO: Remove when invidious support is old
|
|
|
|
// Don't show this to new users
|
|
|
|
SB.config.invidiousUpdateInfoShowCount = 6;
|
2020-01-01 15:44:39 +01:00
|
|
|
}
|
2019-11-23 18:44:38 +01:00
|
|
|
}, 1500);
|
2019-07-31 06:12:02 +02:00
|
|
|
});
|
2019-07-13 00:28:41 +02:00
|
|
|
|
2020-01-10 04:13:00 +01:00
|
|
|
/**
|
|
|
|
* Only works on Firefox.
|
|
|
|
* Firefox requires that it be applied after every extension restart.
|
|
|
|
*
|
|
|
|
* @param {JSON} options
|
|
|
|
*/
|
|
|
|
function registerFirefoxContentScript(options) {
|
|
|
|
let oldRegistration = contentScriptRegistrations[options.id];
|
|
|
|
if (oldRegistration) oldRegistration.unregister();
|
|
|
|
|
|
|
|
browser.contentScripts.register({
|
|
|
|
allFrames: options.allFrames,
|
|
|
|
js: options.js,
|
|
|
|
css: options.css,
|
|
|
|
matches: options.matches
|
|
|
|
}).then(() => void (contentScriptRegistrations[options.id] = registration));
|
|
|
|
}
|
|
|
|
|
2019-07-13 00:28:41 +02:00
|
|
|
//gets the sponsor times from memory
|
|
|
|
function getSponsorTimes(videoID, callback) {
|
2019-08-20 01:21:19 +02:00
|
|
|
let sponsorTimes = [];
|
2020-01-01 20:11:17 +01:00
|
|
|
let sponsorTimesStorage = SB.config.sponsorTimes.get(videoID);
|
|
|
|
|
2020-01-01 15:44:39 +01:00
|
|
|
if (sponsorTimesStorage != undefined && sponsorTimesStorage.length > 0) {
|
|
|
|
sponsorTimes = sponsorTimesStorage;
|
|
|
|
}
|
2020-01-01 20:11:17 +01:00
|
|
|
|
2020-01-01 15:44:39 +01:00
|
|
|
callback(sponsorTimes);
|
2019-07-13 00:28:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 06:54:50 +02:00
|
|
|
function addSponsorTime(time, videoID, callback) {
|
2019-08-20 01:21:19 +02:00
|
|
|
getSponsorTimes(videoID, function(sponsorTimes) {
|
|
|
|
//add to sponsorTimes
|
|
|
|
if (sponsorTimes.length > 0 && sponsorTimes[sponsorTimes.length - 1].length < 2) {
|
|
|
|
//it is an end time
|
|
|
|
sponsorTimes[sponsorTimes.length - 1][1] = time;
|
|
|
|
} else {
|
|
|
|
//it is a start time
|
|
|
|
let sponsorTimesIndex = sponsorTimes.length;
|
|
|
|
sponsorTimes[sponsorTimesIndex] = [];
|
|
|
|
|
|
|
|
sponsorTimes[sponsorTimesIndex][0] = time;
|
|
|
|
}
|
2019-07-12 22:44:50 +02:00
|
|
|
|
2019-08-20 01:21:19 +02:00
|
|
|
//save this info
|
2020-01-01 20:11:17 +01:00
|
|
|
SB.config.sponsorTimes.set(videoID, sponsorTimes);
|
2020-01-01 15:44:39 +01:00
|
|
|
callback();
|
2019-08-20 01:21:19 +02:00
|
|
|
});
|
2019-07-12 22:44:50 +02:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:24:59 +02:00
|
|
|
function submitVote(type, UUID, callback) {
|
2020-01-11 02:49:21 +01:00
|
|
|
let userID = SB.config.userID;
|
|
|
|
|
|
|
|
if (userID == undefined || userID === "undefined") {
|
|
|
|
//generate one
|
|
|
|
userID = generateUserID();
|
|
|
|
SB.config.userID = userID;
|
|
|
|
}
|
2019-08-20 01:21:19 +02:00
|
|
|
|
2020-01-11 02:49:21 +01:00
|
|
|
//publish this vote
|
|
|
|
sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
|
|
|
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
|
|
|
callback({
|
|
|
|
successType: 1
|
|
|
|
});
|
|
|
|
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 405) {
|
|
|
|
//duplicate vote
|
|
|
|
callback({
|
|
|
|
successType: 0,
|
|
|
|
statusCode: xmlhttp.status
|
|
|
|
});
|
|
|
|
} else if (error) {
|
|
|
|
//error while connect
|
|
|
|
callback({
|
|
|
|
successType: -1,
|
|
|
|
statusCode: xmlhttp.status
|
|
|
|
});
|
2019-08-22 22:08:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-11 02:49:21 +01:00
|
|
|
});
|
2019-07-16 01:13:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 15:47:41 +01:00
|
|
|
async function submitTimes(videoID, callback) {
|
2019-08-20 01:21:19 +02:00
|
|
|
//get the video times from storage
|
2020-01-01 20:11:17 +01:00
|
|
|
let sponsorTimes = SB.config.sponsorTimes.get(videoID);
|
2020-01-01 15:44:39 +01:00
|
|
|
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));
|
2019-08-23 05:12:30 +02:00
|
|
|
});
|
2020-01-01 15:44:39 +01:00
|
|
|
});
|
2019-08-23 05:12:30 +02:00
|
|
|
|
2020-01-01 15:44:39 +01:00
|
|
|
//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;
|
2019-08-23 05:12:30 +02:00
|
|
|
}
|
2020-01-01 15:44:39 +01:00
|
|
|
}
|
2019-08-23 05:12:30 +02:00
|
|
|
|
2020-01-01 15:44:39 +01:00
|
|
|
//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;
|
|
|
|
}
|
2019-08-20 01:21:19 +02:00
|
|
|
}
|
2020-01-01 15:44:39 +01:00
|
|
|
} else if (error) {
|
|
|
|
callback({
|
|
|
|
statusCode: -1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-08-20 01:21:19 +02:00
|
|
|
}
|
2020-01-01 15:44:39 +01:00
|
|
|
}
|
2019-07-09 22:36:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-20 21:33:52 +02:00
|
|
|
function sendRequestToServer(type, address, callback) {
|
2019-08-20 01:21:19 +02:00
|
|
|
let xmlhttp = new XMLHttpRequest();
|
2019-07-20 04:24:59 +02:00
|
|
|
|
2019-08-20 01:21:19 +02:00
|
|
|
xmlhttp.open(type, serverAddress + address, true);
|
2019-07-20 04:24:59 +02:00
|
|
|
|
2019-08-20 01:21:19 +02:00
|
|
|
if (callback != undefined) {
|
|
|
|
xmlhttp.onreadystatechange = function () {
|
|
|
|
callback(xmlhttp, false);
|
|
|
|
};
|
2019-07-20 21:33:52 +02:00
|
|
|
|
2019-08-20 01:21:19 +02:00
|
|
|
xmlhttp.onerror = function(ev) {
|
|
|
|
callback(xmlhttp, true);
|
|
|
|
};
|
|
|
|
}
|
2019-07-20 04:24:59 +02:00
|
|
|
|
2019-08-20 01:21:19 +02:00
|
|
|
//submit this request
|
|
|
|
xmlhttp.send();
|
2020-01-01 15:44:39 +01:00
|
|
|
}
|