aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/background.ts
diff options
context:
space:
mode:
authorAjay Ramachandran <[email protected]>2020-02-01 18:47:36 -0500
committerAjay Ramachandran <[email protected]>2020-02-01 18:47:36 -0500
commit17381e7debba87863b91d2bdaf75932882a02298 (patch)
treeee77a443e7faa244ed415f3d6b99229ffa089b7c /src/background.ts
parent4bd410f04e46127f528cf47ada9f499069fde71e (diff)
downloadSponsorBlock-17381e7debba87863b91d2bdaf75932882a02298.tar.gz
SponsorBlock-17381e7debba87863b91d2bdaf75932882a02298.zip
Moved Utils away from a static class.
Moved Firefox content script registration to the background script. Moved onInvidious to the content script.
Diffstat (limited to 'src/background.ts')
-rw-r--r--src/background.ts38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/background.ts b/src/background.ts
index 29f63c51..a5650135 100644
--- a/src/background.ts
+++ b/src/background.ts
@@ -1,17 +1,19 @@
-import Utils from "./utils";
-import SB from "./SB";
-
import * as Types from "./types";
+import SB from "./SB";
-Utils.isBackgroundScript = true;
+import Utils from "./utils";
+var utils = new Utils({
+ registerFirefoxContentScript,
+ unregisterFirefoxContentScript
+});
// Used only on Firefox, which does not support non persistent background pages.
var contentScriptRegistrations = {};
// Register content script if needed
-if (Utils.isFirefox()) {
- Utils.wait(() => SB.config !== undefined).then(function() {
- if (SB.config.supportInvidious) Utils.setupExtraSiteContentScripts();
+if (utils.isFirefox()) {
+ utils.wait(() => SB.config !== undefined).then(function() {
+ if (SB.config.supportInvidious) utils.setupExtraSiteContentScripts();
});
}
@@ -62,9 +64,7 @@ chrome.runtime.onMessage.addListener(function (request, sender, callback) {
registerFirefoxContentScript(request);
return false;
case "unregisterContentScript":
- contentScriptRegistrations[request.id].unregister();
- delete contentScriptRegistrations[request.id];
-
+ unregisterFirefoxContentScript(request.id)
return false;
}
});
@@ -82,7 +82,7 @@ chrome.runtime.onInstalled.addListener(function (object) {
chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")});
//generate a userID
- const newUserID = Utils.generateUserID();
+ const newUserID = utils.generateUserID();
//save this UUID
SB.config.userID = newUserID;
@@ -111,6 +111,16 @@ function registerFirefoxContentScript(options) {
}).then((registration) => void (contentScriptRegistrations[options.id] = registration));
}
+/**
+ * Only works on Firefox.
+ * Firefox requires that this is handled by the background script
+ *
+ */
+function unregisterFirefoxContentScript(id: string) {
+ contentScriptRegistrations[id].unregister();
+ delete contentScriptRegistrations[id];
+}
+
//gets the sponsor times from memory
function getSponsorTimes(videoID, callback) {
let sponsorTimes = [];
@@ -148,12 +158,12 @@ function submitVote(type, UUID, callback) {
if (userID == undefined || userID === "undefined") {
//generate one
- userID = Utils.generateUserID();
+ userID = utils.generateUserID();
SB.config.userID = userID;
}
//publish this vote
- Utils.sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
+ utils.sendRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&userID=" + userID + "&type=" + type, function(xmlhttp, error) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback({
successType: 1
@@ -205,7 +215,7 @@ async function submitTimes(videoID, callback) {
let increasedContributionAmount = false;
//submit the sponsorTime
- Utils.sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
+ utils.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({