Skip to content

Commit 9a6f6f8

Browse files
Merge remote-tracking branch 'lewisdoesstuff/master'
2 parents b407497 + 8b744af commit 9a6f6f8

File tree

10 files changed

+67
-19
lines changed

10 files changed

+67
-19
lines changed

config.json.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"serverAddress": "https://sponsor.ajay.app",
33
"testingServerAddress": "https://sponsor.ajay.app/test",
4+
"fallbackServerAddress": "",
45
"serverAddressComment": "This specifies the default SponsorBlock server to connect to",
6+
"fallbackAddressComment": "This specifies the server SponsorBlock will attempt to connect to if the primary one fails.",
57
"categoryList": ["sponsor", "selfpromo", "exclusive_access", "interaction", "poi_highlight", "intro", "outro", "preview", "filler", "chapter", "music_offtopic"],
68
"categorySupport": {
79
"sponsor": ["skip", "mute", "full"],

public/_locales/en/messages.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,12 @@
505505
"customServerAddressDescription": {
506506
"message": "The address SponsorBlock uses to make calls to the server.\nUnless you have your own server instance, this should not be changed."
507507
},
508+
"fallbackServerAddress": {
509+
"message": "SponsorBlock Fallback Server Address"
510+
},
511+
"fallbackServerAddressDescription": {
512+
"message": "The address SponsorBlock uses to make calls to the server if the main server is down."
513+
},
508514
"save": {
509515
"message": "Save"
510516
},

public/options/options.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,26 @@ <h2>__MSG_exportOptions__</h2>
554554
</div>
555555
</div>
556556
</div>
557+
558+
<div data-type="text-change" data-sync="fallbackServerAddress" data-dependent-on="testingServer" data-dependent-on-inverted="true">
559+
<label class="optionLabel inline">
560+
<span class="optionLabel">__MSG_fallbackServerAddress__:</span>
561+
562+
<input class="option-text-box" type="text" style="margin-right:10px">
563+
</label>
564+
565+
<div class="small-description">__MSG_fallbackServerAddressDescription__</div>
566+
567+
<div class="next-line">
568+
<div class="option-button text-change-set inline">
569+
__MSG_save__
570+
</div>
571+
572+
<div class="option-button text-change-reset inline">
573+
__MSG_reset__
574+
</div>
575+
</div>
576+
</div>
557577

558578
</div>
559579

src/components/SponsorTimeEditComponent.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,10 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
696696
this.saveEditTimes();
697697
}
698698

699-
async fetchSuggestions(description: string): Promise<void> {
699+
async fetchSuggestions(description: string, fallbackServer = false): Promise<void> {
700700
if (this.props.contentContainer().channelIDInfo.status !== ChannelIDStatus.Found) return;
701-
702701
this.fetchingSuggestions = true;
703-
const result = await utils.asyncRequestToServer("GET", "/api/chapterNames", {
702+
const result = await utils.asyncRequestToServer("GET", "/api/chapterNames", fallbackServer, {
704703
description,
705704
channelID: this.props.contentContainer().channelIDInfo.id
706705
});
@@ -713,7 +712,11 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
713712
label: n.description
714713
}))
715714
});
716-
} catch (e) {} //eslint-disable-line no-empty
715+
} catch (e) {
716+
if (!fallbackServer) {
717+
this.fetchSuggestions(description, true);
718+
}
719+
}
717720
}
718721

