From d70732d12bb78d7bac35855c6503ad183b190af6 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Sat, 19 Feb 2022 12:34:08 -0600 Subject: [PATCH] Work on toolbar buffer solution --- background.js | 38 +++++++++++++++++++++++++++----------- settings/keybuffer2.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/background.js b/background.js index 7f78c21..292df8f 100644 --- a/background.js +++ b/background.js @@ -16,6 +16,7 @@ along with this program. If not, see . */ +let lastActive = null const defaultHosts = ""; let appCode = function (){ @@ -201,42 +202,57 @@ let appCode = function (){ browser.runtime.onMessage.addListener(request => { - clearTimeout(bufferTimeout) + console.log(request.keys); + if (request.keys == "GetSet"){ + keyBuffer = elementToSendBuffer.value + return Promise.resolve({response: elementToSendBuffer.value}); + } + + clearTimeout(bufferTimeout) + if (request.keys == "Backspace"){ keyBuffer = keyBuffer.slice(0, -1) } + else if(request.keys.length > 4){ + // pass + } else{ keyBuffer += request.keys; } - bufferTimeout = setTimeout(function(){sendBuffer()}, 1000) + if (request.isEmpty){ + keyBuffer = "" + sendBuffer() + } + bufferTimeout = setTimeout(function(){sendBuffer()}, 100) return Promise.resolve({response: "ack"}); }); function sendBuffer(){ if (elementToSendBuffer == null){ - bufferTimeout = setTimeout(function(){sendBuffer()}, 1000) + bufferTimeout = setTimeout(function(){sendBuffer()}, 100) return } console.debug("sending buffer to " + elementToSendBuffer) - - - if (keyBuffer.length > 0){ - elementToSendBuffer.value = keyBuffer - } + elementToSendBuffer.value = keyBuffer } function setSendEl(e){ - if (document.activeElement.tagName != "INPUT"){ + let activeEl = document.activeElement + if (activeEl.tagName != "INPUT" && activeEl.tagName != "TEXTAREA"){ + keyBuffer = "" return } - console.debug("active el is " + document.activeElement.tagName) - elementToSendBuffer = document.activeElement + + console.debug("active el is " + activeEl.tagName) + elementToSendBuffer = activeEl + keyBuffer = activeEl.value } + document.addEventListener('focus', setSendEl) document.addEventListener('click', setSendEl) diff --git a/settings/keybuffer2.js b/settings/keybuffer2.js index c76a495..9e9ad33 100644 --- a/settings/keybuffer2.js +++ b/settings/keybuffer2.js @@ -1,13 +1,18 @@ +let started = false function onError(error) { console.error(`Error: ${error}`); } function doSendMsg(msg, tabs) { + let empty = false; + if (document.getElementById('keyBuffer').value.length == 0){ + empty = true; + } for (let tab of tabs) { browser.tabs.sendMessage( tab.id, - {keys: msg} + {keys: msg, isEmpty: empty} ).then(response => { console.log("Message from the content script:"); console.log(response.response); @@ -15,11 +20,29 @@ function onError(error) { } } +function getCurrent(){ + + browser.tabs.query({ + currentWindow: true + }).then(function(tabs){ + for (let tab of tabs) { + browser.tabs.sendMessage( + tab.id, + {keys: "GetSet", isEmpty: false} + ).then(response => { + console.log("Message from the content script:"); + console.log(response.response); + + document.getElementById('keyBuffer').value = response.response + + }).catch(onError); + } + }).catch(onError); +} + let sender = async function(e){ - - let sendMessageToTabs = function(tabs){ doSendMsg(e.key, tabs) } @@ -29,6 +52,6 @@ let sender = async function(e){ }).then(sendMessageToTabs).catch(onError); } - +setTimeout(function(){getCurrent()}, 10) document.getElementById('keyBuffer').onkeydown = sender //document.getElementById('keyBuffer').onpaste = sender \ No newline at end of file