From 2169ea9728c258aebf78701bf9ac796ed5689536 Mon Sep 17 00:00:00 2001 From: Dan Masquelier Date: Sat, 12 Aug 2023 22:35:43 -0700 Subject: [PATCH] add "onHighlight" experimental setting that plays immediately on highlight --- background.js | 1 + morsefire.js | 55 +++++++++++++++++++++++++++++++++++++-------------- options.css | 13 +++++++++--- options.html | 6 ++++++ options.js | 18 ++++++++++++++--- 5 files changed, 72 insertions(+), 21 deletions(-) diff --git a/background.js b/background.js index ad2ed74..dfe194e 100644 --- a/background.js +++ b/background.js @@ -6,6 +6,7 @@ const defaultSettings = { eff: 15, ews: 0.1, playvolume: 0.1, + onHighlight: false, }; function saveSettings(settings) { diff --git a/morsefire.js b/morsefire.js index bb3f591..f71a3e4 100644 --- a/morsefire.js +++ b/morsefire.js @@ -5,10 +5,31 @@ const containerCloseId = "morsefire_close"; const playerId = "morsefire_player"; let cw; +let cwSettings; browser.runtime .sendMessage({ action: "morsefire_settings_get" }) .then((settings) => { - cw = new jscw(settings); + cwSettings = settings; + cw = new jscw(cwSettings); + + if (cwSettings.onHighlight) { + document.addEventListener("mouseup", (e) => { + if (e.target.closest(`#${containerId}`)) { + return; + } + const selectedText = window.getSelection().toString().trim(); + if (selectedText) { + handleText(selectedText); + } + }); + + document.addEventListener("mousedown", (e) => { + if (e.target.closest(`#${containerId}`)) { + return; + } + tryStop(); + }); + } }); function getEl(id) { @@ -21,6 +42,14 @@ function setStyle(el, style) { .join(" "); } +function tryStop() { + try { + cw.stop(); + } catch (err) { + // nothing to stop + } +} + function create() { // the overall container const container = document.createElement("div"); @@ -35,11 +64,7 @@ function create() { const close = document.createElement("div"); close.id = containerCloseId; close.addEventListener("click", () => { - try { - cw.stop(); - } catch (err) { - // nothing to stop - } + tryStop(); container.remove(); }); close.textContent = "x"; @@ -88,17 +113,17 @@ function create() { return container; } +function handleText(text) { + tryStop(); + getEl() ?? create(); + cw.setText(text); + cw.renderPlayer(playerId, cw); + cw.play(); +} + browser.runtime.onMessage.addListener((message) => { if (message.action !== containerId) { return; } - try { - cw.stop(); // stop any existing plays - } catch (err) { - // nothing to stop - } - getEl() ?? create(); - cw.setText(message.text); - cw.renderPlayer(playerId, cw); - cw.play(); + handleText(message.text); }); diff --git a/options.css b/options.css index 31930f0..df0708f 100644 --- a/options.css +++ b/options.css @@ -1,12 +1,19 @@ body { padding: 10px; - max-width: 100px; + max-width: 200px; +} +label { + margin-bottom: 4px; } input { - padding: 4px 8px; + padding: 8px; margin-bottom: 8px; + border: 1px solid #555; + border-radius: 4px; +} +summary { + padding: 4px; } - button { margin-top: 8px; padding: 8px; diff --git a/options.html b/options.html index 426fc0e..47aff72 100644 --- a/options.html +++ b/options.html @@ -20,6 +20,12 @@ +
+ Experimental + + +
+ diff --git a/options.js b/options.js index 1066c6c..d5891ca 100644 --- a/options.js +++ b/options.js @@ -1,4 +1,4 @@ -const fields = ["wpm", "freq", "eff", "ews", "playvolume"]; +const fields = ["wpm", "freq", "eff", "ews", "playvolume", "onHighlight"]; document.addEventListener("DOMContentLoaded", () => { // Load current settings and populate the form @@ -6,7 +6,13 @@ document.addEventListener("DOMContentLoaded", () => { .sendMessage({ action: "morsefire_settings_get" }) .then((settings) => { fields.forEach((field) => { - document.getElementById(field).value = settings[field]; + switch (field) { + case "onHighlight": + document.getElementById(field).checked = settings[field]; + break; + default: + document.getElementById(field).value = settings[field]; + } }); }); @@ -14,7 +20,13 @@ document.addEventListener("DOMContentLoaded", () => { document.getElementById("save").addEventListener("click", () => { const settings = {}; fields.forEach((field) => { - settings[field] = parseInt(document.getElementById(field).value, 10); + switch (field) { + case "onHighlight": + settings[field] = document.getElementById(field).checked; + break; + default: + settings[field] = parseInt(document.getElementById(field).value, 10); + } }); // Repeat for other settings