Skip to content

Commit 71213b0

Browse files
committed
wip
1 parent 1b20e41 commit 71213b0

File tree

4 files changed

+63
-54
lines changed

4 files changed

+63
-54
lines changed

src/autoplay.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ export class Autoplay extends BackgroundBehavior {
5454
for (const [, elem] of querySelectorAllDeep(
5555
"video, audio, picture",
5656
).entries()) {
57-
if (!elem["__bx_autoplay_found"]) {
57+
interface AutoplayElement extends HTMLMediaElement {
58+
__bx_autoplay_found?: boolean;
59+
}
60+
if (!(elem as AutoplayElement)["__bx_autoplay_found"]) {
5861
if (!this.running) {
5962
if (this.processFetchableUrl(elem as HTMLMediaElement)) {
60-
elem["__bx_autoplay_found"] = true;
63+
(elem as AutoplayElement)["__bx_autoplay_found"] = true;
6164
}
6265
continue;
6366
}
6467

6568
await this.loadMedia(elem as HTMLMediaElement);
66-
elem["__bx_autoplay_found"] = true;
69+
(elem as AutoplayElement)["__bx_autoplay_found"] = true;
6770
}
6871
}
6972

@@ -140,46 +143,45 @@ export class Autoplay extends BackgroundBehavior {
140143
);
141144
}
142145

143-
void this.attemptMediaPlay(media).then(
144-
async (finished: Promise<void> | undefined) => {
145-
let check = true;
146+
void this.attemptMediaPlay(media).then(async (finished) => {
147+
let check = true;
146148

147-
if (finished) {
148-
void finished.then(() => (check = false));
149-
}
149+
if (finished) {
150+
// @ts-expect-error TODO: not sure what this is supposed to be, I believe `finished` is a boolean here?
151+
void finished.then(() => (check = false));
152+
}
150153

151-
while (check) {
152-
if (this.processFetchableUrl(media)) {
153-
check = false;
154-
}
155-
this.debug(
156-
"Waiting for fixed URL or media to finish: " + media.currentSrc,
157-
);
158-
await sleep(1000);
154+
while (check) {
155+
if (this.processFetchableUrl(media)) {
156+
check = false;
159157
}
160-
},
161-
);
158+
this.debug(
159+
"Waiting for fixed URL or media to finish: " + media.currentSrc,
160+
);
161+
await sleep(1000);
162+
}
163+
});
162164
} else if (media.currentSrc) {
163165
this.debug("media playing from non-URL source: " + media.currentSrc);
164166
}
165167
}
166168

