This commit is contained in:
Ajay
2025-09-09 03:36:49 -04:00
19 changed files with 1228 additions and 3465 deletions

View File

@@ -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);
});
}

View File

@@ -6,6 +6,7 @@ import { ActionTypes, SponsorSourceType, SponsorTime, VideoID } from "../types";
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> {
@@ -37,8 +38,18 @@ export async function getSegmentsForVideo(videoID: VideoID, ignoreCache: boolean
const pendingData = fetchSegmentsForVideo(videoID);
pendingList[videoID] = pendingData;
const result = await pendingData;
delete pendingList[videoID];
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];
}
return result;
}
@@ -82,6 +93,7 @@ async function fetchSegmentsForVideo(videoID: VideoID): Promise<SegmentResponse>
segmentDataCache.setupCache(videoID);
}
}
if (response.status !== 404) logRequest(response, "SB", "skip segments");
return {
segments: null,

View File

@@ -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;
}
}

View File

@@ -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");
}
}