Converted more code to TypeScript.

This commit is contained in:
Ajay Ramachandran
2020-01-28 23:52:15 -05:00
parent 03836b69f2
commit d77f8abf42
6 changed files with 37 additions and 1150 deletions

View File

@@ -1,12 +1,17 @@
isBackgroundScript = true; import Utils from "./utils";
import SB from "./SB";
import * as Types from "./types";
Utils.isBackgroundScript = true;
// Used only on Firefox, which does not support non persistent background pages. // Used only on Firefox, which does not support non persistent background pages.
var contentScriptRegistrations = {}; var contentScriptRegistrations = {};
// Register content script if needed // Register content script if needed
if (isFirefox()) { if (Utils.isFirefox()) {
wait(() => SB.config !== undefined).then(function() { Utils.wait(() => SB.config !== undefined).then(function() {
if (SB.config.supportInvidious) setupExtraSiteContentScripts(); if (SB.config.supportInvidious) Utils.setupExtraSiteContentScripts();
}); });
} }
@@ -77,7 +82,7 @@ chrome.runtime.onInstalled.addListener(function (object) {
chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")}); chrome.tabs.create({url: chrome.extension.getURL("/help/index_en.html")});
//generate a userID //generate a userID
const newUserID = generateUserID(); const newUserID = Utils.generateUserID();
//save this UUID //save this UUID
SB.config.userID = newUserID; SB.config.userID = newUserID;
@@ -143,7 +148,7 @@ function submitVote(type, UUID, callback) {
if (userID == undefined || userID === "undefined") { if (userID == undefined || userID === "undefined") {
//generate one //generate one
userID = generateUserID(); userID = Utils.generateUserID();
SB.config.userID = userID; SB.config.userID = userID;
} }
@@ -176,7 +181,7 @@ async function submitTimes(videoID, callback) {
let userID = SB.config.userID; let userID = SB.config.userID;
if (sponsorTimes != undefined && sponsorTimes.length > 0) { if (sponsorTimes != undefined && sponsorTimes.length > 0) {
let durationResult = await new Promise((resolve, reject) => { let durationResult = <Types.videoDurationResponse> await new Promise((resolve, reject) => {
chrome.tabs.query({ chrome.tabs.query({
active: true, active: true,
currentWindow: true currentWindow: true
@@ -208,19 +213,17 @@ async function submitTimes(videoID, callback) {
}); });
if (xmlhttp.status == 200) { if (xmlhttp.status == 200) {
//add these to the storage log
currentContributionAmount = SB.config.sponsorTimesContributed;
//save the amount contributed //save the amount contributed
if (!increasedContributionAmount) { if (!increasedContributionAmount) {
increasedContributionAmount = true; increasedContributionAmount = true;
SB.config.sponsorTimesContributed = currentContributionAmount + sponsorTimes.length; SB.config.sponsorTimesContributed = SB.config.sponsorTimesContribute + sponsorTimes.length;
}
} }
} else if (error) { } else if (error) {
callback({ callback({
statusCode: -1 statusCode: -1
}); });
} }
}
}); });
} }
} }

View File

@@ -75,7 +75,7 @@ class PreviewBar {
bar.style.left = (timestamps[i][0] / duration * 100) + "%"; bar.style.left = (timestamps[i][0] / duration * 100) + "%";
bar.style.position = "absolute" bar.style.position = "absolute"
this.container.insertAdjacentElement("beforeEnd", bar); this.container.insertAdjacentElement("beforeend", bar);
} }
} }

File diff suppressed because it is too large Load Diff

7
src/types.ts Normal file
View File

@@ -0,0 +1,7 @@
interface videoDurationResponse {
duration: number;
}
export {
videoDurationResponse
};

View File

@@ -134,7 +134,7 @@ class Utils {
matches: this.getInvidiousInstancesRegex() matches: this.getInvidiousInstancesRegex()
}; };
if (isBackgroundScript) { if (this.isBackgroundScript) {
registerFirefoxContentScript(registration); registerFirefoxContentScript(registration);
} else { } else {
chrome.runtime.sendMessage(registration); chrome.runtime.sendMessage(registration);
@@ -172,7 +172,7 @@ class Utils {
if (this.isFirefox()) { if (this.isFirefox()) {
let id = "invidious"; let id = "invidious";
if (isBackgroundScript) { if (this.isBackgroundScript) {
if (contentScriptRegistrations[id]) { if (contentScriptRegistrations[id]) {
contentScriptRegistrations[id].unregister(); contentScriptRegistrations[id].unregister();
delete contentScriptRegistrations[id]; delete contentScriptRegistrations[id];