diff options
author | Ajay <[email protected]> | 2023-11-08 16:07:59 -0500 |
---|---|---|
committer | Ajay <[email protected]> | 2023-11-08 16:07:59 -0500 |
commit | e722ded58a928989bb583ae266acf765c129592c (patch) | |
tree | 28d2dea746c1b764737775564e65ce10b01ef3d4 /src/utils | |
parent | 6d37180d005f8f1c7691e0567ea4802425ca7569 (diff) | |
download | SponsorBlock-e722ded58a928989bb583ae266acf765c129592c.tar.gz SponsorBlock-e722ded58a928989bb583ae266acf765c129592c.zip |
Add dearrow promo based on title and remove old one
Also refactor requests out to seperate file
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/requests.ts | 47 | ||||
-rw-r--r-- | src/utils/videoLabels.ts | 3 | ||||
-rw-r--r-- | src/utils/warnings.ts | 9 |
3 files changed, 53 insertions, 6 deletions
diff --git a/src/utils/requests.ts b/src/utils/requests.ts new file mode 100644 index 00000000..8c160eb0 --- /dev/null +++ b/src/utils/requests.ts @@ -0,0 +1,47 @@ +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 = {}): Promise<FetchResponse> { + return sendRequestToCustomServer(type, url, data); +} + +/** + * 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 async function asyncRequestToServer(type: string, address: string, data = {}): Promise<FetchResponse> { + const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress; + + return await (asyncRequestToCustomServer(type, serverAddress + address, data)); +} + +/** + * 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); + }); +}
\ No newline at end of file diff --git a/src/utils/videoLabels.ts b/src/utils/videoLabels.ts index 731bfd33..82af788c 100644 --- a/src/utils/videoLabels.ts +++ b/src/utils/videoLabels.ts @@ -2,6 +2,7 @@ import { Category, CategorySkipOption, VideoID } from "../types"; import { getHash } from "../../maze-utils/src/hash"; import Utils from "../utils"; import { logWarn } from "./logger"; +import { asyncRequestToServer } from "./requests"; const utils = new Utils(); @@ -20,7 +21,7 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry | return cachedEntry; } - const response = await utils.asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`); + const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`); if (response.status !== 200) { // No video labels or server down labelCache[hashPrefix] = { diff --git a/src/utils/warnings.ts b/src/utils/warnings.ts index 72062699..e35c703c 100644 --- a/src/utils/warnings.ts +++ b/src/utils/warnings.ts @@ -3,8 +3,7 @@ import { getHash } from "../../maze-utils/src/hash"; import Config from "../config"; import GenericNotice, { NoticeOptions } from "../render/GenericNotice"; import { ContentContainer } from "../types"; -import Utils from "../utils"; -const utils = new Utils(); +import { asyncRequestToServer } from "./requests"; export interface ChatConfig { displayName: string; @@ -13,14 +12,14 @@ export interface ChatConfig { } export async function openWarningDialog(contentContainer: ContentContainer): Promise<void> { - const userInfo = await utils.asyncRequestToServer("GET", "/api/userInfo", { + const userInfo = await asyncRequestToServer("GET", "/api/userInfo", { publicUserID: await getHash(Config.config.userID), values: ["warningReason"] }); if (userInfo.ok) { const warningReason = JSON.parse(userInfo.responseText)?.warningReason; - const userNameData = await utils.asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID); + const userNameData = await asyncRequestToServer("GET", "/api/getUsername?userID=" + Config.config.userID); const userName = userNameData.ok ? JSON.parse(userNameData.responseText).userName : ""; const publicUserID = await getHash(Config.config.userID); @@ -43,7 +42,7 @@ export async function openWarningDialog(contentContainer: ContentContainer): Pro { name: chrome.i18n.getMessage("warningConfirmButton"), listener: async () => { - const result = await utils.asyncRequestToServer("POST", "/api/warnUser", { + const result = await asyncRequestToServer("POST", "/api/warnUser", { userID: Config.config.userID, enabled: false }); |