Add enabling/disabling content script
This commit is contained in:
parent
d924bd1dfb
commit
c51ecd14cf
4 changed files with 23 additions and 8 deletions
|
@ -12,6 +12,10 @@ const getTab = async (s: Runtime.MessageSender): Promise<Tabs.Tab> => {
|
|||
|
||||
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 (
|
||||
|
|
|
@ -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));
|
||||
})();
|
||||
|
|
|
@ -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" });
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue