From 6999cce814f9d2e5cd5949fd375079963a6e39d7 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 18 Nov 2026 14:44:40 +0100 Subject: [PATCH 1/3] Form screen recording: respect the Microphone setting, mic off by default (#229336) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The feedback form's "Record screen" (GleapScreenRecorder) ignored Developer options → Microphone (flowConfig.capture.allowMicrophone) and started with the microphone on, so it called getUserMedia on every recording. On a site that already holds microphone permission the browser grants that without a prompt, and the recording had sound although the project had the microphone off. Only capture requests checked the setting. - The recorder asks for the microphone only when allowMicrophone is true and the browser has getUserMedia, read on every use. - With the setting on, the microphone is opt-in: off until the user turns it on, like capture requests. - The toolbar's mic button is hidden when the setting is off, and for a recording that started without the microphone (it can't be added then); it comes back for the next recording. Gleap ticket: https://app.gleap.io/projects/613b5dd730aed737108f87ab/bugs/6afd9df1768dffc58cb92c62 Co-Authored-By: Claude Opus 5.5 --- src/GleapMarkerManager.js | 10 +++- src/GleapScreenRecorder.js | 39 ++++++++++-- src/GleapScreenRecorder.test.js | 101 ++++++++++++++++++++++++++++++-- 3 files changed, 138 insertions(+), 12 deletions(-) diff --git a/src/GleapMarkerManager.js b/src/GleapMarkerManager.js index 774bd236..adb62bd0 100644 --- a/src/GleapMarkerManager.js +++ b/src/GleapMarkerManager.js @@ -644,10 +644,13 @@ export default class GleapMarkerManager { const toolbarItem = toolbarItems[i]; const type = toolbarItem.getAttribute('data-type'); switch (type) { - case 'mic': - if (this.screenRecorder.audioAvailable && this.screenRecorder.available) { + case 'mic': { + // Developer options → Microphone decides whether the button is there at all. + const micState = this.screenRecorder.micButtonState(); + toolbarItem.style.display = micState === 'hidden' ? 'none' : ''; + if (micState === 'on' || micState === 'off') { toolbarItem.classList.remove(itemInactiveClass); - if (!this.screenRecorder.audioMuted) { + if (micState === 'on') { toolbarItem.classList.remove('bb-capture-toolbar-item--inactivecross'); audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('mute'); } else { @@ -660,6 +663,7 @@ export default class GleapMarkerManager { audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('browserNotSupported'); } break; + } case 'recording': if (this.screenRecorder.available) { diff --git a/src/GleapScreenRecorder.js b/src/GleapScreenRecorder.js index fe6fb900..ab7248d2 100644 --- a/src/GleapScreenRecorder.js +++ b/src/GleapScreenRecorder.js @@ -1,7 +1,7 @@ import GleapSession from './GleapSession'; import { DisplayRecording, requestDisplayStream } from './GleapCaptureRecorder'; import { stopStream } from './GleapCaptureScreenshot'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; // The feedback form's screen recording ("Record screen" in the form's capture step). It records the // same way capture requests do (GleapCaptureRecorder): Chromium offers the current tab with one @@ -21,8 +21,10 @@ export class GleapScreenRecorder { rerender; // The running DisplayRecording, from the Start click until it stopped. recording = null; - audioMuted = false; - audioAvailable = true; + // The microphone is opt-in: off until the user turns it on, and never used unless the project + // allows it (Developer options → Microphone, flowConfig.capture.allowMicrophone). + audioMuted = true; + audioAvailable = false; available = true; isRecording = false; file = null; @@ -32,12 +34,36 @@ export class GleapScreenRecorder { this.rerender = rerender; this.permissionErrorText = permissionErrorText; this.available = hasDisplayMedia(); + this.audioAvailable = this.microphoneAllowed(); setTimeout(() => { this.rerender(); }, GleapScreenRecorder.RERENDER_DELAY); } + // Read on every use: the project's config can arrive or change after the form opened. + microphoneAllowed() { + return getCaptureConfig().allowMicrophone === true && hasUserMedia(); + } + + /** + * What the toolbar's mic button shows: 'hidden' (the project doesn't allow the microphone, or this + * recording started without it and it can't be added now), 'unavailable' (asked for, but the + * browser gave none), 'on' or 'off'. + */ + micButtonState() { + if (!this.available || !this.microphoneAllowed()) { + return 'hidden'; + } + if (this.isRecording && this.audioMuted && !this.audioAvailable) { + return 'hidden'; + } + if (!this.audioAvailable) { + return 'unavailable'; + } + return this.audioMuted ? 'off' : 'on'; + } + formatTime(s) { return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s; } @@ -83,7 +109,7 @@ export class GleapScreenRecorder { stopStream(stream); return null; } - return recording.start(stream, !this.audioMuted).then(({ hasMicrophone }) => { + return recording.start(stream, this.microphoneAllowed() && !this.audioMuted).then(({ hasMicrophone }) => { this.audioAvailable = hasMicrophone; this.isRecording = true; this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); @@ -110,6 +136,8 @@ export class GleapScreenRecorder { } this.recording = null; this.isRecording = false; + // The next recording may ask for the microphone again. + this.audioAvailable = this.microphoneAllowed(); this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); if (result) { @@ -135,6 +163,9 @@ export class GleapScreenRecorder { } toggleAudio() { + if (!this.microphoneAllowed() || !this.audioAvailable) { + return; + } this.audioMuted = !this.audioMuted; if (this.recording) { this.recording.setMicrophoneEnabled(!this.audioMuted); diff --git a/src/GleapScreenRecorder.test.js b/src/GleapScreenRecorder.test.js index 4b4b6b7e..55dcba7d 100644 --- a/src/GleapScreenRecorder.test.js +++ b/src/GleapScreenRecorder.test.js @@ -3,7 +3,7 @@ */ import { GleapScreenRecorder } from './GleapScreenRecorder'; import { requestDisplayStream } from './GleapCaptureRecorder'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; // The feedback form's recorder runs on the capture requests' recording engine (#228450). const mockRecordings = []; @@ -11,7 +11,9 @@ const mockRecordings = []; jest.mock('./GleapSession', () => ({ __esModule: true, default: { getInstance: () => ({}) } })); jest.mock('./GleapCaptureScreenshot', () => ({ stopStream: jest.fn() })); jest.mock('./GleapCaptureSettings', () => ({ + getCaptureConfig: jest.fn(() => ({})), hasDisplayMedia: jest.fn(() => true), + hasUserMedia: jest.fn(() => true), isDesktopChromium: jest.fn(() => true), })); jest.mock('./GleapCaptureRecorder', () => ({ @@ -19,7 +21,8 @@ jest.mock('./GleapCaptureRecorder', () => ({ DisplayRecording: class { constructor(options) { this.options = options; - this.start = jest.fn(() => Promise.resolve({ hasMicrophone: true })); + // Like the real engine: a microphone track only when one was asked for. + this.start = jest.fn((stream, withMicrophone) => Promise.resolve({ hasMicrophone: !!withMicrophone })); this.stop = jest.fn(); this.cancel = jest.fn(); this.setMicrophoneEnabled = jest.fn(); @@ -39,6 +42,9 @@ beforeEach(() => { mockRecordings.length = 0; rerender = jest.fn(); hasDisplayMedia.mockReturnValue(true); + hasUserMedia.mockReturnValue(true); + // Developer options → Microphone is on in most tests; the setting's own tests turn it off. + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); isDesktopChromium.mockReturnValue(true); requestDisplayStream.mockReset().mockReturnValue(Promise.resolve(STREAM)); stopStream.mockClear(); @@ -49,8 +55,9 @@ beforeEach(() => { '
'; }); -test('Start asks for the current tab (Chromium) inside the click and records with the microphone', async () => { +test('Start asks for the current tab (Chromium) inside the click and records with the microphone the user turned on', async () => { const recorder = createRecorder(); + recorder.toggleAudio(); recorder.startScreenRecording(); // Synchronously: the share dialog needs the click's user activation. @@ -63,10 +70,10 @@ test('Start asks for the current tab (Chromium) inside the click and records wit expect(recorder.audioAvailable).toBe(true); }); -test('other browsers get their own picker; a muted start records without the microphone', async () => { +test('other browsers get their own picker; the microphone is off until the user turns it on', async () => { isDesktopChromium.mockReturnValue(false); const recorder = createRecorder(); - recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); recorder.startScreenRecording(); await flush(); @@ -74,6 +81,40 @@ test('other browsers get their own picker; a muted start records without the mic expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); }); +// Gleap #229336: the form recorded sound with Developer options → Microphone off, silently where +// the site already had microphone permission. +test('with the Microphone setting off the recording never asks for the microphone', async () => { + getCaptureConfig.mockReturnValue({}); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + expect(recorder.audioAvailable).toBe(false); + + // The mic button is hidden then; a click on it still changes nothing. + recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); + recorder.startScreenRecording(); + await flush(); + + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + expect(recorder.audioAvailable).toBe(false); +}); + +test('allowMicrophone must be exactly true, and the browser must have getUserMedia', async () => { + getCaptureConfig.mockReturnValue({ allowMicrophone: 'true' }); + expect(createRecorder().microphoneAllowed()).toBe(false); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + hasUserMedia.mockReturnValue(false); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); +}); + test('the timer counts down and the stopped recording becomes the file and the preview', async () => { const recorder = createRecorder(); recorder.startScreenRecording(); @@ -130,3 +171,53 @@ test('without getDisplayMedia the recorder is unavailable', () => { expect(recorder.available).toBe(false); expect(requestDisplayStream).not.toHaveBeenCalled(); }); + +test('the mic button: hidden without the setting, off by default, on once turned on', async () => { + getCaptureConfig.mockReturnValue({}); + expect(createRecorder().micButtonState()).toBe('hidden'); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + const recorder = createRecorder(); + expect(recorder.micButtonState()).toBe('off'); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe('on'); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe('off'); +}); + +test('started without the microphone: the button goes away for that recording and is back after it', async () => { + const recorder = createRecorder(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe('hidden'); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + + mockRecordings[0].options.onStop(null); + expect(recorder.micButtonState()).toBe('off'); +}); + +test('turned on but the browser gave no microphone: shown as unavailable, mute does nothing', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + mockRecordings[0].start.mockReturnValue(Promise.resolve({ hasMicrophone: false })); + await flush(); + + expect(recorder.micButtonState()).toBe('unavailable'); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); +}); + +test('muting while recording with the microphone turns its track off', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe('on'); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).toHaveBeenCalledWith(false); + expect(recorder.micButtonState()).toBe('off'); +}); From 3fc2c5c3d2c1e09f342422b7a728ac63acdbea24 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 18 Nov 2026 15:14:26 +0100 Subject: [PATCH 2/3] test: the form's screen recording uses the microphone only when the project allows it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Developer options → Microphone (flowConfig.capture.allowMicrophone) decides whether the feedback form's "Record screen" may ask for the microphone, and with it on the microphone is off until the user turns it on, like capture requests. Two existing tests change because that default changes: they started with the microphone on. Co-Authored-By: Claude Opus 5.5 --- src/GleapScreenRecorder.test.js | 103 ++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 6 deletions(-) diff --git a/src/GleapScreenRecorder.test.js b/src/GleapScreenRecorder.test.js index 1dece9ac..626edf12 100644 --- a/src/GleapScreenRecorder.test.js +++ b/src/GleapScreenRecorder.test.js @@ -2,9 +2,9 @@ * @vitest-environment jsdom */ import { test, expect, vi, beforeEach } from 'vitest'; -import { GleapScreenRecorder } from './GleapScreenRecorder'; +import { GleapScreenRecorder, MIC_BUTTON } from './GleapScreenRecorder'; import { requestDisplayStream } from './GleapCaptureRecorder'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; import { stopStream } from './GleapCaptureScreenshot'; // The feedback form's recorder runs on the capture requests' recording engine (#228450). @@ -13,7 +13,9 @@ const mockRecordings = []; vi.mock('./GleapSession', () => ({ __esModule: true, default: { getInstance: () => ({}) } })); vi.mock('./GleapCaptureScreenshot', () => ({ stopStream: vi.fn() })); vi.mock('./GleapCaptureSettings', () => ({ + getCaptureConfig: vi.fn(() => ({})), hasDisplayMedia: vi.fn(() => true), + hasUserMedia: vi.fn(() => true), isDesktopChromium: vi.fn(() => true), })); vi.mock('./GleapCaptureRecorder', () => ({ @@ -21,7 +23,8 @@ vi.mock('./GleapCaptureRecorder', () => ({ DisplayRecording: class { constructor(options) { this.options = options; - this.start = vi.fn(() => Promise.resolve({ hasMicrophone: true })); + // Like the real engine: a microphone track only when one was asked for. + this.start = vi.fn((stream, withMicrophone) => Promise.resolve({ hasMicrophone: !!withMicrophone })); this.stop = vi.fn(); this.cancel = vi.fn(); this.setMicrophoneEnabled = vi.fn(); @@ -40,6 +43,9 @@ beforeEach(() => { mockRecordings.length = 0; rerender = vi.fn(); hasDisplayMedia.mockReturnValue(true); + hasUserMedia.mockReturnValue(true); + // Developer options → Microphone is on in most tests; the setting's own tests turn it off. + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); isDesktopChromium.mockReturnValue(true); requestDisplayStream.mockReset().mockReturnValue(Promise.resolve(STREAM)); stopStream.mockClear(); @@ -50,8 +56,9 @@ beforeEach(() => { '
'; }); -test('Start asks for the current tab (Chromium) inside the click and records with the microphone', async () => { +test('Start asks for the current tab (Chromium) inside the click and records with the microphone the user turned on', async () => { const recorder = createRecorder(); + recorder.toggleAudio(); recorder.startScreenRecording(); // Synchronously: the share dialog needs the click's user activation. @@ -64,10 +71,10 @@ test('Start asks for the current tab (Chromium) inside the click and records wit expect(recorder.audioAvailable).toBe(true); }); -test('other browsers get their own picker; a muted start records without the microphone', async () => { +test('other browsers get their own picker; the microphone is off until the user turns it on', async () => { isDesktopChromium.mockReturnValue(false); const recorder = createRecorder(); - recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); recorder.startScreenRecording(); await flush(); @@ -75,6 +82,40 @@ test('other browsers get their own picker; a muted start records without the mic expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); }); +// A site that already holds microphone permission gets no browser prompt, so the setting is the +// only thing that keeps sound out of the recording. +test('with the Microphone setting off the recording never asks for the microphone', async () => { + getCaptureConfig.mockReturnValue({}); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + expect(recorder.audioAvailable).toBe(false); + + // The mic button is hidden then; a click on it still changes nothing. + recorder.toggleAudio(); + expect(recorder.audioMuted).toBe(true); + recorder.startScreenRecording(); + await flush(); + + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + expect(recorder.audioAvailable).toBe(false); +}); + +test('allowMicrophone must be exactly true, and the browser must have getUserMedia', async () => { + getCaptureConfig.mockReturnValue({ allowMicrophone: 'true' }); + expect(createRecorder().microphoneAllowed()).toBe(false); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + hasUserMedia.mockReturnValue(false); + const recorder = createRecorder(); + expect(recorder.microphoneAllowed()).toBe(false); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + expect(mockRecordings[0].start).toHaveBeenCalledWith(STREAM, false); +}); + test('the timer counts down and the stopped recording becomes the file and the preview', async () => { const recorder = createRecorder(); recorder.startScreenRecording(); @@ -131,3 +172,53 @@ test('without getDisplayMedia the recorder is unavailable', () => { expect(recorder.available).toBe(false); expect(requestDisplayStream).not.toHaveBeenCalled(); }); + +test('the mic button: hidden without the setting, off by default, on once turned on', () => { + getCaptureConfig.mockReturnValue({}); + expect(createRecorder().micButtonState()).toBe(MIC_BUTTON.HIDDEN); + + getCaptureConfig.mockReturnValue({ allowMicrophone: true }); + const recorder = createRecorder(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.ON); + recorder.toggleAudio(); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); + +test('started without the microphone: the button goes away for that recording and is back after it', async () => { + const recorder = createRecorder(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.HIDDEN); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); + + mockRecordings[0].options.onStop(null); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); + +test('turned on but the browser gave no microphone: shown as unavailable, mute does nothing', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + mockRecordings[0].start.mockReturnValue(Promise.resolve({ hasMicrophone: false })); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.UNAVAILABLE); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).not.toHaveBeenCalled(); +}); + +test('muting while recording with the microphone turns its track off', async () => { + const recorder = createRecorder(); + recorder.toggleAudio(); + recorder.startScreenRecording(); + await flush(); + + expect(recorder.micButtonState()).toBe(MIC_BUTTON.ON); + recorder.toggleAudio(); + expect(mockRecordings[0].setMicrophoneEnabled).toHaveBeenCalledWith(false); + expect(recorder.micButtonState()).toBe(MIC_BUTTON.OFF); +}); From 9dffda2413e06cb652af019563801a008d318cc8 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 18 Nov 2026 15:16:09 +0100 Subject: [PATCH 3/3] fix: the form's screen recording respects the Microphone setting, mic off by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The feedback form's "Record screen" ignored Developer options → Microphone (flowConfig.capture.allowMicrophone) and started with the microphone on, so every recording called getUserMedia. On a site that already holds microphone permission the browser grants that without a prompt, and the recording had sound although the project had the microphone off. Only capture requests checked the setting. - The recorder asks for the microphone only when allowMicrophone is true and the browser has getUserMedia, read on every use. - With the setting on, the microphone is off until the user turns it on, like capture requests. - The toolbar's mic button is hidden when the setting is off, and for a recording that started without the microphone (it can't be added then); it is back for the next recording. Its rendering moves to GleapScreenRecorderMicButton.js, and the tooltip is set as text. Co-Authored-By: Claude Opus 5.5 --- src/GleapMarkerManager.js | 21 +++------ src/GleapScreenRecorder.js | 45 ++++++++++++++++-- src/GleapScreenRecorderMicButton.js | 38 +++++++++++++++ src/GleapScreenRecorderMicButton.test.js | 60 ++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 src/GleapScreenRecorderMicButton.js create mode 100644 src/GleapScreenRecorderMicButton.test.js diff --git a/src/GleapMarkerManager.js b/src/GleapMarkerManager.js index 774bd236..9d1fe2da 100644 --- a/src/GleapMarkerManager.js +++ b/src/GleapMarkerManager.js @@ -2,6 +2,7 @@ import Gleap, { GleapTranslationManager, GleapFrameManager, GleapCaptureManager import { ScreenDrawer } from './ScreenDrawer'; import { GleapScrollStopper } from './GleapScrollStopper'; import { GleapScreenRecorder } from './GleapScreenRecorder'; +import { renderMicButton } from './GleapScreenRecorderMicButton'; import { captureTabScreenshot, requestTabStream } from './GleapCaptureScreenshot'; import { canCaptureTab } from './GleapCaptureSettings'; import { loadIcon } from './UI'; @@ -645,20 +646,12 @@ export default class GleapMarkerManager { const type = toolbarItem.getAttribute('data-type'); switch (type) { case 'mic': - if (this.screenRecorder.audioAvailable && this.screenRecorder.available) { - toolbarItem.classList.remove(itemInactiveClass); - if (!this.screenRecorder.audioMuted) { - toolbarItem.classList.remove('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('mute'); - } else { - toolbarItem.classList.add('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('unmute'); - } - } else { - toolbarItem.classList.add(itemInactiveClass); - toolbarItem.classList.add('bb-capture-toolbar-item--inactivecross'); - audioRecordingTooltip.innerHTML = GleapTranslationManager.translateText('browserNotSupported'); - } + renderMicButton(toolbarItem, { + state: this.screenRecorder.micButtonState(), + tooltip: audioRecordingTooltip, + inactiveClass: itemInactiveClass, + translate: (key) => GleapTranslationManager.translateText(key), + }); break; case 'recording': diff --git a/src/GleapScreenRecorder.js b/src/GleapScreenRecorder.js index fe6fb900..3d061503 100644 --- a/src/GleapScreenRecorder.js +++ b/src/GleapScreenRecorder.js @@ -1,7 +1,10 @@ import GleapSession from './GleapSession'; import { DisplayRecording, requestDisplayStream } from './GleapCaptureRecorder'; import { stopStream } from './GleapCaptureScreenshot'; -import { hasDisplayMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { getCaptureConfig, hasDisplayMedia, hasUserMedia, isDesktopChromium } from './GleapCaptureSettings'; +import { MIC_BUTTON } from './GleapScreenRecorderMicButton'; + +export { MIC_BUTTON }; // The feedback form's screen recording ("Record screen" in the form's capture step). It records the // same way capture requests do (GleapCaptureRecorder): Chromium offers the current tab with one @@ -21,8 +24,9 @@ export class GleapScreenRecorder { rerender; // The running DisplayRecording, from the Start click until it stopped. recording = null; - audioMuted = false; - audioAvailable = true; + // Opt-in: off until the user turns it on, and never used unless the project allows it. + audioMuted = true; + audioAvailable = false; available = true; isRecording = false; file = null; @@ -32,12 +36,40 @@ export class GleapScreenRecorder { this.rerender = rerender; this.permissionErrorText = permissionErrorText; this.available = hasDisplayMedia(); + this.audioAvailable = this.microphoneAllowed(); setTimeout(() => { this.rerender(); }, GleapScreenRecorder.RERENDER_DELAY); } + /** + * Whether this recording may use the microphone: Developer options → Microphone is on and the + * browser has getUserMedia. Read on every use, as the project's config can arrive after the form. + * @returns {boolean} + */ + microphoneAllowed() { + return getCaptureConfig().allowMicrophone === true && hasUserMedia(); + } + + /** + * What the toolbar's mic button shows. A recording that started without the microphone can't + * add it, so the button is hidden until it ends. + * @returns {string} one of MIC_BUTTON + */ + micButtonState() { + if (!this.available || !this.microphoneAllowed()) { + return MIC_BUTTON.HIDDEN; + } + if (this.isRecording && this.audioMuted && !this.audioAvailable) { + return MIC_BUTTON.HIDDEN; + } + if (!this.audioAvailable) { + return MIC_BUTTON.UNAVAILABLE; + } + return this.audioMuted ? MIC_BUTTON.OFF : MIC_BUTTON.ON; + } + formatTime(s) { return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s; } @@ -83,7 +115,7 @@ export class GleapScreenRecorder { stopStream(stream); return null; } - return recording.start(stream, !this.audioMuted).then(({ hasMicrophone }) => { + return recording.start(stream, this.microphoneAllowed() && !this.audioMuted).then(({ hasMicrophone }) => { this.audioAvailable = hasMicrophone; this.isRecording = true; this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); @@ -110,6 +142,8 @@ export class GleapScreenRecorder { } this.recording = null; this.isRecording = false; + // The next recording may ask for the microphone again. + this.audioAvailable = this.microphoneAllowed(); this.setTimerLabel(GleapScreenRecorder.MAX_RECORD_TIME); if (result) { @@ -135,6 +169,9 @@ export class GleapScreenRecorder { } toggleAudio() { + if (!this.microphoneAllowed() || !this.audioAvailable) { + return; + } this.audioMuted = !this.audioMuted; if (this.recording) { this.recording.setMicrophoneEnabled(!this.audioMuted); diff --git a/src/GleapScreenRecorderMicButton.js b/src/GleapScreenRecorderMicButton.js new file mode 100644 index 00000000..9819fa24 --- /dev/null +++ b/src/GleapScreenRecorderMicButton.js @@ -0,0 +1,38 @@ +/** + * What the toolbar's mic button shows (GleapScreenRecorder.micButtonState). + */ +export const MIC_BUTTON = Object.freeze({ + // The project doesn't allow the microphone, or this recording started without it. + HIDDEN: 'hidden', + // Asked for, but the browser gave none. + UNAVAILABLE: 'unavailable', + ON: 'on', + OFF: 'off', +}); + +const CROSSED_CLASS = 'bb-capture-toolbar-item--inactivecross'; + +const TOOLTIP_KEYS = Object.freeze({ + [MIC_BUTTON.ON]: 'mute', + [MIC_BUTTON.OFF]: 'unmute', + [MIC_BUTTON.UNAVAILABLE]: 'browserNotSupported', +}); + +/** + * Shows the feedback form toolbar's mic button for one of MIC_BUTTON's states. + * @param {HTMLElement} item the mic button + * @param {{state: string, tooltip: HTMLElement|null, inactiveClass: string, translate: function(string): string}} options + * @returns {void} + */ +export const renderMicButton = (item, { state, tooltip, inactiveClass, translate }) => { + item.style.display = state === MIC_BUTTON.HIDDEN ? 'none' : ''; + if (state === MIC_BUTTON.HIDDEN) { + return; + } + const usable = state === MIC_BUTTON.ON || state === MIC_BUTTON.OFF; + item.classList.toggle(inactiveClass, !usable); + item.classList.toggle(CROSSED_CLASS, state !== MIC_BUTTON.ON); + if (tooltip) { + tooltip.textContent = translate(TOOLTIP_KEYS[state] || TOOLTIP_KEYS[MIC_BUTTON.UNAVAILABLE]); + } +}; diff --git a/src/GleapScreenRecorderMicButton.test.js b/src/GleapScreenRecorderMicButton.test.js new file mode 100644 index 00000000..d0466f46 --- /dev/null +++ b/src/GleapScreenRecorderMicButton.test.js @@ -0,0 +1,60 @@ +/** + * @vitest-environment jsdom + */ +import { test, expect, beforeEach } from 'vitest'; +import { MIC_BUTTON, renderMicButton } from './GleapScreenRecorderMicButton'; + +const INACTIVE = 'bb-capture-editor-item-inactive'; +const CROSSED = 'bb-capture-toolbar-item--inactivecross'; + +let item; +let tooltip; +const render = (state) => + renderMicButton(item, { state, tooltip, inactiveClass: INACTIVE, translate: (key) => `t:${key}` }); + +beforeEach(() => { + document.body.innerHTML = + '
'; + item = document.querySelector('[data-type="mic"]'); + tooltip = document.querySelector('.bb-tooltip-audio-recording'); +}); + +test('hidden: the button is not shown at all', () => { + render(MIC_BUTTON.HIDDEN); + expect(item.style.display).toBe('none'); +}); + +test('off: shown crossed out, the tooltip offers to unmute', () => { + render(MIC_BUTTON.OFF); + expect(item.style.display).toBe(''); + expect(item.classList.contains(INACTIVE)).toBe(false); + expect(item.classList.contains(CROSSED)).toBe(true); + expect(tooltip.textContent).toBe('t:unmute'); +}); + +test('on: shown without the cross, the tooltip offers to mute', () => { + render(MIC_BUTTON.OFF); + render(MIC_BUTTON.ON); + expect(item.classList.contains(INACTIVE)).toBe(false); + expect(item.classList.contains(CROSSED)).toBe(false); + expect(tooltip.textContent).toBe('t:mute'); +}); + +test('unavailable: shown inactive and crossed out', () => { + render(MIC_BUTTON.UNAVAILABLE); + expect(item.classList.contains(INACTIVE)).toBe(true); + expect(item.classList.contains(CROSSED)).toBe(true); + expect(tooltip.textContent).toBe('t:browserNotSupported'); +}); + +test('shown again after being hidden', () => { + render(MIC_BUTTON.HIDDEN); + render(MIC_BUTTON.OFF); + expect(item.style.display).toBe(''); +}); + +test('a tooltip that is not there is skipped', () => { + tooltip = null; + expect(() => render(MIC_BUTTON.ON)).not.toThrow(); + expect(item.classList.contains(CROSSED)).toBe(false); +});