From b400c42ef3c82f01b19d1504de2e33999afdf74c Mon Sep 17 00:00:00 2001 From: IceDBorn Date: Mon, 10 Apr 2023 03:41:23 +0300 Subject: [PATCH] Virtual mic termination moved to background script --- extension/scripts/background.js | 10 +++++++++- extension/scripts/popup.js | 25 +++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/extension/scripts/background.js b/extension/scripts/background.js index 1478bc5..bb19666 100644 --- a/extension/scripts/background.js +++ b/extension/scripts/background.js @@ -1,4 +1,4 @@ -function handleMessage(response) { +function handleMessage (response) { if (response.message === 'node-shared') { // Passthrough the selected node to pipewire-screenaudio chrome.runtime.sendNativeMessage(response.messageName, { cmd: response.cmd, args: response.args }) @@ -7,6 +7,14 @@ function handleMessage(response) { chrome.runtime.sendMessage('pid-updated') }) } + + if (response.message === 'node-stopped') { + chrome.runtime.sendNativeMessage(response.messageName, { cmd: response.cmd, args: response.args }) + .then(() => { + window.localStorage.setItem('micPid', null) + chrome.runtime.sendMessage('pid-removed') + }) + } } chrome.runtime.onMessage.addListener(handleMessage) diff --git a/extension/scripts/popup.js b/extension/scripts/popup.js index 4bb6dac..10e13e2 100644 --- a/extension/scripts/popup.js +++ b/extension/scripts/popup.js @@ -41,7 +41,7 @@ function createShareBtn (root) { shareBtnEl.appendChild(text) root.removeChild(blacklistBtnEl) - chrome.runtime.sendMessage({messageName: MESSAGE_NAME, message: 'node-shared', cmd: 'StartPipewireScreenAudio', args: [{ node: selectedNode }]}) + chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'node-shared', cmd: 'StartPipewireScreenAudio', args: [{ node: selectedNode }] }) }) } @@ -56,12 +56,7 @@ function createStopBtn (root) { stopBtnEl.addEventListener('click', async () => { if (await isRunning()) { const micPid = window.localStorage.getItem('micPid') - chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'StopPipewireScreenAudio', args: [{ micPid }] }) - .then(() => { - root.removeChild(stopBtnEl) - window.localStorage.setItem('micPid', null) - updateGui() - }) + chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'node-stopped', cmd: 'StopPipewireScreenAudio', args: [{ micPid }] }) } }) } @@ -103,8 +98,8 @@ async function updateGui () { createShareBtn(buttonGroup) createBlacklistBtn(buttonGroup) } else { - message.innerText = "No nodes available to share..." - message.className = "mt-5" + message.innerText = 'No nodes available to share...' + message.className = 'mt-5' message.hidden = false dropdown.hidden = true } @@ -137,8 +132,8 @@ async function populateNodesList (response) { } if (!dropdown.children.length) { - message.innerText = "No nodes available to share..." - message.className = "mt-5" + message.innerText = 'No nodes available to share...' + message.className = 'mt-5' message.hidden = false dropdown.hidden = true document.getElementById('share-btn').hidden = true @@ -179,12 +174,18 @@ function onError (error) { dropdown.hidden = true } -function handleMessage(message) { +function handleMessage (message) { if (message === 'pid-updated') { const shareBtnEl = document.getElementById('share-btn') buttonGroup.removeChild(shareBtnEl) updateGui() } + + if (message === 'pid-removed') { + const stopBtnEl = document.getElementById('stop-btn') + buttonGroup.removeChild(stopBtnEl) + updateGui() + } } chrome.runtime.onMessage.addListener(handleMessage)