From 099703eeb43675fc7fd0cdb822166ab9c13ed1d8 Mon Sep 17 00:00:00 2001 From: Lukewh Date: Wed, 19 Apr 2023 09:22:56 +0100 Subject: [PATCH] More refactor and more consistency --- package.json | 2 +- renderer.js | 67 +++++++++++++++++++++++++++++++++++--------------- snapcraft.yaml | 2 +- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 537b18e..a0859b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gathertown", - "version": "0.2.7", + "version": "0.2.9", "description": "A better way to meet.", "main": "main.js", "scripts": { diff --git a/renderer.js b/renderer.js index 73c2824..92cc788 100644 --- a/renderer.js +++ b/renderer.js @@ -20,8 +20,6 @@ navigator.mediaDevices.getDisplayMedia = async () => { return stream; }; -let virtualShareBtn = null; - const showBtn = (btn) => { btn.style.display = "flex"; }; @@ -40,21 +38,25 @@ const getScreenShareBtn = () => { return null; }; -const initShareBtn = () => { - let screenShareBtn = null; - if (window.game && window.game.spaceId) { - screenShareBtn = getScreenShareBtn(); +const getVirtualBtn = () => { + const btns = document.querySelectorAll("[aria-label='Screen share']"); + if (btns.length > 1) { + return Array.from(btns).filter((btn) => btn.dataset.isVirtual)[0]; } + return null; +}; - if (!screenShareBtn) { - setTimeout(initShareBtn, 1000); - return; +const setupBtns = () => { + const screenShareBtn = getScreenShareBtn(); + const virtualBtn = getVirtualBtn(); + if (virtualBtn) { + return virtualBtn; } - - virtualShareBtn = screenShareBtn.cloneNode(true); + const virtualShareBtn = screenShareBtn.cloneNode(true); virtualShareBtn.dataset.isVirtual = true; - console.log(virtualShareBtn); + virtualShareBtn.title = "Virtual"; + screenShareBtn.title = "Original"; screenShareBtn.parentNode.appendChild(virtualShareBtn); showBtn(virtualShareBtn); hideBtn(screenShareBtn); @@ -65,11 +67,28 @@ const initShareBtn = () => { }); screenShareBtn.addEventListener("click", (e) => { - showBtn(virtualShareBtn); - hideBtn(getScreenShareBtn()); + setTimeout(() => { + setupBtns(); + showBtn(screenShareBtn); + hideBtn(virtualShareBtn); + }, 500); }); - //setTimeout(initShareBtn, 60 * 1000); + return virtualShareBtn; +}; + +const initShareBtn = () => { + let screenShareBtn = null; + if (window.game && window.game.spaceId) { + screenShareBtn = getScreenShareBtn(); + } + + if (!screenShareBtn) { + setTimeout(initShareBtn, 1000); + return; + } + + setupBtns(); }; initShareBtn(); @@ -87,20 +106,28 @@ const sourceSelector = async () => { const selector = buildSourceSelector(sources); document.body.appendChild(selector); const sourceEls = selector.querySelectorAll(".source"); - const screenShareBtn = getScreenShareBtn(); for (const sourceEl of sourceEls) { sourceEl.addEventListener("click", (e) => { e.stopImmediatePropagation(); const target = findTargetByClass(e.target, "source"); const id = target.id; sourceId = id; - selector.parentNode.removeChild(selector); - + + const screenShareBtn = getScreenShareBtn(); + + // click this so that it's active screenShareBtn.click(); - showBtn(screenShareBtn); - hideBtn(virtualShareBtn); + + setTimeout(() => { + showBtn(getScreenShareBtn()); + hideBtn(getVirtualBtn()); + }, 500); + + // Remove the selector + selector.parentNode.removeChild(selector); }); } + const closeEl = selector.querySelector(".close"); closeEl.addEventListener("click", (e) => { selector.parentNode.removeChild(selector); diff --git a/snapcraft.yaml b/snapcraft.yaml index d27ca7e..4a26625 100644 --- a/snapcraft.yaml +++ b/snapcraft.yaml @@ -1,5 +1,5 @@ name: gathertown -version: 0.2.8 +version: 0.2.9 summary: A better way to meet. description: | Electron wrapper for Gather Town