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

Commit

Permalink
add separate event that gets fired when not on transactions page and …
Browse files Browse the repository at this point in the history
…test it (#9)
  • Loading branch information
alexkolson committed Nov 25, 2021
1 parent f9c9aac commit c2fe6f7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions extension/advanzia-assistant.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import './advanzia-assistant';
import { Script } from './advanzia-assistant';

describe('#ContentScript', () => {
it('should do something', async () => {
describe('Content Script', () => {
it('should not do anything if not on transactions page', async () => {
await new Promise((resolve) => {
new Script()
.on('done', () => resolve(void 0))
.on('noop', () => resolve(void 0))
.execute();
});
});
Expand Down
8 changes: 7 additions & 1 deletion extension/advanzia-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface ContentScript {

export enum ContentScriptStatus {
Initializing = "Initializing",
Noop = "Noop",
Ready = "Ready",
Done = "Done",
Err = "Error"
Expand All @@ -15,6 +16,7 @@ export interface WasmExports extends WebAssembly.Exports {
};

export interface ContentScriptEvents {
readonly noop: Event;
readonly ready: Event;
readonly done: Event;
readonly error: ErrorEvent;
Expand All @@ -37,6 +39,7 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
this.status = ContentScriptStatus.Initializing;
this.memory = new WebAssembly.Memory({ initial: 10 });
this.events = {
noop: new Event('noop'),
ready: new Event('ready'),
done: new Event('done'),
error: new ErrorEvent('error')
Expand All @@ -60,6 +63,9 @@ export class Script extends EventTarget implements ContentScript, EventListenerO
case this.events.ready.type:
this.status = ContentScriptStatus.Ready;
break;
case this.events.noop.type:
this.status = ContentScriptStatus.Noop;
break;
case this.events.done.type:
this.status = ContentScriptStatus.Done;
break;
Expand All @@ -76,7 +82,7 @@ export class Script extends EventTarget implements ContentScript, EventListenerO

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

Expand Down
1 change: 1 addition & 0 deletions extension/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Script } from './advanzia-assistant';
const script = new Script();

script
.on('noop', () => console.log('noop'))
.on('done', () => console.log('done'))
.on('ready', () => console.log('ready'))
.on('error', (e) => console.log({ errors: script.errors, status: script.status }))
Expand Down

0 comments on commit c2fe6f7

Please sign in to comment.