Work on toolbar buffer solution

This commit is contained in:
Kevin F 2022-02-19 12:34:08 -06:00
parent a63066edbd
commit d70732d12b
2 changed files with 54 additions and 15 deletions

View File

@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
let lastActive = null
const defaultHosts = "<all_urls>"; const defaultHosts = "<all_urls>";
let appCode = function (){ let appCode = function (){
@ -201,42 +202,57 @@ let appCode = function (){
browser.runtime.onMessage.addListener(request => { browser.runtime.onMessage.addListener(request => {
clearTimeout(bufferTimeout)
console.log(request.keys); console.log(request.keys);
if (request.keys == "GetSet"){
keyBuffer = elementToSendBuffer.value
return Promise.resolve({response: elementToSendBuffer.value});
}
clearTimeout(bufferTimeout)
if (request.keys == "Backspace"){ if (request.keys == "Backspace"){
keyBuffer = keyBuffer.slice(0, -1) keyBuffer = keyBuffer.slice(0, -1)
} }
else if(request.keys.length > 4){
// pass
}
else{ else{
keyBuffer += request.keys; keyBuffer += request.keys;
} }
bufferTimeout = setTimeout(function(){sendBuffer()}, 1000) if (request.isEmpty){
keyBuffer = ""
sendBuffer()
}
bufferTimeout = setTimeout(function(){sendBuffer()}, 100)
return Promise.resolve({response: "ack"}); return Promise.resolve({response: "ack"});
}); });
function sendBuffer(){ function sendBuffer(){
if (elementToSendBuffer == null){ if (elementToSendBuffer == null){
bufferTimeout = setTimeout(function(){sendBuffer()}, 1000) bufferTimeout = setTimeout(function(){sendBuffer()}, 100)
return return
} }
console.debug("sending buffer to " + elementToSendBuffer) console.debug("sending buffer to " + elementToSendBuffer)
elementToSendBuffer.value = keyBuffer
if (keyBuffer.length > 0){
elementToSendBuffer.value = keyBuffer
}
} }
function setSendEl(e){ function setSendEl(e){
if (document.activeElement.tagName != "INPUT"){ let activeEl = document.activeElement
if (activeEl.tagName != "INPUT" && activeEl.tagName != "TEXTAREA"){
keyBuffer = ""
return 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('focus', setSendEl)
document.addEventListener('click', setSendEl) document.addEventListener('click', setSendEl)

View File

@ -1,13 +1,18 @@
let started = false
function onError(error) { function onError(error) {
console.error(`Error: ${error}`); console.error(`Error: ${error}`);
} }
function doSendMsg(msg, tabs) { function doSendMsg(msg, tabs) {
let empty = false;
if (document.getElementById('keyBuffer').value.length == 0){
empty = true;
}
for (let tab of tabs) { for (let tab of tabs) {
browser.tabs.sendMessage( browser.tabs.sendMessage(
tab.id, tab.id,
{keys: msg} {keys: msg, isEmpty: empty}
).then(response => { ).then(response => {
console.log("Message from the content script:"); console.log("Message from the content script:");
console.log(response.response); 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 sender = async function(e){
let sendMessageToTabs = function(tabs){ let sendMessageToTabs = function(tabs){
doSendMsg(e.key, tabs) doSendMsg(e.key, tabs)
} }
@ -29,6 +52,6 @@ let sender = async function(e){
}).then(sendMessageToTabs).catch(onError); }).then(sendMessageToTabs).catch(onError);
} }
setTimeout(function(){getCurrent()}, 10)
document.getElementById('keyBuffer').onkeydown = sender document.getElementById('keyBuffer').onkeydown = sender
//document.getElementById('keyBuffer').onpaste = sender //document.getElementById('keyBuffer').onpaste = sender