This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
730f1ac
commit 864966a
Showing
10 changed files
with
3,508 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"eslint.workingDirectories": ["extension"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
dist | ||
transpiled | ||
.eslintrc.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": ["airbnb-base", "airbnb-typescript/base"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,134 @@ | ||
export interface ContentScript { | ||
execute(): void; | ||
}; | ||
execute(): void; | ||
} | ||
|
||
export enum ContentScriptStatus { | ||
Initializing = "Initializing", | ||
Noop = "Noop", | ||
Ready = "Ready", | ||
Done = "Done", | ||
Err = "Error" | ||
}; | ||
Initializing = 'Initializing', | ||
Noop = 'Noop', | ||
Ready = 'Ready', | ||
Done = 'Done', | ||
Err = 'Error', | ||
} | ||
|
||
export interface WasmExports extends WebAssembly.Exports { | ||
add_one: (num: number) => number, | ||
}; | ||
add_one: (num: number) => number, | ||
} | ||
|
||
export interface ContentScriptEvents { | ||
readonly noop: Event; | ||
readonly ready: Event; | ||
readonly done: Event; | ||
readonly error: ErrorEvent; | ||
}; | ||
readonly noop: Event; | ||
readonly ready: Event; | ||
readonly done: Event; | ||
readonly error: ErrorEvent; | ||
} | ||
|
||
export class Script extends EventTarget implements ContentScript, EventListenerObject { | ||
private memory: WebAssembly.Memory; | ||
private wasmExports?: WasmExports; | ||
|
||
|
||
private events: ContentScriptEvents; | ||
|
||
status: ContentScriptStatus; | ||
|
||
errors: Error[]; | ||
|
||
constructor() { | ||
super(); | ||
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') | ||
}; | ||
|
||
this.errors = []; | ||
|
||
this.registerEventListeners(); | ||
} | ||
|
||
private registerEventListeners() { | ||
Object.values(this.events).forEach(e => this.registerEventListener(e.type)); | ||
private memory: WebAssembly.Memory; | ||
|
||
private wasmExports?: WasmExports; | ||
|
||
private events: ContentScriptEvents; | ||
|
||
status: ContentScriptStatus; | ||
|
||
errors: Error[]; | ||
|
||
constructor() { | ||
super(); | ||
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'), | ||
}; | ||
|
||
this.errors = []; | ||
|
||
this.registerEventListeners(); | ||
} | ||
|
||
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.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; | ||
case this.events.error.type: | ||
this.status = ContentScriptStatus.Err; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private registerEventListener(name: string) { | ||
this.addEventListener(name, this); | ||
} | ||
on(eventName: string, handler: EventListenerOrEventListenerObject) { | ||
this.addEventListener(eventName, handler); | ||
return this; | ||
} | ||
|
||
handleEvent(e: Event | ErrorEvent) { | ||
switch (e.type) { | ||
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; | ||
case this.events.error.type: | ||
this.status = ContentScriptStatus.Err; | ||
break; | ||
} | ||
async execute() { | ||
if (window.location.host !== 'mein.gebuhrenfrei.com') { | ||
this.error(new Error('this script can only be run on mein.gebuhrenfrei.com')); | ||
return; | ||
} | ||
|
||
on(eventName: string, handler: EventListenerOrEventListenerObject) { | ||
this.addEventListener(eventName, handler); | ||
return this; | ||
if (window.location.pathname.indexOf('retail-app') === -1) { | ||
this.dispatchEvent(this.events.noop); | ||
return; | ||
} | ||
|
||
async execute() { | ||
if (location.host !== 'mein.gebuhrenfrei.com') { | ||
this.error(new Error('this script can only be run on mein.gebuhrenfrei.com')); | ||
return; | ||
} | ||
const wasmPath = chrome.runtime.getURL('advanzia-assistant.wasm'); | ||
const wasmResponse = await fetch(wasmPath); | ||
|
||
if (location.pathname.indexOf('retail-app') === -1) { | ||
this.dispatchEvent(this.events.noop); | ||
return; | ||
} | ||
const wasmResponseArrayBuffer = await wasmResponse.arrayBuffer(); | ||
|
||
const wasmPath = chrome.runtime.getURL('advanzia-assistant.wasm'); | ||
const wasmResponse = await fetch(wasmPath); | ||
const wasmImports = { env: { memory: this.memory } }; | ||
|
||
const wasmResponseArrayBuffer = await wasmResponse.arrayBuffer(); | ||
const wasm = await WebAssembly.instantiate(wasmResponseArrayBuffer, wasmImports); | ||
this.wasmExports = wasm.instance.exports as WasmExports; | ||
|
||
const wasm = await WebAssembly.instantiate(wasmResponseArrayBuffer, { env: { memory: this.memory } }); | ||
this.wasmExports = wasm.instance.exports as WasmExports; | ||
await Script.pageReasonablyLoaded(); | ||
|
||
await this.pageReasonablyLoaded(); | ||
this.dispatchEvent(this.events.ready); | ||
} | ||
|
||
this.dispatchEvent(this.events.ready); | ||
} | ||
private error(e: Error) { | ||
this.errors.push(e); | ||
this.dispatchEvent(this.events.error); | ||
} | ||
|
||
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 = document.querySelector(signalSelector); | ||
if (el) { | ||
resolve(); | ||
return; | ||
} | ||
new MutationObserver((_mutationsList, observer) => { | ||
if (document.querySelector(signalSelector)) { | ||
resolve(); | ||
observer.disconnect(); | ||
} | ||
}) | ||
.observe(document.documentElement, { | ||
childList: true, | ||
subtree: true | ||
}); | ||
private static pageReasonablyLoaded(): Promise<void> { | ||
const signalSelector = '.card'; | ||
return new Promise((resolve) => { | ||
const el = document.querySelector(signalSelector); | ||
if (el) { | ||
resolve(); | ||
return; | ||
} | ||
new MutationObserver((_mutationsList, observer) => { | ||
if (document.querySelector(signalSelector)) { | ||
resolve(); | ||
observer.disconnect(); | ||
} | ||
}) | ||
.observe(document.documentElement, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
} | ||
}; | ||
}); | ||
} | ||
} |
Oops, something went wrong.