Skip to content

Commit 11a65ec

Browse files
committed
fix remaining type & lint issues
1 parent 1d6e853 commit 11a65ec

File tree

7 files changed

+72
-46
lines changed

7 files changed

+72
-46
lines changed

src/autoplay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Autoplay extends BackgroundBehavior {
99
mediaSet: Set<string>;
1010
autofetcher: AutoFetcher;
1111
numPlaying: number;
12-
promises: Promise<any>[];
12+
promises: Promise<boolean | undefined>[];
1313
_initDone: Function;
1414
running = false;
1515
polling = false;

src/autoscroll.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export class AutoScroll
4444
this.origPath = document.location.pathname;
4545
}
4646

47-
static id = "Autoscroll" as const;
47+
static get id() {
48+
return "Autoscroll";
49+
}
4850

4951
currScrollPos() {
5052
return Math.round(self.scrollY + self.innerHeight);
@@ -68,7 +70,7 @@ export class AutoScroll
6870

6971
hasScrollEL(obj: HTMLElement | Document | Window) {
7072
try {
71-
return !!self["getEventListeners"](obj).scroll;
73+
return !!self["getEventListeners"]!(obj).scroll;
7274
} catch (_) {
7375
// unknown, assume has listeners
7476
this.debug("getEventListeners() not available");

src/index.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ type BehaviorClass =
6060
| typeof AutoScroll
6161
| typeof Autoplay
6262
| typeof AutoFetcher
63+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6364
| typeof BehaviorRunner<any, any>;
6465

6566
type BehaviorInstance = InstanceType<BehaviorClass>;
6667

6768
export class BehaviorManager {
6869
autofetch?: AutoFetcher;
69-
behaviors: BehaviorInstance[] | null;
70+
behaviors: BehaviorInstance[];
7071
loadedBehaviors: Record<string, BehaviorClass>;
7172
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7273
mainBehavior: BehaviorInstance | BehaviorRunner<any, any> | null;
@@ -139,17 +140,17 @@ export class BehaviorManager {
139140

140141
if (opts.autofetch) {
141142
void behaviorLog("Using AutoFetcher");
142-
this.behaviors!.push(this.autofetch);
143+
this.behaviors.push(this.autofetch);
143144
}
144145

145146
if (opts.autoplay) {
146147
void behaviorLog("Using Autoplay");
147-
this.behaviors!.push(new Autoplay(this.autofetch, !!opts.startEarly));
148+
this.behaviors.push(new Autoplay(this.autofetch, !!opts.startEarly));
148149
}
149150

150151
if (opts.autoclick) {
151152
void behaviorLog("Using AutoClick");
152-
this.behaviors!.push(
153+
this.behaviors.push(
153154
new AutoClick(opts.clickSelector || DEFAULT_CLICK_SELECTOR),
154155
);
155156
}
@@ -160,6 +161,7 @@ export class BehaviorManager {
160161
this.load(behaviorClass);
161162
} catch (e) {
162163
void behaviorLog(
164+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
163165
`Failed to load custom behavior: ${e} ${behaviorClass}`,
164166
);
165167
}
@@ -186,6 +188,7 @@ export class BehaviorManager {
186188
: {};
187189
try {
188190
this.mainBehavior = new BehaviorRunner(
191+
// @ts-expect-error TODO figure out types here
189192
siteBehaviorClass,
190193
siteSpecificOpts,
191194
);
@@ -208,7 +211,7 @@ export class BehaviorManager {
208211
}
209212

210213
if (this.mainBehavior) {
211-
this.behaviors!.push(this.mainBehavior);
214+
this.behaviors.push(this.mainBehavior);
212215

213216
if (this.mainBehavior instanceof BehaviorRunner) {
214217
return this.mainBehavior.behaviorProps.id;
@@ -221,6 +224,7 @@ export class BehaviorManager {
221224
load(behaviorClass: unknown) {
222225
if (typeof behaviorClass !== "function") {
223226
void behaviorLog(
227+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
224228
`Must pass a class object, got ${behaviorClass}`,
225229
"error",
226230
);
@@ -279,7 +283,7 @@ export class BehaviorManager {
279283
if (
280284
this.mainBehavior &&
281285
"awaitPageLoad" in this.mainBehavior &&
282-
(this.mainBehavior as AbstractBehavior<any, any>).awaitPageLoad
286+
(this.mainBehavior as AbstractBehavior<unknown, unknown>).awaitPageLoad
283287
) {
284288
void behaviorLog("Waiting for custom page load via behavior");
285289
// @ts-expect-error TODO why isn't `log` passed in here? It seems like functions expect it to be
@@ -304,18 +308,21 @@ export class BehaviorManager {
304308

305309
await awaitLoad();
306310

307-
this.behaviors!.forEach((x) => {
308-
const id = x.id || x.constructor.id || "(Unnamed)";
309-
behaviorLog("Starting behavior: " + id, "debug");
310-
x.start();
311+
this.behaviors.forEach((x) => {
312+
const id =
313+
(x as unknown as typeof AbstractBehavior<unknown>).id ||
314+
(x.constructor as unknown as typeof AbstractBehavior<unknown>).id ||
315+
"(Unnamed)";
316+
void behaviorLog("Starting behavior: " + id, "debug");
317+
"start" in x && void x.start();
311318
});
312319

313320
this.started = true;
314321

315322
await sleep(500);
316323

317324
const allBehaviors = Promise.allSettled(
318-
this.behaviors.map((x) => x.done()),
325+
this.behaviors.map(async (x) => "done" in x && x.done()),
319326
);
320327

321328
if (this.timeout) {
@@ -325,44 +332,48 @@ export class BehaviorManager {
325332
);
326333
await Promise.race([allBehaviors, sleep(this.timeout)]);
327334
} else {
328-
behaviorLog("Waiting for behaviors to finish", "debug");
335+
void behaviorLog("Waiting for behaviors to finish", "debug");
329336
await allBehaviors;
330337
}
331338

332-
behaviorLog("All Behaviors Done for " + self.location.href, "debug");
339+
void behaviorLog("All Behaviors Done for " + self.location.href, "debug");
333340

334-
if (this.mainBehavior && this.mainBehaviorClass.cleanup) {
341+
if (this.mainBehavior && "cleanup" in this.mainBehavior) {
335342
this.mainBehavior.cleanup();
336343
}
337344
}
338345

339-
async runOne(name, behaviorOpts = {}) {
346+
async runOne(name: string, behaviorOpts = {}) {
340347
const siteBehaviorClass = siteBehaviors.find((b) => b.name === name);
341348
if (typeof siteBehaviorClass === "undefined") {
342349
console.error(`No behavior of name ${name} found`);
343350
return;
344351
}
345352
//const behavior = new siteBehaviorClass(behaviorOpts);
346-
const behavior = new BehaviorRunner(siteBehaviorClass, behaviorOpts);
353+
const behavior = new BehaviorRunner(
354+
// @ts-expect-error TODO figure out types here
355+
siteBehaviorClass,
356+
behaviorOpts,
357+
);
347358
behavior.start();
348359
console.log(`Running behavior: ${name}`);
349360
await behavior.done();
350361
console.log(`Behavior ${name} completed`);
351362
}
352363

353364
pause() {
354-
behaviorLog("Pausing Main Behavior" + this.mainBehaviorClass.name);
355-
this.behaviors.forEach((x) => x.pause());
365+
void behaviorLog("Pausing Main Behavior" + this.mainBehaviorClass.name);
366+
this.behaviors.forEach((x) => "pause" in x && x.pause());
356367
}
357368

358369
unpause() {
359370
// behaviorLog("Unpausing Main Behavior: " + this.mainBehaviorClass.name);
360-
this.behaviors.forEach((x) => x.unpause());
371+
this.behaviors.forEach((x) => "pause" in x && x.unpause());
361372
}
362373

363-
doAsyncFetch(url) {
364-
behaviorLog("Queueing Async Fetch Url: " + url);
365-
return this.autofetch.queueUrl(url, true);
374+
doAsyncFetch(url: string) {
375+
void behaviorLog("Queueing Async Fetch Url: " + url);
376+
return this.autofetch!.queueUrl(url, true);
366377
}
367378

368379
isInTopFrame() {
@@ -391,9 +402,10 @@ export class BehaviorManager {
391402

392403
const urls = new Set<string>();
393404

394-
document.querySelectorAll(selector).forEach((elem: any) => {
405+
document.querySelectorAll(selector).forEach((elem) => {
395406
// first, try property, unless attrOnly is set
396-
let value = !attrOnly ? elem[extractName] : null;
407+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
408+
let value = !attrOnly ? (elem as any)[extractName] : null;
397409
if (!value) {
398410
value = elem.getAttribute(extractName);
399411
}

src/lib/behavior.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export class BackgroundBehavior {
1717
}
1818

1919
// ===========================================================================
20-
export class Behavior extends BackgroundBehavior {
20+
export class Behavior<State> extends BackgroundBehavior {
2121
_running: Promise<void> | null;
22-
paused: any;
23-
_unpause: any;
24-
state: any;
22+
paused: Promise<void> | null;
23+
_unpause: (() => void) | null;
24+
state: Partial<State>;
2525
scrollOpts: {
2626
behavior: string;
2727
block: string;
@@ -77,12 +77,15 @@ export class Behavior extends BackgroundBehavior {
7777
}
7878
}
7979

80-
getState(msg: string, incrValue?: string) {
80+
getState<IncrKey extends Lib.NumberKeys<State>>(
81+
msg: string,
82+
incrValue?: IncrKey,
83+
) {
8184
if (incrValue) {
8285
if (this.state[incrValue] === undefined) {
83-
this.state[incrValue] = 1;
86+
(this.state[incrValue] as number) = 1;
8487
} else {
85-
this.state[incrValue]++;
88+
(this.state[incrValue] as number)++;
8689
}
8790
}
8891

@@ -91,7 +94,7 @@ export class Behavior extends BackgroundBehavior {
9194

9295
cleanup() {}
9396

94-
async awaitPageLoad(_: any) {
97+
async awaitPageLoad() {
9598
// wait for initial page load here
9699
}
97100

@@ -119,13 +122,19 @@ export type Context<State, Opts = EmptyObject> = {
119122
Lib: typeof Lib;
120123
state: State;
121124
opts: Opts;
122-
log: (data: any, type?: string) => Promise<void>;
125+
log: (data: unknown, type?: string) => Promise<void>;
123126
};
124127

125128
export abstract class AbstractBehavior<State, Opts = EmptyObject> {
126129
static readonly id: string;
127130
static isMatch: () => boolean;
128-
static init: () => any;
131+
static init: () => {
132+
// TODO: type these
133+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
134+
state?: any;
135+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
136+
opts?: any;
137+
};
129138

130139
abstract run: (
131140
ctx: Context<State, Opts>,
@@ -154,9 +163,9 @@ export class BehaviorRunner<State, Opts>
154163
inst: AbstractBehavior<State, Opts>;
155164
behaviorProps: ConcreteBehaviorConstructor<State, Opts>;
156165
ctx: Context<State, Opts>;
157-
_running: any;
158-
paused: any;
159-
_unpause: any;
166+
_running: Promise<void> | null;
167+
paused: Promise<void> | (() => Promise<void>) | null;
168+
_unpause: ((value: void | PromiseLike<void>) => void) | null;
160169

161170
get id() {
162171
return (this.inst.constructor as ConcreteBehaviorConstructor<State, Opts>)
@@ -209,7 +218,7 @@ export class BehaviorRunner<State, Opts>
209218
this._running = this.run();
210219
}
211220

212-
done() {
221+
async done() {
213222
return this._running ? this._running : Promise.resolve();
214223
}
215224

src/lib/global.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ interface BehaviorGlobals {
1010
idleTime: number;
1111
concurrency: number;
1212
}) => Promise<void>;
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1314
__bx_initFlow?: (params: any) => Promise<number>;
15+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1416
__bx_nextFlowStep?: (id: number) => Promise<any>;
1517
__bx_contentCheckFailed?: (reason: string) => void;
1618
__bx_open?: (params: { url: string | URL }) => Promise<void>;
@@ -31,7 +33,7 @@ declare global {
3133
* Chrome DevTools’s `getEventListeners` API
3234
* @see https://developer.chrome.com/docs/devtools/console/utilities/#getEventListeners-function
3335
*/
34-
getEventListeners: <Obj>(
36+
getEventListeners?: <Obj>(
3537
obj: Obj,
3638
) => Record<
3739
Obj extends Window ? keyof WindowEventMap : string,

src/lib/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { type BehaviorManager } from "..";
22
import { type Context } from "./behavior";
33

4-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5-
let _logFunc: ((...data: any[]) => void) | null = console.log;
4+
let _logFunc: ((...data: unknown[]) => void) | null = console.log;
65
let _behaviorMgrClass: typeof BehaviorManager | null = null;
76

87
const scrollOpts: ScrollIntoViewOptions = {
@@ -173,6 +172,7 @@ export async function waitForNetworkIdle(idleTime = 500, concurrency = 0) {
173172
}
174173
}
175174

175+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
176176
export async function initFlow(params: any): Promise<number> {
177177
if (typeof self["__bx_initFlow"] === "function") {
178178
return await callBinding(self["__bx_initFlow"], params);
@@ -181,6 +181,7 @@ export async function initFlow(params: any): Promise<number> {
181181
return -1;
182182
}
183183

184+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
184185
export async function nextFlowStep(id: number): Promise<any> {
185186
if (typeof self["__bx_nextFlowStep"] === "function") {
186187
return await callBinding(self["__bx_nextFlowStep"], id);
@@ -228,7 +229,7 @@ export async function openWindow(
228229
export function _setLogFunc(
229230
func: ((message: string, level: string) => void) | null,
230231
) {
231-
_logFunc = func;
232+
_logFunc = func as (...data: unknown[]) => void;
232233
}
233234

234235
export function _setBehaviorManager(cls: typeof BehaviorManager) {

src/site/youtube.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AutoScroll } from "../autoscroll";
22
import { type Context } from "../lib/behavior";
33

44
export class YoutubeBehavior extends AutoScroll {
5-
static id = "Youtube" as const;
5+
static override id = "Youtube" as const;
66
async awaitPageLoad(ctx: Context<{}, {}>) {
77
const { sleep, assertContentValid } = ctx.Lib;
88
await sleep(10);

0 commit comments

Comments
 (0)