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

feat: add blind and mute options while autojoining #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions src/components/SettingsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ const SettingsForm = ({ settingsState: [settings, setSettings] }: Props) => {
value={settings.autoJoin}
onChange={onChange}
/>
{settings.autoJoin && (
<>
<Row
label=""
description="Turn off Camera while auto joining"
name="joinBlind"
value={settings.joinBlind}
onChange={onChange}
/>
<Row
label=""
description="Mute while auto joining"
name="joinMuted"
value={settings.joinMuted}
onChange={onChange}
/>
</>
)}
<Row
label="Ask for confirmation before exit"
description="Restart chrome after enabling this setting"
Expand Down
76 changes: 71 additions & 5 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ import { SettingsFormValues } from '../popup/Popup'

const getJoinButton = () => document.getElementsByClassName(joinButtonClass)[0] as HTMLElement

const getCameraButton = (): HTMLElement | undefined => {
const elements = document.querySelectorAll('[aria-label]')
let cameraElement: HTMLElement | undefined = undefined
elements.forEach((element) => {
if (element.ariaLabel?.includes('camera') && element.ariaLabel.startsWith('Turn')) {
cameraElement = element as HTMLElement
}
})

return cameraElement
}

const getMicrophoneButton = (): HTMLElement | undefined => {
const elements = document.querySelectorAll('[aria-label]')
let micElement: HTMLElement | undefined = undefined
elements.forEach((element) => {
if (element.ariaLabel?.includes('microphone') && element.ariaLabel.startsWith('Turn')) {
micElement = element as HTMLElement
}
})

return micElement
}

const unloadHandler = (event: BeforeUnloadEvent) => {
if (document.querySelector('video')) {
// event.preventDefault() unsupported on chrome
Expand All @@ -14,16 +38,55 @@ const unloadHandler = (event: BeforeUnloadEvent) => {
}
}

const clickJoinbutton = () => {
const turnOffCamera = () => {
const cameraBtn = getCameraButton()
if (cameraBtn && cameraBtn.ariaLabel?.includes('off')) {
cameraBtn.click()
// return if the button was clicked or not
return cameraBtn.ariaLabel.includes('on')
}
return false
}

const turnOffMic = () => {
const micBtn = getMicrophoneButton()
if (micBtn && micBtn.ariaLabel?.includes('off')) {
micBtn.click()
// return if the button was clicked or not
return micBtn.ariaLabel.includes('on')
}
return false
}

type JoinOptions = {
blind: boolean
muted: boolean
}

const clickJoinbutton = ({ blind, muted }: JoinOptions) => {
let counter = 0
let isMuted = false
let isCameraOff = false

// After clicking we don't clear the interval
// because sometimes clicking the button the first time, doesn't work
const intervalID = setInterval(() => {
const joinButton = getJoinButton()
if (joinButton) {
joinButton.click()
if (blind && !isCameraOff) {
isCameraOff = turnOffCamera()
}
if (muted && !isMuted) {
isMuted = turnOffMic()
}

const canJoin = blind === isCameraOff && muted === isMuted

if (canJoin) {
const joinButton = getJoinButton()
if (joinButton) {
joinButton.click()
}
}

counter++
if (counter >= 5) {
clearInterval(intervalID)
Expand All @@ -39,7 +102,10 @@ const settingsManager = (settings: SettingsFormValues | undefined) => {
}

if (settings?.autoJoin) {
clickJoinbutton()
clickJoinbutton({
blind: settings?.joinBlind,
muted: settings?.joinMuted,
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ const settingsFormInitialValues = {
confirmExit: false,
disableVideo: false,
focusTab: false,
joinMuted: true,
joinBlind: true,
}
2 changes: 2 additions & 0 deletions src/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ export interface SettingsFormValues {
confirmExit: boolean
disableVideo: boolean
autoJoin: boolean
joinMuted: boolean
joinBlind: boolean
}