719722
this.fetchingSuggestions = false;

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface SBConfig {
4141
invidiousInstances: string[];
4242
supportInvidious: boolean;
4343
serverAddress: string;
44+
fallbackServerAddress: string;
4445
minDuration: number;
4546
skipNoticeDuration: number;
4647
audioNotificationOnSkip: boolean;
@@ -181,6 +182,7 @@ const Config: SBObject = {
181182
invidiousInstances: ["invidious.snopyta.org"], // leave as default
182183
supportInvidious: false,
183184
serverAddress: CompileConfig.serverAddress,
185+
fallbackServerAddress: CompileConfig.fallbackServerAddress,
184186
minDuration: 0,
185187
skipNoticeDuration: 4,
186188
audioNotificationOnSkip: false,

src/content.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ let isAdPlaying = false;
155155
let lastResponseStatus: number;
156156
let retryCount = 0;
157157

158+
// flips between true/false on server request failure
159+
let fallbackServer = false;
160+
158161
// Contains all of the functions and variables needed by the skip notice
159162
const skipNoticeContentContainer: ContentContainer = () => ({
160163
vote,
@@ -1008,9 +1011,8 @@ async function sponsorsLookup(keepOldSubmissions = true) {
10081011
const extraRequestData: Record<string, unknown> = {};
10091012
const hashParams = getHashParams();
10101013
if (hashParams.requiredSegment) extraRequestData.requiredSegment = hashParams.requiredSegment;
1011-
10121014
const hashPrefix = (await utils.getHash(sponsorVideoID, 1)).slice(0, 4) as VideoID & HashedValue;
1013-
const response = await utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, {
1015+
const response = await utils.asyncRequestToServer('GET', "/api/skipSegments/" + hashPrefix, fallbackServer, {
10141016
categories,
10151017
actionTypes: getEnabledActionTypes(showChapterMessage),
10161018
userAgent: `${chrome.runtime.id}`,
@@ -1019,7 +1021,6 @@ async function sponsorsLookup(keepOldSubmissions = true) {
10191021

10201022
// store last response status
10211023
lastResponseStatus = response?.status;
1022-
10231024
if (response?.ok) {
10241025
let recievedSegments: SponsorTime[] = JSON.parse(response.responseText)
10251026
?.filter((video) => video.videoID === sponsorVideoID)
@@ -1170,7 +1171,7 @@ function getEnabledActionTypes(forceFullVideo = false): ActionType[] {
11701171

11711172
async function lockedCategoriesLookup(): Promise<void> {
11721173
const hashPrefix = (await utils.getHash(sponsorVideoID, 1)).slice(0, 4);
1173-
const response = await utils.asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix);
1174+
const response = await utils.asyncRequestToServer("GET", "/api/lockCategories/" + hashPrefix, fallbackServer);
11741175

11751176
if (response.ok) {
11761177
try {
@@ -1192,9 +1193,16 @@ function retryFetch(errorCode: number): void {
11921193
return;
11931194
}
11941195

1196+
fallbackServer = retryCount % 2 === 0
11951197
retryCount++;
1196-
1197-
const delay = errorCode === 404 ? (30000 + Math.random() * 30000) : (2000 + Math.random() * 10000);
1198+
let delay: number;
1199+
switch (errorCode) {
1200+
case 404:
1201+
delay = (30000 + Math.random() * 30000);
1202+
break;
1203+
default:
1204+
delay = retryCount <= 2 ? 0 : (2000 + Math.random() * 10000);
1205+
}
11981206
retryFetchTimeout = setTimeout(() => {
11991207
if (sponsorVideoID && sponsorTimes?.length === 0
12001208
|| sponsorTimes.every((segment) => segment.source !== SponsorSourceType.Server)) {
@@ -1630,7 +1638,7 @@ function sendTelemetryAndCount(skippingSegments: SponsorTime[], secondsSkipped:
16301638
counted = true;
16311639
}
16321640

1633-
if (fullSkip) utils.asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID);
1641+
if (fullSkip) utils.asyncRequestToServer("POST", "/api/viewedVideoSponsorTime?UUID=" + segment.UUID, false);
16341642
}
16351643
}
16361644
}
@@ -2236,7 +2244,7 @@ async function sendSubmitMessage() {
22362244
}
22372245
}
22382246

2239-
const response = await utils.asyncRequestToServer("POST", "/api/skipSegments", {
2247+
const response = await utils.asyncRequestToServer("POST", "/api/skipSegments", false, {
22402248
videoID: sponsorVideoID,
22412249
userID: Config.config.userID,
22422250
segments: sponsorTimesSubmitting,

src/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ async function init() {
175175
textChangeSetButton.addEventListener("click", async () => {
176176
// See if anything extra must be done
177177
switch (option) {
178-
case "serverAddress": {
178+
case "serverAddress":
179+
case "fallbackServerAddress": {
179180
const result = validateServerAddress(textChangeInput.value);
180181

181182
if (result !== null) {

src/utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,17 @@ export default class Utils {
365365
*
366366
* @param type The request type. "GET", "POST", etc.
367367
* @param address The address to add to the SponsorBlock server address
368+
* @param fallback Use the fallback server URL if true.
368369
* @param callback
369370
*/
370-
async asyncRequestToServer(type: string, address: string, data = {}): Promise<FetchResponse> {
371-
const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;
372-
371+
async asyncRequestToServer(type: string, address: string, fallback = false, data = {}): Promise<FetchResponse> {
372+
let serverAddress = Config.config.serverAddress;
373+
if (Config.config.testingServer){
374+
serverAddress = CompileConfig.testingServerAddress;
375+
} else if (fallback && Config.config.fallbackServerAddress) {
376+
serverAddress = Config.config.fallbackServerAddress;
377+
}
378+
373379
return await (this.asyncRequestToCustomServer(type, serverAddress + address, data));
374380
}
375381

src/utils/licenseKey.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as CompileConfig from "../../config.json";
55
const utils = new Utils();
66

77
export async function checkLicenseKey(licenseKey: string): Promise<boolean> {
8-
const result = await utils.asyncRequestToServer("GET", "/api/verifyToken", {
8+
const result = await utils.asyncRequestToServer("GET", "/api/verifyToken", false,{
99
licenseKey
1010
});
1111

@@ -51,7 +51,7 @@ export async function fetchingChaptersAllowed(): Promise<boolean> {
5151
Config.forceSyncUpdate("payments");
5252

5353
// Check for free access if no license key, and it is the first time
54-
const result = await utils.asyncRequestToServer("GET", "/api/userInfo", {
54+
const result = await utils.asyncRequestToServer("GET", "/api/userInfo", false,{
5555
value: "freeChaptersAccess",
5656
publicUserID: await utils.getHash(Config.config.userID)
5757
});

src/utils/warnings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function openWarningDialog(contentContainer: ContentContainer): Pro
4242
{
4343
name: chrome.i18n.getMessage("warningConfirmButton"),
4444
listener: async () => {
45-
const result = await utils.asyncRequestToServer("POST", "/api/warnUser", {
45+
const result = await utils.asyncRequestToServer("POST", "/api/warnUser", false, {
4646
userID: Config.config.userID,
4747
enabled: false
4848
});

0 commit comments

Comments
 (0)