Skip to content

Commit

Permalink
Virtual mic termination moved to background script
Browse files Browse the repository at this point in the history
  • Loading branch information
IceDBorn committed Apr 10, 2023
1 parent c787963 commit b400c42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
10 changes: 9 additions & 1 deletion extension/scripts/background.js
Original file line number Diff line number Diff line change
@@ -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 })
Expand All @@ -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)
25 changes: 13 additions & 12 deletions extension/scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }] })
})
}

Expand All @@ -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 }] })
}
})
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b400c42

Please sign in to comment.