mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2026-02-01 15:21:12 +03:00
Merge branch 'master' into chapters
This commit is contained in:
@@ -140,9 +140,6 @@ const manualSkipPercentCount = 0.5;
|
||||
//get messages from the background script and the popup
|
||||
chrome.runtime.onMessage.addListener(messageListener);
|
||||
|
||||
//store pressed modifier keys
|
||||
const pressedKeys = new Set();
|
||||
|
||||
function messageListener(request: Message, sender: unknown, sendResponse: (response: MessageResponse) => void): void | boolean {
|
||||
//messages from popup script
|
||||
switch(request.message){
|
||||
@@ -911,7 +908,8 @@ function retryFetch(): void {
|
||||
* Ex. When segments are first loaded
|
||||
*/
|
||||
function startSkipScheduleCheckingForStartSponsors() {
|
||||
if (!switchingVideos && sponsorTimes) {
|
||||
// switchingVideos is ignored in Safari due to event fire order. See #1142
|
||||
if ((!switchingVideos || isSafari) && sponsorTimes) {
|
||||
// See if there are any starting sponsors
|
||||
let startingSegmentTime = getStartTimeFromUrl(document.URL) || -1;
|
||||
let found = false;
|
||||
@@ -2028,37 +2026,34 @@ function addPageListeners(): void {
|
||||
|
||||
function addHotkeyListener(): void {
|
||||
document.addEventListener("keydown", hotkeyListener);
|
||||
document.addEventListener("keyup", (e) => pressedKeys.delete(e.key));
|
||||
document.addEventListener("focus", (e) => pressedKeys.clear());
|
||||
}
|
||||
|
||||
function hotkeyListener(e: KeyboardEvent): void {
|
||||
if (["textarea", "input"].includes(document.activeElement?.tagName?.toLowerCase())
|
||||
|| document.activeElement?.id?.toLowerCase()?.includes("editable")) return;
|
||||
|
||||
if (["Alt", "Control", "Shift", "AltGraph"].includes(e.key)) {
|
||||
pressedKeys.add(e.key);
|
||||
return;
|
||||
}
|
||||
|
||||
const key:Keybind = {key: e.key, code: e.code, alt: pressedKeys.has("Alt"), ctrl: pressedKeys.has("Control"), shift: pressedKeys.has("Shift")};
|
||||
const key: Keybind = {
|
||||
key: e.key,
|
||||
code: e.code,
|
||||
alt: e.altKey,
|
||||
ctrl: e.ctrlKey,
|
||||
shift: e.shiftKey
|
||||
};
|
||||
|
||||
const skipKey = Config.config.skipKeybind;
|
||||
const startSponsorKey = Config.config.startSponsorKeybind;
|
||||
const submitKey = Config.config.submitKeybind;
|
||||
|
||||
if (!pressedKeys.has("AltGraph")) {
|
||||
if (keybindEquals(key, skipKey)) {
|
||||
if (activeSkipKeybindElement)
|
||||
activeSkipKeybindElement.toggleSkip.call(activeSkipKeybindElement);
|
||||
return;
|
||||
} else if (keybindEquals(key, startSponsorKey)) {
|
||||
startOrEndTimingNewSegment();
|
||||
return;
|
||||
} else if (keybindEquals(key, submitKey)) {
|
||||
submitSponsorTimes();
|
||||
return;
|
||||
}
|
||||
if (keybindEquals(key, skipKey)) {
|
||||
if (activeSkipKeybindElement)
|
||||
activeSkipKeybindElement.toggleSkip.call(activeSkipKeybindElement);
|
||||
return;
|
||||
} else if (keybindEquals(key, startSponsorKey)) {
|
||||
startOrEndTimingNewSegment();
|
||||
return;
|
||||
} else if (keybindEquals(key, submitKey)) {
|
||||
submitSponsorTimes();
|
||||
return;
|
||||
}
|
||||
|
||||
//legacy - to preserve keybinds for skipKey, startSponsorKey and submitKey for people who set it before the update. (shouldn't be changed for future keybind options)
|
||||
|
||||
102
src/popup.ts
102
src/popup.ts
@@ -177,56 +177,40 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
PageElements.showNoticeAgain.style.display = "unset";
|
||||
}
|
||||
|
||||
utils.sendRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID, (res) => {
|
||||
utils.sendRequestToServer("GET", "/api/userInfo?value=userName&value=viewCount&value=minutesSaved&userID=" + Config.config.userID, (res) => {
|
||||
if (res.status === 200) {
|
||||
PageElements.usernameValue.innerText = JSON.parse(res.responseText).userName
|
||||
const userInfo = JSON.parse(res.responseText);
|
||||
PageElements.usernameValue.innerText = userInfo.userName;
|
||||
|
||||
const viewCount = userInfo.viewCount;
|
||||
if (viewCount != 0) {
|
||||
if (viewCount > 1) {
|
||||
PageElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
|
||||
} else {
|
||||
PageElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
|
||||
}
|
||||
PageElements.sponsorTimesViewsDisplay.innerText = viewCount.toLocaleString();
|
||||
PageElements.sponsorTimesViewsContainer.style.display = "unset";
|
||||
}
|
||||
|
||||
showDonateWidget(viewCount);
|
||||
|
||||
const minutesSaved = userInfo.minutesSaved;
|
||||
if (minutesSaved != 0) {
|
||||
if (minutesSaved != 1) {
|
||||
PageElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
|
||||
} else {
|
||||
PageElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
|
||||
}
|
||||
PageElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
//get the amount of times this user has contributed and display it to thank them
|
||||
if (Config.config.sponsorTimesContributed != undefined) {
|
||||
PageElements.sponsorTimesContributionsDisplay.innerText = Config.config.sponsorTimesContributed.toLocaleString();
|
||||
PageElements.sponsorTimesContributionsContainer.classList.remove("hidden");
|
||||
|
||||
//get the userID
|
||||
const userID = Config.config.userID;
|
||||
if (userID != undefined) {
|
||||
//there are probably some views on these submissions then
|
||||
//get the amount of views from the sponsors submitted
|
||||
utils.sendRequestToServer("GET", "/api/getViewsForUser?userID=" + userID, function (response) {
|
||||
if (response.status == 200) {
|
||||
const viewCount = JSON.parse(response.responseText).viewCount;
|
||||
if (viewCount != 0) {
|
||||
if (viewCount > 1) {
|
||||
PageElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segments");
|
||||
} else {
|
||||
PageElements.sponsorTimesViewsDisplayEndWord.innerText = chrome.i18n.getMessage("Segment");
|
||||
}
|
||||
|
||||
PageElements.sponsorTimesViewsDisplay.innerText = viewCount.toLocaleString();
|
||||
PageElements.sponsorTimesViewsContainer.style.display = "unset";
|
||||
}
|
||||
|
||||
showDonateWidget(viewCount);
|
||||
}
|
||||
});
|
||||
|
||||
//get this time in minutes
|
||||
utils.sendRequestToServer("GET", "/api/getSavedTimeForUser?userID=" + userID, function (response) {
|
||||
if (response.status == 200) {
|
||||
const minutesSaved = JSON.parse(response.responseText).timeSaved;
|
||||
if (minutesSaved != 0) {
|
||||
if (minutesSaved != 1) {
|
||||
PageElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minsLower");
|
||||
} else {
|
||||
PageElements.sponsorTimesOthersTimeSavedEndWord.innerText = chrome.i18n.getMessage("minLower");
|
||||
}
|
||||
|
||||
PageElements.sponsorTimesOthersTimeSavedDisplay.innerText = getFormattedHours(minutesSaved);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//get the amount of times this user has skipped a sponsor
|
||||
@@ -280,7 +264,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
PageElements.sbConsiderDonateLink.addEventListener("click", () => {
|
||||
Config.config.donateClicked = Config.config.donateClicked + 1;
|
||||
});
|
||||
|
||||
|
||||
PageElements.sbCloseDonate.addEventListener("click", () => {
|
||||
PageElements.sponsorTimesDonateContainer.style.display = "none";
|
||||
Config.config.showPopupDonationCount = 100;
|
||||
@@ -455,8 +439,8 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
const category = segmentTimes[i].category;
|
||||
const actionType = segmentTimes[i].actionType;
|
||||
|
||||
const sponsorTimeButton = document.createElement("button");
|
||||
sponsorTimeButton.className = "segmentTimeButton popupElement";
|
||||
const segmentSummary = document.createElement("summary");
|
||||
segmentSummary.className = "segmentSummary";
|
||||
|
||||
const categoryColorCircle = document.createElement("span");
|
||||
categoryColorCircle.id = "sponsorTimesCategoryColorCircle" + UUID;
|
||||
@@ -483,24 +467,23 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
} else {
|
||||
segmentTimeFromToNode.innerText = GenericUtils.getFormattedTime(segmentTimes[i].segment[0], true) +
|
||||
(actionType !== ActionType.Poi
|
||||
? " " + chrome.i18n.getMessage("to") + " " + GenericUtils.getFormattedTime(segmentTimes[i].segment[1], true)
|
||||
? " " + chrome.i18n.getMessage("to") + " " + GenericUtils.getFormattedTime(segmentTimes[i].segment[1], true)
|
||||
: "");
|
||||
}
|
||||
|
||||
|
||||
segmentTimeFromToNode.style.margin = "5px";
|
||||
|
||||
if (actionType !== ActionType.Chapter) sponsorTimeButton.appendChild(categoryColorCircle);
|
||||
sponsorTimeButton.appendChild(textNode);
|
||||
sponsorTimeButton.appendChild(segmentTimeFromToNode);
|
||||
if (actionType !== ActionType.Chapter) segmentSummary.appendChild(categoryColorCircle);
|
||||
segmentSummary.appendChild(textNode);
|
||||
segmentSummary.appendChild(segmentTimeFromToNode);
|
||||
|
||||
const votingButtons = document.createElement("div");
|
||||
const votingButtons = document.createElement("details");
|
||||
votingButtons.classList.add("votingButtons");
|
||||
|
||||
//thumbs up and down buttons
|
||||
const voteButtonsContainer = document.createElement("div");
|
||||
voteButtonsContainer.id = "sponsorTimesVoteButtonsContainer" + UUID;
|
||||
voteButtonsContainer.setAttribute("align", "center");
|
||||
voteButtonsContainer.classList.add('voteButtonsContainer--hide');
|
||||
|
||||
const upvoteButton = document.createElement("img");
|
||||
upvoteButton.id = "sponsorTimesUpvoteButtonsContainer" + UUID;
|
||||
@@ -570,18 +553,13 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
voteButtonsContainer.appendChild(upvoteButton);
|
||||
voteButtonsContainer.appendChild(downvoteButton);
|
||||
voteButtonsContainer.appendChild(uuidButton);
|
||||
if (segmentTimes[i].actionType === ActionType.Skip
|
||||
if (segmentTimes[i].actionType === ActionType.Skip
|
||||
&& [SponsorHideType.Visible, SponsorHideType.Hidden].includes(segmentTimes[i].hidden)) {
|
||||
voteButtonsContainer.appendChild(hideButton);
|
||||
}
|
||||
voteButtonsContainer.appendChild(skipButton);
|
||||
|
||||
|
||||
//add click listener to open up vote panel
|
||||
sponsorTimeButton.addEventListener("click", function () {
|
||||
voteButtonsContainer.classList.toggle("voteButtonsContainer--hide");
|
||||
});
|
||||
|
||||
// Will contain request status
|
||||
const voteStatusContainer = document.createElement("div");
|
||||
voteStatusContainer.id = "sponsorTimesVoteStatusContainer" + UUID;
|
||||
@@ -593,7 +571,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
thanksForVotingText.classList.add("sponsorTimesThanksForVotingText");
|
||||
voteStatusContainer.appendChild(thanksForVotingText);
|
||||
|
||||
votingButtons.append(sponsorTimeButton);
|
||||
votingButtons.append(segmentSummary);
|
||||
votingButtons.append(voteButtonsContainer);
|
||||
votingButtons.append(voteStatusContainer);
|
||||
|
||||
@@ -901,8 +879,8 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
|
||||
/**
|
||||
* Converts time in minutes to 2d 5h 25.1
|
||||
* If less than 1 hour, just returns minutes
|
||||
*
|
||||
* @param {float} minutes
|
||||
*
|
||||
* @param {float} minutes
|
||||
* @returns {string}
|
||||
*/
|
||||
function getFormattedHours(minutes) {
|
||||
|
||||
@@ -72,20 +72,18 @@ function getFormattedTime(seconds: number, precise?: boolean): string {
|
||||
* @returns {string} errorMessage
|
||||
*/
|
||||
function getErrorMessage(statusCode: number, responseText: string): string {
|
||||
let errorMessage = "";
|
||||
const postFix = (responseText ? "\n\n" + responseText : "");
|
||||
|
||||
if([400, 429, 409, 502, 503, 0].includes(statusCode)) {
|
||||
//treat them the same
|
||||
const postFix = ((responseText && !responseText.includes(`cf-wrapper`)) ? "\n\n" + responseText : "");
|
||||
// display response body for 4xx
|
||||
if([400, 429, 409, 0].includes(statusCode)) {
|
||||
return chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode + postFix;
|
||||
} else if (statusCode >= 500 && statusCode <= 599) {
|
||||
// 503 == 502
|
||||
if (statusCode == 503) statusCode = 502;
|
||||
|
||||
errorMessage = chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode
|
||||
+ "\n\n" + chrome.i18n.getMessage("statusReminder");
|
||||
return chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode
|
||||
+ "\n\n" + chrome.i18n.getMessage("statusReminder");
|
||||
} else {
|
||||
errorMessage = chrome.i18n.getMessage("connectionError") + statusCode;
|
||||
return chrome.i18n.getMessage("connectionError") + statusCode + postFix;
|
||||
}
|
||||
|
||||
return errorMessage + postFix;
|
||||
}
|
||||
|
||||
/* Gets percieved luminance of a color */
|
||||
|
||||
Reference in New Issue
Block a user