Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript code cleanup #29

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions extension/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
function handleMessage(response) {
if (response.message === 'sharing-started') {
// Start pipewire-screenaudio
chrome.runtime.sendNativeMessage(response.messageName, { cmd: response.cmd })
.then(({ micId }) => {
window.localStorage.setItem('micId', micId)
chrome.runtime.sendMessage('mic-id-updated')
// Passthrough the selected node to pipewire-screenaudio
chrome.runtime.sendNativeMessage(response.messageName, { cmd: 'SetSharingNode', args: [{ node: response.args[0].node, micId }] })
})
}
// Any event, that is handled in here, should have a comment with the reason it is handled in here

if (response.message === 'node-changed') {
// Passthrough the selected node to pipewire-screenaudio
chrome.runtime.sendNativeMessage(response.messageName, { cmd: response.cmd, args: response.args })
function handleMessage(request) {
// Asynchronously update micId in LocalStorage
if (request.message === "sharing-started") {
chrome.runtime
.sendNativeMessage(request.messageName, { cmd: request.cmd })
.then(({ micId }) => {
window.localStorage.setItem("micId", micId);
chrome.runtime.sendMessage("mic-id-updated");
});
}

if (response.message === 'sharing-stopped') {
chrome.runtime.sendNativeMessage(response.messageName, { cmd: response.cmd, args: response.args })
.then(() => {
window.localStorage.setItem('micId', null)
chrome.runtime.sendMessage('mic-id-removed')
// Asynchronously update micId in LocalStorage
if (request.message === "sharing-stopped") {
chrome.runtime
.sendNativeMessage(request.messageName, {
cmd: request.cmd,
args: request.args,
})
.then(() => {
window.localStorage.setItem("micId", null);
chrome.runtime.sendMessage("mic-id-removed");
});
}

if (response.message === 'get-session-type') {
return chrome.runtime.sendNativeMessage(response.messageName, { cmd: 'GetSessionType', args: [] })
// Called from injector.js - It cannot directly call sendNativeMessage
if (request.message === "get-session-type") {
return chrome.runtime.sendNativeMessage(request.messageName, {
cmd: "GetSessionType",
args: [],
});
}
}

chrome.runtime.onMessage.addListener(handleMessage)
chrome.runtime.onMessage.addListener(handleMessage);
27 changes: 18 additions & 9 deletions extension/scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ let shareStopBtnState = null

let selectedNode = null
let nodesLoop = null
let micId = null

function setMicId (id, skipStorage) {
micId = id
skipStorage || window.localStorage.setItem('micId', id)
}

async function isRunning () {
const micId = window.localStorage.getItem('micId')
setMicId(window.localStorage.getItem('micId'), true)

if (!micId) {
return false
}

const { isRunning } = await chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'IsPipewireScreenAudioRunning', args: [{ micId }] })

if (!isRunning) {
window.localStorage.setItem('micId', null)
setMicId(null)
}

return isRunning
Expand All @@ -41,9 +48,9 @@ function setButtonToShare() {
clearInterval(nodesLoop)
shareStopBtn.appendChild(spinner)
shareStopBtn.appendChild(text)
document.getElementById('blacklist-btn').hidden = true

chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'sharing-started', cmd: 'StartPipewireScreenAudio', args: [{ node: selectedNode }] })
if (document.getElementById('blacklist-btn'))
document.getElementById('blacklist-btn').hidden = true
chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'sharing-started', cmd: 'StartPipewireScreenAudio' })
}

shareStopBtn.addEventListener('click', eventListener)
Expand All @@ -58,7 +65,6 @@ function setButtonToStop() {
const eventListener = async () => {
shareStopBtn.removeEventListener('click', eventListener)
if (await isRunning()) {
const micId = window.localStorage.getItem('micId')
chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'sharing-stopped', cmd: 'StopPipewireScreenAudio', args: [{ micId }] })
}
};
Expand Down Expand Up @@ -93,7 +99,7 @@ function createBlacklistBtn (root) {

async function updateGui () {
if (await isRunning()) {
message.innerText = `Running virtmic Id: ${window.localStorage.getItem('micId')}`
message.innerText = `Running virtmic Id: ${micId}`
message.hidden = false
dropdown.hidden = false
shareStopBtn.hidden = false
Expand Down Expand Up @@ -166,8 +172,7 @@ async function populateNodesList (response) {
dropdown.addEventListener('change', () => {
selectedNode = dropdown.value
window.localStorage.setItem('selectedNode', selectedNode)
const micId = window.localStorage.getItem('micId')
chrome.runtime.sendMessage({ messageName: MESSAGE_NAME, message: 'node-changed', cmd: 'SetSharingNode', args: [{ node: selectedNode, micId }] })
chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'SetSharingNode', args: [{ node: selectedNode, micId }] })
})
}
}
Expand Down Expand Up @@ -207,9 +212,13 @@ function onError (error) {

function handleMessage (message) {
if (message === 'mic-id-updated') {
setMicId(window.localStorage.getItem('micId'), true)

const hideBtnEl = document.getElementById('blacklist-btn')
buttonGroup.removeChild(hideBtnEl)
updateGui()

chrome.runtime.sendNativeMessage(MESSAGE_NAME, { cmd: 'SetSharingNode', args: [{ node: selectedNode, micId }] })
}

if (message === 'mic-id-removed') {
Expand Down
1 change: 1 addition & 0 deletions native/connector/pipewire-screen-audio-connector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function SetSharingNode () {
echo "$node" >> "$fifoPath"
fi

toMessage '{"success":true}'
exit
}

Expand Down