From c51ecd14cfbca101df74426a6f1ee2cf8b3ca1b3 Mon Sep 17 00:00:00 2001 From: a Date: Thu, 30 Jul 2020 11:55:00 +0200 Subject: [PATCH] Add enabling/disabling content script --- src/background.ts | 4 ++++ src/content_script.ts | 17 +++++++++++++---- src/popup/popup.ts | 6 +++--- webpack.config.js | 4 +++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/background.ts b/src/background.ts index 38e514a..5b80753 100644 --- a/src/background.ts +++ b/src/background.ts @@ -12,6 +12,10 @@ const getTab = async (s: Runtime.MessageSender): Promise => { const setEnabledCommand = async (v: Boolean, s: Runtime.MessageSender) => { const tab = await getTab(s); + browser.tabs.sendMessage(tab.id, { + commandKind: commands.setEnabled, + value: v, + }); await browser.sessions.setTabValue(tab.id, "lingoEnabled", v); }; const getEnabledCommand = async ( diff --git a/src/content_script.ts b/src/content_script.ts index 505cfd7..7d1cf98 100644 --- a/src/content_script.ts +++ b/src/content_script.ts @@ -1,4 +1,5 @@ import { browser } from "webextension-polyfill-ts"; +import { isEnabledOnPage } from "./utils"; declare global { interface Window { @@ -9,17 +10,25 @@ declare global { //Make sure our script only runs once if (window.hasRun) return; window.hasRun = true; + + const setEnabled = (v: boolean) => { + document.removeEventListener("selectionchange", onSelectionChange); + if (v) { + document.addEventListener("selectionchange", onSelectionChange); + } + }; + browser.runtime.onMessage.addListener((c: command) => { switch (c.commandKind) { case commands.setEnabled: - document.removeEventListener("selectionchange", onSelectionChange); - if (c.value) { - document.addEventListener("selectionchange", onSelectionChange); - } + setEnabled(c.value); + break; } }); const onSelectionChange = () => { console.log(document.getSelection().toString()); }; + + isEnabledOnPage().then((v) => setEnabled(v)); })(); diff --git a/src/popup/popup.ts b/src/popup/popup.ts index 87be7ba..e45ccef 100644 --- a/src/popup/popup.ts +++ b/src/popup/popup.ts @@ -1,4 +1,5 @@ import { browser } from "webextension-polyfill-ts"; +import { isEnabledOnPage } from "../utils"; import "./popup.scss"; class ExtensionToggle extends HTMLButtonElement { isON: boolean = false; @@ -49,9 +50,8 @@ browser.runtime.onMessage.addListener((c: command) => { break; } }); -browser.runtime - .sendMessage({ commandKind: commands.getEnabled }) - .then((resp) => (toggle.on = resp)); + +isEnabledOnPage().then((resp) => (toggle.on = resp)); //Runs in the current tab browser.tabs.executeScript({ file: "/content_script.bundle.js" }); diff --git a/webpack.config.js b/webpack.config.js index 5f39003..aaacc00 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -17,6 +17,9 @@ let options = { path: path.resolve(__dirname, "dist"), filename: "[name].bundle.js", }, + resolve: { + extensions: [".ts", ".js", ".json"], + }, mode: mode, devtool: "inline-source-map", module: { @@ -68,4 +71,3 @@ if (mode !== "development") { } module.exports = options; -