Add enabling/disabling content script

This commit is contained in:
a 2020-07-30 11:55:00 +02:00
parent d924bd1dfb
commit c51ecd14cf
4 changed files with 23 additions and 8 deletions

View file

@ -12,6 +12,10 @@ const getTab = async (s: Runtime.MessageSender): Promise<Tabs.Tab> => {
const setEnabledCommand = async (v: Boolean, s: Runtime.MessageSender) => { const setEnabledCommand = async (v: Boolean, s: Runtime.MessageSender) => {
const tab = await getTab(s); const tab = await getTab(s);
browser.tabs.sendMessage(tab.id, {
commandKind: commands.setEnabled,
value: v,
});
await browser.sessions.setTabValue(tab.id, "lingoEnabled", v); await browser.sessions.setTabValue(tab.id, "lingoEnabled", v);
}; };
const getEnabledCommand = async ( const getEnabledCommand = async (

View file

@ -1,4 +1,5 @@
import { browser } from "webextension-polyfill-ts"; import { browser } from "webextension-polyfill-ts";
import { isEnabledOnPage } from "./utils";
declare global { declare global {
interface Window { interface Window {
@ -9,17 +10,25 @@ declare global {
//Make sure our script only runs once //Make sure our script only runs once
if (window.hasRun) return; if (window.hasRun) return;
window.hasRun = true; window.hasRun = true;
const setEnabled = (v: boolean) => {
document.removeEventListener("selectionchange", onSelectionChange);
if (v) {
document.addEventListener("selectionchange", onSelectionChange);
}
};
browser.runtime.onMessage.addListener((c: command) => { browser.runtime.onMessage.addListener((c: command) => {
switch (c.commandKind) { switch (c.commandKind) {
case commands.setEnabled: case commands.setEnabled:
document.removeEventListener("selectionchange", onSelectionChange); setEnabled(c.value);
if (c.value) { break;
document.addEventListener("selectionchange", onSelectionChange);
}
} }
}); });
const onSelectionChange = () => { const onSelectionChange = () => {
console.log(document.getSelection().toString()); console.log(document.getSelection().toString());
}; };
isEnabledOnPage().then((v) => setEnabled(v));
})(); })();

View file

@ -1,4 +1,5 @@
import { browser } from "webextension-polyfill-ts"; import { browser } from "webextension-polyfill-ts";
import { isEnabledOnPage } from "../utils";
import "./popup.scss"; import "./popup.scss";
class ExtensionToggle extends HTMLButtonElement { class ExtensionToggle extends HTMLButtonElement {
isON: boolean = false; isON: boolean = false;
@ -49,9 +50,8 @@ browser.runtime.onMessage.addListener((c: command) => {
break; break;
} }
}); });
browser.runtime
.sendMessage({ commandKind: commands.getEnabled }) isEnabledOnPage().then((resp) => (toggle.on = resp));
.then((resp) => (toggle.on = resp));
//Runs in the current tab //Runs in the current tab
browser.tabs.executeScript({ file: "/content_script.bundle.js" }); browser.tabs.executeScript({ file: "/content_script.bundle.js" });

View file

@ -17,6 +17,9 @@ let options = {
path: path.resolve(__dirname, "dist"), path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js", filename: "[name].bundle.js",
}, },
resolve: {
extensions: [".ts", ".js", ".json"],
},
mode: mode, mode: mode,
devtool: "inline-source-map", devtool: "inline-source-map",
module: { module: {
@ -68,4 +71,3 @@ if (mode !== "development") {
} }
module.exports = options; module.exports = options;