This commit is contained in:
a 2020-08-18 21:53:38 +02:00
parent c5fc07454e
commit 5b4743768e
6 changed files with 130 additions and 1 deletions

1
.eslintignore Normal file
View file

@ -0,0 +1 @@
*.svelte

15
.eslintrc.js Normal file
View file

@ -0,0 +1,15 @@
module.exports = {
root: true,
env: { webextensions: true },
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
],
rules: {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
},
};

View file

@ -50,6 +50,6 @@
} }
}, },
"lint-staged": { "lint-staged": {
"*/**": "prettier --write" "*/**": ["eslint --fix ","prettier --write"]
} }
} }

View file

@ -0,0 +1,76 @@
import { browser, Runtime } from "webextension-polyfill-ts";
import { Communicator, commandList, commandFunction } from "../communicator";
import { newPromise } from "../utils";
interface BackgroundCommands extends commandList {
translationFinished: TranslationMessage;
languageList: LanguageListMessage;
}
interface ContentScriptCommands extends commandList {
translationRequest: TranslationRequest;
}
interface TranslationMessage extends commandFunction {
args: Translation;
}
interface LanguageListMessage extends commandFunction {
args: Array<Language>;
}
interface TranslationRequest extends commandFunction {
args: string;
}
export class BackgroundMessenger extends Communicator<
BackgroundCommands,
ContentScriptCommands
> {
conn: Promise<Runtime.Port>;
private resolvConn: (p: Runtime.Port) => void;
constructor() {
super();
[this.conn, this.resolvConn] = newPromise<Runtime.Port>();
browser.runtime.onConnect.addListener((p) => {
if (p.name != "gtranslate_scraper_conn") return;
this.resolvConn(p);
p.onDisconnect.addListener(
() => ([this.conn, this.resolvConn] = newPromise<Runtime.Port>())
);
p.onMessage.addListener((m) => this.onMessage(m, p.sender!));
});
}
async runCommand<K extends keyof ContentScriptCommands>(
command: K,
args: ContentScriptCommands[K]["args"]
) {
const msg = this.getCommandMessage(command, args);
(await this.conn).postMessage(msg);
}
}
export class ContentScriptMessenger extends Communicator<
ContentScriptCommands,
BackgroundCommands
> {
conn: Runtime.Port;
constructor() {
super();
this.conn = browser.runtime.connect({
name: "gtranslate_scraper_conn",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any); //The declaration file is wrong
this.conn.onMessage.addListener((m) => this.onMessage(m));
}
async runCommand<K extends keyof BackgroundCommands>(
command: K,
args: BackgroundCommands[K]["args"]
) {
const msg = this.getCommandMessage(command, args);
return this.conn.postMessage(msg);
}
}

4
src/file.d.ts vendored Normal file
View file

@ -0,0 +1,4 @@
declare module "*?file" {
const file: string;
export = file;
}

33
src/types.ts Normal file
View file

@ -0,0 +1,33 @@
import type { Flashcards } from "./background/database";
declare global {
interface Translation {
src: string;
result: string;
languages: LanguagePair;
}
interface Flashcard {
id?: IDBValidKey;
src: string;
result: string;
dateAdded: Date;
exported: boolean;
languages: LanguagePair;
}
interface LanguagePair {
srcLang: Language;
dstLang: Language;
}
interface Language {
code: string;
name: string;
}
interface Window {
hasRun?: boolean;
flashcardDB: Flashcards;
}
}