Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
dependency injection is a bit overkill
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkolson committed Nov 25, 2021
1 parent 41a5525 commit b5674d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 60 deletions.
90 changes: 39 additions & 51 deletions extension/advanzia-assistant.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
export interface ContentScriptDependencies {
readonly chrome: typeof chrome;
readonly console: typeof console;
readonly document: Document,
readonly fetch: typeof fetch;
readonly location: typeof window.location;
readonly MutationObserver: typeof MutationObserver;
readonly wasm: typeof WebAssembly;
};

export interface ContentScript {
init(): void;
execute(): void;
Expand All @@ -34,111 +24,109 @@ export interface ContentScriptEvents {


export class Script extends EventTarget implements ContentScript, EventListenerObject {
private deps: ContentScriptDependencies;
private memory: WebAssembly.Memory;
private wasmExports?: WasmExports;

private events: ContentScriptEvents;

status: ContentScriptStatus;

errors: Error[];
constructor(deps: ContentScriptDependencies) {

constructor() {
super();
this.deps = deps;
this.status = ContentScriptStatus.Initializing;
this.memory = new this.deps.wasm.Memory({ initial: 10 });
this.memory = new WebAssembly.Memory({ initial: 10 });
this.events = {
instantiateWasm: new Event('instantiatedWasm'),
ready: new Event('ready'),
done: new Event('done'),
error: new ErrorEvent('error')
};

this.errors = [];

this.registerEventListeners();
if (this.deps.location.pathname.indexOf('retail-app') === -1) {

if (location.pathname.indexOf('retail-app') === -1) {
this.dispatchEvent(this.events.done);
return;
}

this.dispatchEvent(this.events.instantiateWasm);
}

private registerEventListeners() {
Object.values(this.events).forEach(e => this.registerEventListener(e.type));
}

private registerEventListener(name: string) {
this.addEventListener(name, this);
}

handleEvent(e: Event | ErrorEvent) {
switch (e.type) {
case this.events.instantiateWasm.type:
this.init();
break;
this.init();
break;
case this.events.ready.type:
this.status = ContentScriptStatus.Ready;
break;
this.status = ContentScriptStatus.Ready;
break;
case this.events.done.type:
this.status = ContentScriptStatus.Done;
break;
this.status = ContentScriptStatus.Done;
break;
case this.events.error.type:
this.status = ContentScriptStatus.Err;
break;
this.status = ContentScriptStatus.Err;
break;
}
}

on(eventName: string, handler: EventListenerOrEventListenerObject) {
this.addEventListener(eventName, handler);
return this;
}

async init() {
const wasmResponse = await this.deps.fetch(this.deps.chrome.runtime.getURL('advanzia-assistant.wasm'));
const wasm = await this.deps.wasm.instantiateStreaming(wasmResponse, { env: { memory: this.memory } });
const wasmResponse = await fetch(chrome.runtime.getURL('advanzia-assistant.wasm'));
const wasm = await WebAssembly.instantiateStreaming(wasmResponse, { env: { memory: this.memory } });
this.wasmExports = wasm.instance.exports as WasmExports;

await this.pageReasonablyLoaded();

this.dispatchEvent(this.events.ready);
}

async execute() {
if (this.status !== ContentScriptStatus.Ready) {
this.error(new Error(`Script cannot be executed. Script needs to have ready status but status is ${this.status}`));
}

this.dispatchEvent(this.events.done);
}

private error(e: Error) {
this.errors.push(e);
this.dispatchEvent(this.events.error);
}

private pageReasonablyLoaded(): Promise<void> {
const signalSelector = '.card';
return new Promise((resolve) => {
const el = this.deps.document.querySelector(signalSelector);
const el = document.querySelector(signalSelector);
if (el) {
resolve();
return;
}
new this.deps.MutationObserver((_mutationsList, observer) => {
if (this.deps.document.querySelector(signalSelector)) {
new MutationObserver((_mutationsList, observer) => {
if (document.querySelector(signalSelector)) {
resolve();
observer.disconnect();
}
})
.observe(this.deps.document.documentElement, {
childList: true,
subtree: true
});
.observe(document.documentElement, {
childList: true,
subtree: true
});
});
}
};
10 changes: 1 addition & 9 deletions extension/content-script.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Script } from './advanzia-assistant';

const script = new Script({
chrome,
console,
document,
fetch: window.fetch.bind(window),
location,
MutationObserver,
wasm: WebAssembly,
});
const script = new Script();

script
.on('ready', () => script.execute())
Expand Down

0 comments on commit b5674d8

Please sign in to comment.