From 412317e06ab6468fc0c79e56ab26733cd5588e04 Mon Sep 17 00:00:00 2001 From: a Date: Thu, 23 Jul 2020 17:52:45 +0200 Subject: [PATCH] Add commands --- src/content_script.ts | 9 ++++++++- src/popup/popup.ts | 9 ++++++--- src/types.d.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/types.d.ts diff --git a/src/content_script.ts b/src/content_script.ts index 93e81c0..6da726f 100644 --- a/src/content_script.ts +++ b/src/content_script.ts @@ -10,5 +10,12 @@ interface WindowHack { return; } (window as WindowHack)[name] = true; - console.log("loaded content_script"); + + let ON = false; + browser.runtime.onMessage.addListener((c: command) => { + switch (c.command) { + case commands.toggle: + ON = c.value; + } + }); })(); diff --git a/src/popup/popup.ts b/src/popup/popup.ts index 2254b6b..b297bf0 100644 --- a/src/popup/popup.ts +++ b/src/popup/popup.ts @@ -21,8 +21,11 @@ customElements.define("extension-toggle", ExtensionToggle, { }); const toggle = document.querySelector("button[is=extension-toggle]"); -toggle.addEventListener("extensionToggled", (e: CustomEvent) => - console.log(`aaa: ${e.detail}`) -); +toggle.addEventListener("extensionToggled", async (e: CustomEvent) => { + let tab = await browser.tabs.query({ active: true, currentWindow: true }); + let com = { command: commands.toggle, value: e.detail }; + browser.tabs.sendMessage(tab[0].id, com); +}); +//Runs in the current tab browser.tabs.executeScript({ file: "/content_script.bundle.js" }); diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..aa2bd21 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,8 @@ +declare const enum commands { + toggle = 0, +} +interface ToggleCommand { + command: commands.toggle; + value: boolean; +} +type command = ToggleCommand;