167-
async attemptMediaPlay(media) {
169+
async attemptMediaPlay(media: HTMLMediaElement) {
168170
// finished promise
169-
let resolveFinished;
171+
let resolveFinished: (value?: boolean) => void;
170172

171-
const finished = new Promise((res) => {
173+
const finished = new Promise<boolean | undefined>((res) => {
172174
resolveFinished = res;
173175
});
174176

175177
// started promise
176-
let resolveStarted;
178+
let resolveStarted!: (value?: boolean) => void;
177179

178-
const started = new Promise((res) => {
180+
const started = new Promise<boolean | undefined>((res) => {
179181
resolveStarted = res;
180182
});
181183

182-
started.then(() => this.promises.push(finished));
184+
void started.then(() => this.promises.push(finished));
183185

184186
// already started
185187
if (!media.paused && media.currentTime > 0) {
@@ -242,7 +244,7 @@ export class Autoplay extends BackgroundBehavior {
242244
}
243245
}
244246

245-
media.play();
247+
void media.play();
246248

247249
if (await Promise.race([started, sleep(1000)])) {
248250
this.debug("play started after media.play()");

src/index.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
addLink,
1313
checkToJsonOverride,
1414
} from "./lib/utils";
15-
import { type Behavior, BehaviorRunner } from "./lib/behavior";
15+
import { AbstractBehavior, type Behavior, BehaviorRunner } from "./lib/behavior";
1616
import * as Lib from "./lib/utils";
1717

1818
import siteBehaviors from "./site";
@@ -27,10 +27,10 @@ interface BehaviorManagerOpts {
2727
autoplay?: boolean;
2828
autoscroll?: boolean;
2929
autoclick?: boolean;
30-
log?: ((...message: string[]) => void) | string | false;
31-
siteSpecific?: boolean | object;
30+
log?: ((...message: string[]) => void) | keyof typeof self | false;
31+
siteSpecific?: boolean | Record<string, unknown>;
3232
timeout?: number;
33-
fetchHeaders?: object | null;
33+
fetchHeaders?: Record<string, string> | null;
3434
startEarly?: boolean | null;
3535
clickSelector?: string;
3636
}
@@ -54,12 +54,14 @@ const DEFAULT_CLICK_SELECTOR = "a";
5454
const DEFAULT_LINK_SELECTOR = "a[href]";
5555
const DEFAULT_LINK_EXTRACT = "href";
5656

57+
type TODOBehaviorClass = AbstractBehavior<unknown, unknown, unknown>;
58+
5759
export class BehaviorManager {
58-
autofetch: AutoFetcher;
59-
behaviors: any[];
60-
loadedBehaviors: any;
60+
autofetch?: AutoFetcher;
61+
behaviors: TODOBehaviorClass[] | null;
62+
loadedBehaviors: TODOBehaviorClass;
6163
mainBehavior: Behavior | BehaviorRunner<unknown, unknown, unknown> | null;
62-
mainBehaviorClass: any;
64+
mainBehaviorClass: TODOBehaviorClass;
6365
inited: boolean;
6466
started: boolean;
6567
timeout?: number;
@@ -79,13 +81,13 @@ export class BehaviorManager {
7981
selector: DEFAULT_LINK_SELECTOR,
8082
extractName: DEFAULT_LINK_EXTRACT,
8183
};
82-
behaviorLog("Loaded behaviors for: " + self.location.href);
84+
void behaviorLog("Loaded behaviors for: " + self.location.href);
8385
}
8486

8587
init(
8688
opts: BehaviorManagerOpts = DEFAULT_OPTS,
8789
restart = false,
88-
customBehaviors: any[] = null,
90+
customBehaviors: TODOBehaviorClass[] | null = null,
8991
) {
9092
if (this.inited && !restart) {
9193
return;
@@ -94,6 +96,7 @@ export class BehaviorManager {
9496
this.inited = true;
9597
this.opts = opts;
9698

99+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
97100
if (!self.window) {
98101
return;
99102
}
@@ -119,22 +122,22 @@ export class BehaviorManager {
119122
this.autofetch = new AutoFetcher(
120123
!!opts.autofetch,
121124
opts.fetchHeaders,
122-
opts.startEarly,
125+
!!opts.startEarly,
123126
);
124127

125128
if (opts.autofetch) {
126-
behaviorLog("Using AutoFetcher");
127-
this.behaviors.push(this.autofetch);
129+
void behaviorLog("Using AutoFetcher");
130+
this.behaviors!.push(this.autofetch);
128131
}
129132

130133
if (opts.autoplay) {
131-
behaviorLog("Using Autoplay");
132-
this.behaviors.push(new Autoplay(this.autofetch, opts.startEarly));
134+
void behaviorLog("Using Autoplay");
135+
this.behaviors!.push(new Autoplay(this.autofetch, !!opts.startEarly));
133136
}
134137

135138
if (opts.autoclick) {
136-
behaviorLog("Using AutoClick");
137-
this.behaviors.push(
139+
void behaviorLog("Using AutoClick");
140+
this.behaviors!.push(
138141
new AutoClick(opts.clickSelector || DEFAULT_CLICK_SELECTOR),
139142
);
140143
}
@@ -144,7 +147,9 @@ export class BehaviorManager {
144147
try {
145148
this.load(behaviorClass);
146149
} catch (e) {
147-
behaviorLog(`Failed to load custom behavior: ${e} ${behaviorClass}`);
150+
void behaviorLog(
151+
`Failed to load custom behavior: ${e} ${behaviorClass}`,
152+
);
148153
}
149154
}
150155
}
@@ -157,11 +162,11 @@ export class BehaviorManager {
157162
const opts = this.opts;
158163
let siteMatch = false;
159164

160-
if (opts.siteSpecific) {
165+
if (opts?.siteSpecific) {
161166
for (const name in this.loadedBehaviors) {
162167
const siteBehaviorClass = this.loadedBehaviors[name];
163168
if (siteBehaviorClass.isMatch()) {
164-
behaviorLog("Using Site-Specific Behavior: " + name);
169+
void behaviorLog("Using Site-Specific Behavior: " + name);
165170
this.mainBehaviorClass = siteBehaviorClass;
166171
const siteSpecificOpts =
167172
typeof opts.siteSpecific === "object"
@@ -173,7 +178,10 @@ export class BehaviorManager {
173178
siteSpecificOpts,
174179
);
175180
} catch (e) {
176-
behaviorLog({ msg: e.toString(), siteSpecific: true }, "error");
181+
void behaviorLog(
182+
{ msg: (e as Error).toString(), siteSpecific: true },
183+
"error",
184+
);
177185
}
178186
siteMatch = true;
179187
break;

src/lib/behavior.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,9 @@ interface StaticAbstractBehavior {
132132
init: () => any;
133133
}
134134

135-
type AbstractBehavior<State, Opts, RunResult> = (new () => AbstractBehaviorInst<
136-
State,
137-
Opts,
138-
RunResult
139-
>) &
140-
StaticAbstractBehavior;
135+
export type AbstractBehavior<State, Opts, RunResult> =
136+
(new () => AbstractBehaviorInst<State, Opts, RunResult>) &
137+
StaticAbstractBehavior;
141138

142139
export class BehaviorRunner<State, Opts, RunResult> extends BackgroundBehavior {
143140
inst: AbstractBehaviorInst<State, Opts, RunResult>;

src/lib/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ export async function openWindow(
219219
return window.open(url);
220220
}
221221

222-
export function _setLogFunc(func: (message: string, level: string) => void) {
222+
export function _setLogFunc(
223+
func: ((message: string, level: string) => void) | null,
224+
) {
223225
_logFunc = func;
224226
}
225227

0 commit comments

Comments
 (0)