mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-08 12:37:05 +03:00
Converted more code to TypeScript.
This commit is contained in:
@@ -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
|
||||||
@@ -196,31 +201,29 @@ async function submitTimes(videoID, callback) {
|
|||||||
|
|
||||||
//submit these times
|
//submit these times
|
||||||
for (let i = 0; i < sponsorTimes.length; i++) {
|
for (let i = 0; i < sponsorTimes.length; i++) {
|
||||||
//to prevent it from happeneing twice
|
//to prevent it from happeneing twice
|
||||||
let increasedContributionAmount = false;
|
let increasedContributionAmount = false;
|
||||||
|
|
||||||
//submit the sponsorTime
|
//submit the sponsorTime
|
||||||
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
|
sendRequestToServer("GET", "/api/postVideoSponsorTimes?videoID=" + videoID + "&startTime=" + sponsorTimes[i][0] + "&endTime=" + sponsorTimes[i][1]
|
||||||
+ "&userID=" + userID, 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
|
||||||
});
|
});
|
||||||
|
|
||||||
if (xmlhttp.status == 200) {
|
if (xmlhttp.status == 200) {
|
||||||
//add these to the storage log
|
//save the amount contributed
|
||||||
currentContributionAmount = SB.config.sponsorTimesContributed;
|
if (!increasedContributionAmount) {
|
||||||
//save the amount contributed
|
increasedContributionAmount = true;
|
||||||
if (!increasedContributionAmount) {
|
SB.config.sponsorTimesContributed = SB.config.sponsorTimesContribute + sponsorTimes.length;
|
||||||
increasedContributionAmount = true;
|
|
||||||
SB.config.sponsorTimesContributed = currentContributionAmount + sponsorTimes.length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (error) {
|
} else if (error) {
|
||||||
callback({
|
callback({
|
||||||
statusCode: -1
|
statusCode: -1
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ function messageListener(request, sender, sendResponse) {
|
|||||||
break;
|
break;
|
||||||
case "getVideoDuration":
|
case "getVideoDuration":
|
||||||
sendResponse({
|
sendResponse({
|
||||||
duration: v.duration
|
duration: v.duration
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1123
src/popup.js
1123
src/popup.js
File diff suppressed because it is too large
Load Diff
7
src/types.ts
Normal file
7
src/types.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
interface videoDurationResponse {
|
||||||
|
duration: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
videoDurationResponse
|
||||||
|
};
|
||||||
@@ -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];
|
||||||
|
|||||||
Reference in New Issue
Block a user