mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-06 03:27:03 +03:00
Improve error handling
- pass errors from background threads back to clients - log all HTTP request errors and display them to the user where possible
This commit is contained in:
Submodule maze-utils updated: e2f19e21c2...cbfe6baa92
Submodule public/_locales updated: c9dcf1ec21...17027165bd
@@ -3,7 +3,7 @@ import * as CompileConfig from "../config.json";
|
||||
import Config from "./config";
|
||||
import { Registration } from "./types";
|
||||
import "content-scripts-register-polyfill";
|
||||
import { sendRealRequestToCustomServer, setupBackgroundRequestProxy } from "../maze-utils/src/background-request-proxy";
|
||||
import { sendRealRequestToCustomServer, serializeOrStringify, setupBackgroundRequestProxy } from "../maze-utils/src/background-request-proxy";
|
||||
import { setupTabUpdates } from "../maze-utils/src/tab-updates";
|
||||
import { generateUserID } from "../maze-utils/src/setup";
|
||||
|
||||
@@ -227,33 +227,16 @@ async function submitVote(type: number, UUID: string, category: string, videoID:
|
||||
|
||||
try {
|
||||
const response = await asyncRequestToServer("POST", "/api/voteOnSponsorTime?UUID=" + UUID + "&videoID=" + videoID + "&userID=" + userID + typeSection);
|
||||
|
||||
if (response.ok) {
|
||||
return {
|
||||
successType: 1,
|
||||
responseText: await response.text()
|
||||
};
|
||||
} else if (response.status == 405) {
|
||||
//duplicate vote
|
||||
return {
|
||||
successType: 0,
|
||||
statusCode: response.status,
|
||||
responseText: await response.text()
|
||||
};
|
||||
} else {
|
||||
//error while connect
|
||||
return {
|
||||
successType: -1,
|
||||
statusCode: response.status,
|
||||
responseText: await response.text()
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
|
||||
return {
|
||||
successType: -1,
|
||||
statusCode: -1,
|
||||
responseText: ""
|
||||
status: response.status,
|
||||
ok: response.ok,
|
||||
responseText: await response.text(),
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Error while voting:", e);
|
||||
return {
|
||||
error: serializeOrStringify(e),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -263,4 +246,4 @@ async function asyncRequestToServer(type: string, address: string, data = {}) {
|
||||
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
||||
|
||||
return await (sendRealRequestToCustomServer(type, serverAddress + address, data));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ import { downvoteButtonColor, SkipNoticeAction } from "../utils/noticeUtils";
|
||||
import { VoteResponse } from "../messageTypes";
|
||||
import { AnimationUtils } from "../../maze-utils/src/animationUtils";
|
||||
import { Tooltip } from "../render/Tooltip";
|
||||
import { getErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { formatJSErrorMessage, getLongErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
export interface CategoryPillProps {
|
||||
vote: (type: number, UUID: SegmentUUID, category?: Category) => Promise<VoteResponse>;
|
||||
@@ -127,15 +128,19 @@ class CategoryPillComponent extends React.Component<CategoryPillProps, CategoryP
|
||||
const response = await this.props.vote(type, this.state.segment.UUID);
|
||||
await stopAnimation();
|
||||
|
||||
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
|
||||
if ("error" in response) {
|
||||
console.error("[SB] Caught error while attempting to vote on a FV label", response.error);
|
||||
alert(formatJSErrorMessage(response.error));
|
||||
} else if (response.ok || response.status === 429) {
|
||||
this.setState({
|
||||
open: false,
|
||||
show: type === 1
|
||||
});
|
||||
|
||||
this.closeTooltip();
|
||||
} else if (response.statusCode !== 403) {
|
||||
alert(getErrorMessage(response.statusCode, response.responseText));
|
||||
} else if (response.status !== 403) {
|
||||
logRequest({headers: null, ...response}, "SB", "vote on FV label");
|
||||
alert(getLongErrorMessage(response.status, response.responseText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ import { downvoteButtonColor, SkipNoticeAction } from "../utils/noticeUtils";
|
||||
import { VoteResponse } from "../messageTypes";
|
||||
import { AnimationUtils } from "../../maze-utils/src/animationUtils";
|
||||
import { Tooltip } from "../render/Tooltip";
|
||||
import { getErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { formatJSErrorMessage, getLongErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
export interface ChapterVoteProps {
|
||||
vote: (type: number, UUID: SegmentUUID, category?: Category) => Promise<VoteResponse>;
|
||||
@@ -119,12 +120,16 @@ class ChapterVoteComponent extends React.Component<ChapterVoteProps, ChapterVote
|
||||
const response = await this.props.vote(type, this.state.segment.UUID);
|
||||
await stopAnimation();
|
||||
|
||||
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
|
||||
if ("error" in response){
|
||||
console.error("[SB] Caught error while attempting to vote on a chapter", response.error);
|
||||
alert(formatJSErrorMessage(response.error));
|
||||
} else if (response.ok || response.status == 429) {
|
||||
this.setState({
|
||||
show: type === 1
|
||||
});
|
||||
} else if (response.statusCode !== 403) {
|
||||
alert(getErrorMessage(response.statusCode, response.responseText));
|
||||
} else if (response.status !== 403) {
|
||||
logRequest({headers: null, ...response}, "SB", "vote on chapter");
|
||||
alert(getLongErrorMessage(response.status, response.responseText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { defaultPreviewTime } from "../utils/constants";
|
||||
import { getVideo, getVideoDuration } from "../../maze-utils/src/video";
|
||||
import { AnimationUtils } from "../../maze-utils/src/animationUtils";
|
||||
import { Tooltip } from "../render/Tooltip";
|
||||
import { logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
export interface SponsorTimeEditProps {
|
||||
index: number;
|
||||
@@ -781,23 +782,26 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
if (this.props.contentContainer().channelIDInfo.status !== ChannelIDStatus.Found) return;
|
||||
|
||||
this.fetchingSuggestions = true;
|
||||
const result = await asyncRequestToServer("GET", "/api/chapterNames", {
|
||||
description,
|
||||
channelID: this.props.contentContainer().channelIDInfo.id
|
||||
});
|
||||
|
||||
if (result.ok) {
|
||||
try {
|
||||
try {
|
||||
const result = await asyncRequestToServer("GET", "/api/chapterNames", {
|
||||
description,
|
||||
channelID: this.props.contentContainer().channelIDInfo.id
|
||||
});
|
||||
if (result.ok) {
|
||||
const names = JSON.parse(result.responseText) as {description: string}[];
|
||||
this.setState({
|
||||
suggestedNames: names.map(n => ({
|
||||
label: n.description
|
||||
}))
|
||||
});
|
||||
} catch (e) {} //eslint-disable-line no-empty
|
||||
} else if (result.status !== 404) {
|
||||
logRequest(result, "SB", "chapter suggestion")
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("[SB] Caught error while fetching chapter suggestions", e);
|
||||
} finally {
|
||||
this.fetchingSuggestions = false;
|
||||
}
|
||||
|
||||
this.fetchingSuggestions = false;
|
||||
}
|
||||
|
||||
configUpdate(): void {
|
||||
|
||||
@@ -34,7 +34,7 @@ import { importTimes } from "./utils/exporter";
|
||||
import { ChapterVote } from "./render/ChapterVote";
|
||||
import { openWarningDialog } from "./utils/warnings";
|
||||
import { extensionUserAgent, isFirefoxOrSafari, waitFor } from "../maze-utils/src";
|
||||
import { getErrorMessage, getFormattedTime } from "../maze-utils/src/formating";
|
||||
import { formatJSErrorMessage, getFormattedTime, getLongErrorMessage } from "../maze-utils/src/formating";
|
||||
import { getChannelIDInfo, getVideo, getIsAdPlaying, getIsLivePremiere, setIsAdPlaying, checkVideoIDChange, getVideoID, getYouTubeVideoID, setupVideoModule, checkIfNewVideoID, isOnInvidious, isOnMobileYouTube, isOnYouTubeMusic, isOnYTTV, getLastNonInlineVideoID, triggerVideoIDChange, triggerVideoElementChange, getIsInline, getCurrentTime, setCurrentTime, getVideoDuration, verifyCurrentTime, waitForVideo } from "../maze-utils/src/video";
|
||||
import { Keybind, StorageChangesObject, isSafari, keybindEquals, keybindToString } from "../maze-utils/src/config";
|
||||
import { findValidElement } from "../maze-utils/src/dom"
|
||||
@@ -53,6 +53,7 @@ import { defaultPreviewTime } from "./utils/constants";
|
||||
import { onVideoPage } from "../maze-utils/src/pageInfo";
|
||||
import { getSegmentsForVideo } from "./utils/segmentData";
|
||||
import { getCategoryDefaultSelection, getCategorySelection } from "./utils/skipRule";
|
||||
import { FetchResponse, logRequest } from "../maze-utils/src/background-request-proxy";
|
||||
|
||||
cleanPage();
|
||||
|
||||
@@ -173,7 +174,7 @@ let popupInitialised = false;
|
||||
|
||||
let submissionNotice: SubmissionNotice = null;
|
||||
|
||||
let lastResponseStatus: number;
|
||||
let lastResponseStatus: number | Error | string;
|
||||
|
||||
// Contains all of the functions and variables needed by the skip notice
|
||||
const skipNoticeContentContainer: ContentContainer = () => ({
|
||||
@@ -1314,15 +1315,19 @@ function importExistingChapters(wait: boolean) {
|
||||
|
||||
async function lockedCategoriesLookup(): Promise<void> {
|
||||
const hashPrefix = (await getHash(getVideoID(), 1)).slice(0, 4);
|
||||
const response = await asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);
|
||||
try {
|
||||
const response = await asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);
|
||||
|
||||
if (response.ok) {
|
||||
try {
|
||||
if (response.ok) {
|
||||
const categoriesResponse = JSON.parse(response.responseText).filter((lockInfo) => lockInfo.videoID === getVideoID())[0]?.categories;
|
||||
if (Array.isArray(categoriesResponse)) {
|
||||
lockedCategories = categoriesResponse;
|
||||
}
|
||||
} catch (e) { } //eslint-disable-line no-empty
|
||||
} else if (response.status !== 404) {
|
||||
logRequest(response, "SB", "locked categories")
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`[SB] Caught error while looking up category locks for hashprefix ${hashPrefix}`, e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1724,7 +1729,11 @@ function sendTelemetryAndCount(skippingSegments: SponsorTime[], secondsSkipped:
|
||||
counted = true;
|
||||
}
|
||||
|
||||
if (fullSkip) asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID + "&videoID=" + getVideoID());
|
||||
if (fullSkip) asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID + "&videoID=" + getVideoID())
|
||||
.then(r => {
|
||||
if (!r.ok) logRequest(r, "SB", "segment skip log");
|
||||
})
|
||||
.catch(e => console.warn("[SB] Caught error while attempting to log segment skip", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2284,25 +2293,29 @@ function clearSponsorTimes() {
|
||||
async function vote(type: number, UUID: SegmentUUID, category?: Category, skipNotice?: SkipNoticeComponent): Promise<VoteResponse> {
|
||||
if (skipNotice !== null && skipNotice !== undefined) {
|
||||
//add loading info
|
||||
skipNotice.addVoteButtonInfo.bind(skipNotice)(chrome.i18n.getMessage("Loading"))
|
||||
skipNotice.setNoticeInfoMessage.bind(skipNotice)();
|
||||
skipNotice.addVoteButtonInfo(chrome.i18n.getMessage("Loading"))
|
||||
skipNotice.setNoticeInfoMessage();
|
||||
}
|
||||
|
||||
const response = await voteAsync(type, UUID, category);
|
||||
if (response != undefined) {
|
||||
//see if it was a success or failure
|
||||
if (skipNotice != null) {
|
||||
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
|
||||
if ("error" in response) {
|
||||
skipNotice.setNoticeInfoMessage(formatJSErrorMessage(response.error))
|
||||
skipNotice.resetVoteButtonInfo();
|
||||
} else if (response.ok || response.status === 429) {
|
||||
//success (treat rate limits as a success)
|
||||
skipNotice.afterVote.bind(skipNotice)(utils.getSponsorTimeFromUUID(sponsorTimes, UUID), type, category);
|
||||
} else if (response.successType == -1) {
|
||||
if (response.statusCode === 403 && response.responseText.startsWith("Vote rejected due to a tip from a moderator.")) {
|
||||
skipNotice.afterVote(utils.getSponsorTimeFromUUID(sponsorTimes, UUID), type, category);
|
||||
} else {
|
||||
logRequest({headers: null, ...response}, "SB", "vote on segment");
|
||||
if (response.status === 403 && response.responseText.startsWith("Vote rejected due to a tip from a moderator.")) {
|
||||
openWarningDialog(skipNoticeContentContainer);
|
||||
} else {
|
||||
skipNotice.setNoticeInfoMessage.bind(skipNotice)(getErrorMessage(response.statusCode, response.responseText))
|
||||
skipNotice.setNoticeInfoMessage(getLongErrorMessage(response.status, response.responseText))
|
||||
}
|
||||
|
||||
skipNotice.resetVoteButtonInfo.bind(skipNotice)();
|
||||
skipNotice.resetVoteButtonInfo();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2339,7 +2352,7 @@ async function voteAsync(type: number, UUID: SegmentUUID, category?: Category):
|
||||
category: category,
|
||||
videoID: getVideoID()
|
||||
}, (response) => {
|
||||
if (response.successType === 1) {
|
||||
if (response.ok === true) {
|
||||
// Change the sponsor locally
|
||||
const segment = utils.getSponsorTimeFromUUID(sponsorTimes, UUID);
|
||||
if (segment) {
|
||||
@@ -2468,13 +2481,23 @@ async function sendSubmitMessage(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
const response = await asyncRequestToServer("POST", "/api/skipSegments", {
|
||||
videoID: getVideoID(),
|
||||
userID: Config.config.userID,
|
||||
segments: sponsorTimesSubmitting,
|
||||
videoDuration: getVideoDuration(),
|
||||
userAgent: extensionUserAgent(),
|
||||
});
|
||||
let response: FetchResponse;
|
||||
try {
|
||||
response = await asyncRequestToServer("POST", "/api/skipSegments", {
|
||||
videoID: getVideoID(),
|
||||
userID: Config.config.userID,
|
||||
segments: sponsorTimesSubmitting,
|
||||
videoDuration: getVideoDuration(),
|
||||
userAgent: extensionUserAgent(),
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while attempting to submit segments", e);
|
||||
// Show that the upload failed
|
||||
playerButtons.submit.button.style.animation = "unset";
|
||||
playerButtons.submit.image.src = chrome.runtime.getURL("icons/PlayerUploadFailedIconSponsorBlocker.svg");
|
||||
alert(formatJSErrorMessage(e));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (response.status === 200) {
|
||||
stopAnimation();
|
||||
@@ -2523,7 +2546,8 @@ async function sendSubmitMessage(): Promise<boolean> {
|
||||
if (response.status === 403 && response.responseText.startsWith("Submission rejected due to a tip from a moderator.")) {
|
||||
openWarningDialog(skipNoticeContentContainer);
|
||||
} else {
|
||||
alert(getErrorMessage(response.status, response.responseText));
|
||||
logRequest(response, "SB", "segment submission");
|
||||
alert(getLongErrorMessage(response.status, response.responseText));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ export async function tryShowingDeArrowPromotion() {
|
||||
const title = deArrowDataJson?.[getVideoID()]?.titles?.[0];
|
||||
if (title && title.title && (title.locked || title.votes > 0)) {
|
||||
Config.config.showDeArrowPromotion = false;
|
||||
|
||||
|
||||
tooltip = new Tooltip({
|
||||
text: chrome.i18n.getMessage("DeArrowTitleReplacementSuggestion") + "\n\n" + title.title,
|
||||
linkOnClick: () => {
|
||||
@@ -71,4 +71,4 @@ function badTitle(title: string): boolean {
|
||||
|
||||
export function hideDeArrowPromotion(): void {
|
||||
if (tooltip) tooltip.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ export type Message = BaseMessage & (DefaultMessage | BoolValueMessage | IsInfoF
|
||||
|
||||
export interface IsInfoFoundMessageResponse {
|
||||
found: boolean;
|
||||
status: number;
|
||||
status: number | string | Error;
|
||||
sponsorTimes: SponsorTime[];
|
||||
time: number;
|
||||
onMobileYouTube: boolean;
|
||||
@@ -120,11 +120,13 @@ export type MessageResponse =
|
||||
| LogResponse
|
||||
| LoopedChapterResponse;
|
||||
|
||||
export interface VoteResponse {
|
||||
successType: number;
|
||||
statusCode: number;
|
||||
export type VoteResponse = {
|
||||
status: number;
|
||||
ok: boolean;
|
||||
responseText: string;
|
||||
}
|
||||
} | {
|
||||
error: Error | string;
|
||||
};
|
||||
|
||||
interface ImportSegmentsResponse {
|
||||
importedSegments: SponsorTime[];
|
||||
|
||||
@@ -609,6 +609,8 @@ function activatePrivateTextChange(element: HTMLElement) {
|
||||
if (userInfo.warnings > 0 || userInfo.banned) {
|
||||
setButton.classList.add("hidden");
|
||||
}
|
||||
}).catch(e => {
|
||||
console.error("[SB] Caught error while fetching user info for the new user ID", e)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ export enum LoadingStatus {
|
||||
SegmentsFound,
|
||||
NoSegmentsFound,
|
||||
ConnectionError,
|
||||
JSError,
|
||||
StillLoading,
|
||||
NoVideo
|
||||
}
|
||||
@@ -24,6 +25,7 @@ export enum LoadingStatus {
|
||||
export interface LoadingData {
|
||||
status: LoadingStatus;
|
||||
code?: number;
|
||||
error?: Error | string;
|
||||
}
|
||||
|
||||
let loadRetryCount = 0;
|
||||
@@ -302,7 +304,9 @@ function getVideoStatusText(status: LoadingData): string {
|
||||
case LoadingStatus.NoSegmentsFound:
|
||||
return chrome.i18n.getMessage("sponsor404");
|
||||
case LoadingStatus.ConnectionError:
|
||||
return chrome.i18n.getMessage("connectionError") + status.code;
|
||||
return `${chrome.i18n.getMessage("connectionError")} ${chrome.i18n.getMessage("errorCode").replace("{code}", `${status.code}`)}`;
|
||||
case LoadingStatus.JSError:
|
||||
return `${chrome.i18n.getMessage("connectionError")} ${status.error}`;
|
||||
case LoadingStatus.StillLoading:
|
||||
return chrome.i18n.getMessage("segmentsStillLoading");
|
||||
case LoadingStatus.NoVideo:
|
||||
@@ -350,6 +354,11 @@ function segmentsLoaded(response: IsInfoFoundMessageResponse, props: SegmentsLoa
|
||||
props.setStatus({
|
||||
status: LoadingStatus.SegmentsFound
|
||||
});
|
||||
} else if (typeof response.status !== "number") {
|
||||
props.setStatus({
|
||||
status: LoadingStatus.JSError,
|
||||
error: response.status,
|
||||
})
|
||||
} else if (response.status === 404 || response.status === 200) {
|
||||
props.setStatus({
|
||||
status: LoadingStatus.NoSegmentsFound
|
||||
@@ -457,4 +466,4 @@ window.addEventListener("message", async (e): Promise<void> => {
|
||||
style.textContent = e.data.css;
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import { ActionType, SegmentUUID, SponsorHideType, SponsorTime, VideoID } from "
|
||||
import Config from "../config";
|
||||
import { waitFor } from "../../maze-utils/src";
|
||||
import { shortCategoryName } from "../utils/categoryUtils";
|
||||
import { getErrorMessage, getFormattedTime } from "../../maze-utils/src/formating";
|
||||
import { formatJSErrorMessage, getFormattedTime, getShortErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { AnimationUtils } from "../../maze-utils/src/animationUtils";
|
||||
import { asyncRequestToServer } from "../utils/requests";
|
||||
import { Message, MessageResponse, VoteResponse } from "../messageTypes";
|
||||
@@ -11,6 +11,7 @@ import { LoadingStatus } from "./PopupComponent";
|
||||
import GenericNotice from "../render/GenericNotice";
|
||||
import { exportTimes } from "../utils/exporter";
|
||||
import { copyToClipboardPopup } from "./popupUtils";
|
||||
import { logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
interface SegmentListComponentProps {
|
||||
videoID: VideoID;
|
||||
@@ -192,20 +193,26 @@ function SegmentListItem({ segment, videoID, currentTime, isVip, startingLooped,
|
||||
onClick={async (e) => {
|
||||
const stopAnimation = AnimationUtils.applyLoadingAnimation(e.currentTarget, 0.3);
|
||||
|
||||
if (segment.UUID.length > 60) {
|
||||
copyToClipboardPopup(segment.UUID, sendMessage);
|
||||
} else {
|
||||
const segmentIDData = await asyncRequestToServer("GET", "/api/segmentID", {
|
||||
UUID: segment.UUID,
|
||||
videoID: videoID
|
||||
});
|
||||
|
||||
if (segmentIDData.ok && segmentIDData.responseText) {
|
||||
copyToClipboardPopup(segmentIDData.responseText, sendMessage);
|
||||
try {
|
||||
if (segment.UUID.length > 60) {
|
||||
copyToClipboardPopup(segment.UUID, sendMessage);
|
||||
} else {
|
||||
const segmentIDData = await asyncRequestToServer("GET", "/api/segmentID", {
|
||||
UUID: segment.UUID,
|
||||
videoID: videoID
|
||||
});
|
||||
if (segmentIDData.ok && segmentIDData.responseText) {
|
||||
copyToClipboardPopup(segmentIDData.responseText, sendMessage);
|
||||
} else {
|
||||
logRequest(segmentIDData, "SB", "segment UUID resolution");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while attempting to resolve and copy segment UUID", e);
|
||||
} finally {
|
||||
stopAnimation();
|
||||
}
|
||||
|
||||
stopAnimation();
|
||||
}}/>
|
||||
{
|
||||
segment.actionType === ActionType.Chapter &&
|
||||
@@ -298,14 +305,24 @@ async function vote(props: {
|
||||
}) as VoteResponse;
|
||||
|
||||
if (response != undefined) {
|
||||
let messageDuration = 1_500;
|
||||
// See if it was a success or failure
|
||||
if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) {
|
||||
if ("error" in response) {
|
||||
// JS error
|
||||
console.error("[SB] Caught error while attempting to submit a vote", response.error);
|
||||
props.setVoteMessage(formatJSErrorMessage(response.error));
|
||||
messageDuration = 10_000;
|
||||
}
|
||||
else if (response.ok || response.status === 429) {
|
||||
// Success (treat rate limits as a success)
|
||||
props.setVoteMessage(chrome.i18n.getMessage("voted"));
|
||||
} else if (response.successType == -1) {
|
||||
props.setVoteMessage(getErrorMessage(response.statusCode, response.responseText));
|
||||
} else {
|
||||
// Error
|
||||
logRequest({headers: null, ...response}, "SB", "vote on segment");
|
||||
props.setVoteMessage(getShortErrorMessage(response.status, response.responseText));
|
||||
messageDuration = 10_000;
|
||||
}
|
||||
setTimeout(() => props.setVoteMessage(null), 1500);
|
||||
setTimeout(() => props.setVoteMessage(null), messageDuration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -433,4 +450,4 @@ function ImportSegments(props: ImportSegmentsProps) {
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import * as React from "react";
|
||||
import { getHash } from "../../maze-utils/src/hash";
|
||||
import { getErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { formatJSErrorMessage, getShortErrorMessage } from "../../maze-utils/src/formating";
|
||||
import Config from "../config";
|
||||
import { asyncRequestToServer } from "../utils/requests";
|
||||
import PencilIcon from "../svg-icons/pencilIcon";
|
||||
import ClipboardIcon from "../svg-icons/clipboardIcon";
|
||||
import CheckIcon from "../svg-icons/checkIcon";
|
||||
import { showDonationLink } from "../utils/configUtils";
|
||||
import { FetchResponse, logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
export const YourWorkComponent = () => {
|
||||
const [isSettingUsername, setIsSettingUsername] = React.useState(false);
|
||||
@@ -21,10 +22,16 @@ export const YourWorkComponent = () => {
|
||||
React.useEffect(() => {
|
||||
(async () => {
|
||||
const values = ["userName", "viewCount", "minutesSaved", "vip", "permissions", "segmentCount"];
|
||||
const result = await asyncRequestToServer("GET", "/api/userInfo", {
|
||||
publicUserID: await getHash(Config.config!.userID!),
|
||||
values
|
||||
});
|
||||
let result: FetchResponse;
|
||||
try {
|
||||
result = await asyncRequestToServer("GET", "/api/userInfo", {
|
||||
publicUserID: await getHash(Config.config!.userID!),
|
||||
values
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while fetching user info", e);
|
||||
return
|
||||
}
|
||||
|
||||
if (result.ok) {
|
||||
const userInfo = JSON.parse(result.responseText);
|
||||
@@ -38,6 +45,8 @@ export const YourWorkComponent = () => {
|
||||
|
||||
setShowDonateMessage(Config.config.showDonationLink && Config.config.donateClicked <= 0 && Config.config.showPopupDonationCount < 5
|
||||
&& viewCount < 50000 && !Config.config.isVip && Config.config.skipCount > 10 && showDonationLink());
|
||||
} else {
|
||||
logRequest(result, "SB", "user info");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
@@ -94,10 +103,12 @@ export const YourWorkComponent = () => {
|
||||
setUsername(newUsername);
|
||||
setIsSettingUsername(!isSettingUsername);
|
||||
} else {
|
||||
setUsernameSubmissionStatus(getErrorMessage(result.status, result.responseText));
|
||||
logRequest(result, "SB", "username change");
|
||||
setUsernameSubmissionStatus(getShortErrorMessage(result.status, result.responseText));
|
||||
}
|
||||
}).catch((e) => {
|
||||
setUsernameSubmissionStatus(`${chrome.i18n.getMessage("Error")}: ${e}`);
|
||||
console.error("[SB] Caught error while requesting a username change", e)
|
||||
setUsernameSubmissionStatus(formatJSErrorMessage(e));
|
||||
});
|
||||
}
|
||||
}}>
|
||||
@@ -205,4 +216,4 @@ function getFormattedHours(minutes) {
|
||||
const days = Math.floor(minutes / 1440) % 365;
|
||||
const hours = Math.floor(minutes / 60) % 24;
|
||||
return (years > 0 ? years + chrome.i18n.getMessage("yearAbbreviation") + " " : "") + (days > 0 ? days + chrome.i18n.getMessage("dayAbbreviation") + " " : "") + (hours > 0 ? hours + chrome.i18n.getMessage("hourAbbreviation") + " " : "") + (minutes % 60).toFixed(1);
|
||||
}
|
||||
}
|
||||
|
||||
21
src/utils.ts
21
src/utils.ts
@@ -6,6 +6,8 @@ import { waitFor } from "../maze-utils/src";
|
||||
import { findValidElementFromSelector } from "../maze-utils/src/dom";
|
||||
import { isSafari } from "../maze-utils/src/config";
|
||||
import { asyncRequestToServer } from "./utils/requests";
|
||||
import { FetchResponse, logRequest } from "../maze-utils/src/background-request-proxy";
|
||||
import { formatJSErrorMessage, getLongErrorMessage } from "../maze-utils/src/formating";
|
||||
|
||||
export default class Utils {
|
||||
|
||||
@@ -276,13 +278,24 @@ export default class Utils {
|
||||
|| !Config.config.trackDownvotes) return;
|
||||
|
||||
if (segmentUUID.length < 60) {
|
||||
const segmentIDData = await asyncRequestToServer("GET", "/api/segmentID", {
|
||||
UUID: segmentUUID,
|
||||
videoID
|
||||
});
|
||||
let segmentIDData: FetchResponse;
|
||||
try {
|
||||
segmentIDData = await asyncRequestToServer("GET", "/api/segmentID", {
|
||||
UUID: segmentUUID,
|
||||
videoID
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while trying to resolve the segment UUID to be hidden", e);
|
||||
alert(`${chrome.i18n.getMessage("segmentHideFailed")}\n${formatJSErrorMessage(e)}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (segmentIDData.ok && segmentIDData.responseText) {
|
||||
segmentUUID = segmentIDData.responseText;
|
||||
} else {
|
||||
logRequest(segmentIDData, "SB", "segment UUID resolution");
|
||||
alert(`${chrome.i18n.getMessage("segmentHideFailed")}\n${getLongErrorMessage(segmentIDData.status, segmentIDData.responseText)}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,17 +2,6 @@ import Config from "../config";
|
||||
import * as CompileConfig from "../../config.json";
|
||||
import { FetchResponse, sendRequestToCustomServer } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
/**
|
||||
* Sends a request to a custom server
|
||||
*
|
||||
* @param type The request type. "GET", "POST", etc.
|
||||
* @param address The address to add to the SponsorBlock server address
|
||||
* @param callback
|
||||
*/
|
||||
export function asyncRequestToCustomServer(type: string, url: string, data = {}, headers = {}): Promise<FetchResponse> {
|
||||
return sendRequestToCustomServer(type, url, data, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to the SponsorBlock server with address added as a query
|
||||
*
|
||||
@@ -23,25 +12,5 @@ export function asyncRequestToCustomServer(type: string, url: string, data = {},
|
||||
export async function asyncRequestToServer(type: string, address: string, data = {}, headers = {}): Promise<FetchResponse> {
|
||||
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
||||
|
||||
return await (asyncRequestToCustomServer(type, serverAddress + address, data, headers));
|
||||
return await (sendRequestToCustomServer(type, serverAddress + address, data, headers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to the SponsorBlock server with address added as a query
|
||||
*
|
||||
* @param type The request type. "GET", "POST", etc.
|
||||
* @param address The address to add to the SponsorBlock server address
|
||||
* @param callback
|
||||
*/
|
||||
export function sendRequestToServer(type: string, address: string, callback?: (response: FetchResponse) => void): void {
|
||||
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
|
||||
|
||||
// Ask the background script to do the work
|
||||
chrome.runtime.sendMessage({
|
||||
message: "sendRequest",
|
||||
type,
|
||||
url: serverAddress + address
|
||||
}, (response) => {
|
||||
callback(response);
|
||||
});
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { ActionType, ActionTypes, SponsorSourceType, SponsorTime, VideoID } from
|
||||
import { getHashParams } from "./pageUtils";
|
||||
import { asyncRequestToServer } from "./requests";
|
||||
import { extensionUserAgent } from "../../maze-utils/src";
|
||||
import { logRequest, serializeOrStringify } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
const segmentDataCache = new DataCache<VideoID, SegmentResponse>(() => {
|
||||
return {
|
||||
@@ -18,7 +19,7 @@ const pendingList: Record<VideoID, Promise<SegmentResponse>> = {};
|
||||
|
||||
export interface SegmentResponse {
|
||||
segments: SponsorTime[] | null;
|
||||
status: number;
|
||||
status: number | Error | string;
|
||||
}
|
||||
|
||||
export async function getSegmentsForVideo(videoID: VideoID, ignoreCache: boolean): Promise<SegmentResponse> {
|
||||
@@ -40,6 +41,12 @@ export async function getSegmentsForVideo(videoID: VideoID, ignoreCache: boolean
|
||||
let result: Awaited<typeof pendingData>;
|
||||
try {
|
||||
result = await pendingData;
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while fetching segments", e);
|
||||
return {
|
||||
segments: null,
|
||||
status: serializeOrStringify(e),
|
||||
}
|
||||
} finally {
|
||||
delete pendingList[videoID];
|
||||
}
|
||||
@@ -89,6 +96,7 @@ async function fetchSegmentsForVideo(videoID: VideoID): Promise<SegmentResponse>
|
||||
segmentDataCache.setupCache(videoID);
|
||||
}
|
||||
}
|
||||
if (response.status !== 404) logRequest(response, "SB", "skip segments");
|
||||
|
||||
return {
|
||||
segments: null,
|
||||
@@ -106,4 +114,4 @@ function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
|
||||
}
|
||||
|
||||
return actionTypes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { getHash } from "../../maze-utils/src/hash";
|
||||
import { logWarn } from "./logger";
|
||||
import { asyncRequestToServer } from "./requests";
|
||||
import { getCategorySelection } from "./skipRule";
|
||||
import { FetchResponse, logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
|
||||
export interface VideoLabelsCacheData {
|
||||
category: Category;
|
||||
@@ -24,8 +25,15 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
|
||||
return cachedEntry;
|
||||
}
|
||||
|
||||
const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}?hasStartSegment=true`);
|
||||
let response: FetchResponse;
|
||||
try {
|
||||
response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}?hasStartSegment=true`);
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while fetching video labels", e)
|
||||
return null;
|
||||
}
|
||||
if (response.status !== 200) {
|
||||
logRequest(response, "SB", "video labels");
|
||||
// No video labels or server down
|
||||
labelCache[hashPrefix] = {
|
||||
timestamp: Date.now(),
|
||||
@@ -85,4 +93,4 @@ export async function getHasStartSegment(videoID: VideoID): Promise<boolean | nu
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { objectToURI } from "../../maze-utils/src";
|
||||
import { FetchResponse, logRequest } from "../../maze-utils/src/background-request-proxy";
|
||||
import { formatJSErrorMessage, getLongErrorMessage } from "../../maze-utils/src/formating";
|
||||
import { getHash } from "../../maze-utils/src/hash";
|
||||
import Config from "../config";
|
||||
import GenericNotice, { NoticeOptions } from "../render/GenericNotice";
|
||||
@@ -12,15 +14,26 @@ export interface ChatConfig {
|
||||
}
|
||||
|
||||
export async function openWarningDialog(contentContainer: ContentContainer): Promise<void> {
|
||||
const userInfo = await asyncRequestToServer("GET", "/api/userInfo", {
|
||||
publicUserID: await getHash(Config.config.userID),
|
||||
values: ["warningReason"]
|
||||
});
|
||||
let userInfo: FetchResponse;
|
||||
try {
|
||||
userInfo = await asyncRequestToServer("GET", "/api/userInfo", {
|
||||
publicUserID: await getHash(Config.config.userID),
|
||||
values: ["warningReason"]
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while trying to fetch user's active warnings", e)
|
||||
return;
|
||||
}
|
||||
|
||||
if (userInfo.ok) {
|
||||
const warningReason = JSON.parse(userInfo.responseText)?.warningReason;
|
||||
const userNameData = await asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID);
|
||||
const userName = userNameData.ok ? JSON.parse(userNameData.responseText).userName : "";
|
||||
let userName = "";
|
||||
try {
|
||||
const userNameData = await asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID);
|
||||
userName = userNameData.ok ? JSON.parse(userNameData.responseText).userName : "";
|
||||
} catch (e) {
|
||||
console.warn("[SB] Caught non-fatal error while trying to resolve user's username", e);
|
||||
}
|
||||
const publicUserID = await getHash(Config.config.userID);
|
||||
|
||||
let notice: GenericNotice = null;
|
||||
@@ -42,15 +55,22 @@ export async function openWarningDialog(contentContainer: ContentContainer): Pro
|
||||
{
|
||||
name: chrome.i18n.getMessage("warningConfirmButton"),
|
||||
listener: async () => {
|
||||
const result = await asyncRequestToServer("POST", "/api/warnUser", {
|
||||
userID: Config.config.userID,
|
||||
enabled: false
|
||||
});
|
||||
let result: FetchResponse;
|
||||
try {
|
||||
result = await asyncRequestToServer("POST", "/api/warnUser", {
|
||||
userID: Config.config.userID,
|
||||
enabled: false
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[SB] Caught error while trying to acknowledge user's active warning", e);
|
||||
alert(formatJSErrorMessage(e));
|
||||
}
|
||||
|
||||
if (result.ok) {
|
||||
notice?.close();
|
||||
} else {
|
||||
alert(`${chrome.i18n.getMessage("warningError")} ${result.status}`);
|
||||
logRequest(result, "SB", "warning acknowledgement");
|
||||
alert(getLongErrorMessage(result.status, result.responseText));
|
||||
}
|
||||
}
|
||||
}],
|
||||
@@ -58,6 +78,8 @@ export async function openWarningDialog(contentContainer: ContentContainer): Pro
|
||||
};
|
||||
|
||||
notice = new GenericNotice(contentContainer, "warningNotice", options);
|
||||
} else {
|
||||
logRequest(userInfo, "SB", "user's active warnings");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user