mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 22:17:21 +03:00
Moved server code to utils
This commit is contained in:
@@ -153,7 +153,7 @@ function submitVote(type, UUID, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//publish this vote
|
//publish this vote
|
||||||
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) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
callback({
|
callback({
|
||||||
successType: 1
|
successType: 1
|
||||||
@@ -205,7 +205,7 @@ async function submitTimes(videoID, callback) {
|
|||||||
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]
|
Utils.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({
|
||||||
@@ -228,22 +228,3 @@ async function submitTimes(videoID, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendRequestToServer(type, address, callback) {
|
|
||||||
let xmlhttp = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xmlhttp.open(type, serverAddress + address, true);
|
|
||||||
|
|
||||||
if (callback != undefined) {
|
|
||||||
xmlhttp.onreadystatechange = function () {
|
|
||||||
callback(xmlhttp, false);
|
|
||||||
};
|
|
||||||
|
|
||||||
xmlhttp.onerror = function(ev) {
|
|
||||||
callback(xmlhttp, true);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
//submit this request
|
|
||||||
xmlhttp.send();
|
|
||||||
}
|
|
||||||
|
|||||||
140
src/popup.ts
140
src/popup.ts
@@ -1,4 +1,3 @@
|
|||||||
import * as CompileConfig from "../config.json";
|
|
||||||
import Utils from "./utils";
|
import Utils from "./utils";
|
||||||
import SB from "./SB";
|
import SB from "./SB";
|
||||||
|
|
||||||
@@ -170,43 +169,43 @@ async function runThePopup() {
|
|||||||
OptionsElements.sponsorTimesContributionsContainer.style.display = "unset";
|
OptionsElements.sponsorTimesContributionsContainer.style.display = "unset";
|
||||||
|
|
||||||
//get the userID
|
//get the userID
|
||||||
let userID = SB.config.userID;
|
let userID = SB.config.userID;
|
||||||
if (userID != undefined) {
|
if (userID != undefined) {
|
||||||
//there are probably some views on these submissions then
|
//there are probably some views on these submissions then
|
||||||
//get the amount of views from the sponsors submitted
|
//get the amount of views from the sponsors submitted
|
||||||
sendRequestToServer("GET", "/api/getViewsForUser?userID=" + userID, function(xmlhttp) {
|
Utils.sendRequestToServer("GET", "/api/getViewsForUser?userID=" + userID, function(xmlhttp) {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
let viewCount = JSON.parse(xmlhttp.responseText).viewCount;
|
let viewCount = JSON.parse(xmlhttp.responseText).viewCount;
|
||||||
if (viewCount != 0) {
|
if (viewCount != 0) {
|
||||||
if (viewCount > 1) {
|
if (viewCount > 1) {
|
||||||
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
|
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
|
||||||
} else {
|
} else {
|
||||||
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
|
OptionsElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
|
||||||
}
|
|
||||||
|
|
||||||
OptionsElements.sponsorTimesViewsDisplay.innerText = viewCount;
|
|
||||||
OptionsElements.sponsorTimesViewsContainer.style.display = "unset";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OptionsElements.sponsorTimesViewsDisplay.innerText = viewCount;
|
||||||
|
OptionsElements.sponsorTimesViewsContainer.style.display = "unset";
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//get this time in minutes
|
//get this time in minutes
|
||||||
sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function(xmlhttp) {
|
Utils.sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function(xmlhttp) {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
let minutesSaved = JSON.parse(xmlhttp.responseText).timeSaved;
|
let minutesSaved = JSON.parse(xmlhttp.responseText).timeSaved;
|
||||||
if (minutesSaved != 0) {
|
if (minutesSaved != 0) {
|
||||||
if (minutesSaved != 1) {
|
if (minutesSaved != 1) {
|
||||||
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
|
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
|
||||||
} else {
|
} else {
|
||||||
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
|
OptionsElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
|
||||||
}
|
|
||||||
|
|
||||||
OptionsElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
|
|
||||||
OptionsElements.sponsorTimesOthersTimeSavedContainer.style.display = "unset";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OptionsElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
|
||||||
|
OptionsElements.sponsorTimesOthersTimeSavedContainer.style.display = "unset";
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the amount of times this user has skipped a sponsor
|
//get the amount of times this user has skipped a sponsor
|
||||||
@@ -859,27 +858,27 @@ async function runThePopup() {
|
|||||||
|
|
||||||
//make the options username setting option visible
|
//make the options username setting option visible
|
||||||
function setUsernameButton() {
|
function setUsernameButton() {
|
||||||
//get username from the server
|
//get username from the server
|
||||||
sendRequestToServer("GET", "/api/getUsername?userID=" + SB.config.userID, function (xmlhttp, error) {
|
Utils.sendRequestToServer("GET", "/api/getUsername?userID=" + SB.config.userID, function (xmlhttp, error) {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
OptionsElements.usernameInput.value = JSON.parse(xmlhttp.responseText).userName;
|
OptionsElements.usernameInput.value = JSON.parse(xmlhttp.responseText).userName;
|
||||||
|
|
||||||
OptionsElements.submitUsername.style.display = "unset";
|
OptionsElements.submitUsername.style.display = "unset";
|
||||||
OptionsElements.usernameInput.style.display = "unset";
|
OptionsElements.usernameInput.style.display = "unset";
|
||||||
|
|
||||||
OptionsElements.setUsernameContainer.style.display = "none";
|
OptionsElements.setUsernameContainer.style.display = "none";
|
||||||
OptionsElements.setUsername.style.display = "unset";
|
OptionsElements.setUsername.style.display = "unset";
|
||||||
OptionsElements
|
OptionsElements
|
||||||
OptionsElements.setUsernameStatusContainer.style.display = "none";
|
OptionsElements.setUsernameStatusContainer.style.display = "none";
|
||||||
} else if (xmlhttp.readyState == 4) {
|
} else if (xmlhttp.readyState == 4) {
|
||||||
OptionsElements.setUsername.style.display = "unset";
|
OptionsElements.setUsername.style.display = "unset";
|
||||||
OptionsElements.submitUsername.style.display = "none";
|
OptionsElements.submitUsername.style.display = "none";
|
||||||
OptionsElements.usernameInput.style.display = "none";
|
OptionsElements.usernameInput.style.display = "none";
|
||||||
|
|
||||||
OptionsElements.setUsernameStatusContainer.style.display = "unset";
|
OptionsElements.setUsernameStatusContainer.style.display = "unset";
|
||||||
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
|
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//submit the new username
|
//submit the new username
|
||||||
@@ -889,17 +888,17 @@ async function runThePopup() {
|
|||||||
OptionsElements.setUsernameStatus.innerText = "Loading...";
|
OptionsElements.setUsernameStatus.innerText = "Loading...";
|
||||||
|
|
||||||
//get the userID
|
//get the userID
|
||||||
sendRequestToServer("POST", "/api/setUsername?userID=" + SB.config.userID + "&username=" + OptionsElements.usernameInput.value, function (xmlhttp, error) {
|
Utils.sendRequestToServer("POST", "/api/setUsername?userID=" + SB.config.userID + "&username=" + OptionsElements.usernameInput.value, function (xmlhttp, error) {
|
||||||
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
|
||||||
//submitted
|
//submitted
|
||||||
OptionsElements.submitUsername.style.display = "none";
|
OptionsElements.submitUsername.style.display = "none";
|
||||||
OptionsElements.usernameInput.style.display = "none";
|
OptionsElements.usernameInput.style.display = "none";
|
||||||
|
|
||||||
OptionsElements.setUsernameStatus.innerText = chrome.i18n.getMessage("success");
|
OptionsElements.setUsernameStatus.innerText = chrome.i18n.getMessage("success");
|
||||||
} else if (xmlhttp.readyState == 4) {
|
} else if (xmlhttp.readyState == 4) {
|
||||||
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
|
OptionsElements.setUsernameStatus.innerText = Utils.getErrorMessage(xmlhttp.status);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
OptionsElements.setUsernameContainer.style.display = "none";
|
OptionsElements.setUsernameContainer.style.display = "none";
|
||||||
@@ -1106,25 +1105,6 @@ async function runThePopup() {
|
|||||||
return secondsFormatted;
|
return secondsFormatted;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendRequestToServer(type, address, callback) {
|
|
||||||
let xmlhttp = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
|
|
||||||
|
|
||||||
if (callback != undefined) {
|
|
||||||
xmlhttp.onreadystatechange = function () {
|
|
||||||
callback(xmlhttp, false);
|
|
||||||
};
|
|
||||||
|
|
||||||
xmlhttp.onerror = function(ev) {
|
|
||||||
callback(xmlhttp, true);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
//submit this request
|
|
||||||
xmlhttp.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts time in hours to 5h 25.1
|
* Converts time in hours to 5h 25.1
|
||||||
* If less than 1 hour, just returns minutes
|
* If less than 1 hour, just returns minutes
|
||||||
|
|||||||
20
src/utils.ts
20
src/utils.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import * as CompileConfig from "../config.json";
|
||||||
import SB from "./SB";
|
import SB from "./SB";
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
@@ -268,6 +269,25 @@ class Utils {
|
|||||||
return errorMessage;
|
return errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static sendRequestToServer(type, address, callback) {
|
||||||
|
let xmlhttp = new XMLHttpRequest();
|
||||||
|
|
||||||
|
xmlhttp.open(type, CompileConfig.serverAddress + address, true);
|
||||||
|
|
||||||
|
if (callback != undefined) {
|
||||||
|
xmlhttp.onreadystatechange = function () {
|
||||||
|
callback(xmlhttp, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
xmlhttp.onerror = function(ev) {
|
||||||
|
callback(xmlhttp, true);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//submit this request
|
||||||
|
xmlhttp.send();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is this Firefox (web-extensions)
|
* Is this Firefox (web-extensions)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user