feat: Add isCurrentlyAudible() to WebContents (#13614)
* 🔧 Add isCurrentlyAudible() to WebContents * ❤️ Implement feedback, use await to wait for event * 👷 Add missing imports
This commit is contained in:
parent
e90c4abc0d
commit
deedf6c3f4
7 changed files with 57 additions and 0 deletions
|
@ -1434,6 +1434,10 @@ bool WebContents::IsAudioMuted() {
|
||||||
return web_contents()->IsAudioMuted();
|
return web_contents()->IsAudioMuted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool WebContents::IsCurrentlyAudible() {
|
||||||
|
return web_contents()->IsCurrentlyAudible();
|
||||||
|
}
|
||||||
|
|
||||||
void WebContents::Print(mate::Arguments* args) {
|
void WebContents::Print(mate::Arguments* args) {
|
||||||
PrintSettings settings = {false, false, base::string16()};
|
PrintSettings settings = {false, false, base::string16()};
|
||||||
if (args->Length() >= 1 && !args->GetNext(&settings)) {
|
if (args->Length() >= 1 && !args->GetNext(&settings)) {
|
||||||
|
@ -2018,6 +2022,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("setIgnoreMenuShortcuts", &WebContents::SetIgnoreMenuShortcuts)
|
.SetMethod("setIgnoreMenuShortcuts", &WebContents::SetIgnoreMenuShortcuts)
|
||||||
.SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
|
.SetMethod("setAudioMuted", &WebContents::SetAudioMuted)
|
||||||
.SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
|
.SetMethod("isAudioMuted", &WebContents::IsAudioMuted)
|
||||||
|
.SetMethod("isCurrentlyAudible", &WebContents::IsCurrentlyAudible)
|
||||||
.SetMethod("undo", &WebContents::Undo)
|
.SetMethod("undo", &WebContents::Undo)
|
||||||
.SetMethod("redo", &WebContents::Redo)
|
.SetMethod("redo", &WebContents::Redo)
|
||||||
.SetMethod("cut", &WebContents::Cut)
|
.SetMethod("cut", &WebContents::Cut)
|
||||||
|
|
|
@ -143,6 +143,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
void SetIgnoreMenuShortcuts(bool ignore);
|
void SetIgnoreMenuShortcuts(bool ignore);
|
||||||
void SetAudioMuted(bool muted);
|
void SetAudioMuted(bool muted);
|
||||||
bool IsAudioMuted();
|
bool IsAudioMuted();
|
||||||
|
bool IsCurrentlyAudible();
|
||||||
void Print(mate::Arguments* args);
|
void Print(mate::Arguments* args);
|
||||||
std::vector<printing::PrinterBasicInfo> GetPrinterList();
|
std::vector<printing::PrinterBasicInfo> GetPrinterList();
|
||||||
void SetEmbedder(const WebContents* embedder);
|
void SetEmbedder(const WebContents* embedder);
|
||||||
|
|
|
@ -832,6 +832,10 @@ Mute the audio on the current web page.
|
||||||
|
|
||||||
Returns `Boolean` - Whether this page has been muted.
|
Returns `Boolean` - Whether this page has been muted.
|
||||||
|
|
||||||
|
#### `contents.isCurrentlyAudible()`
|
||||||
|
|
||||||
|
Returns `Boolean` - Whether audio is currently playing.
|
||||||
|
|
||||||
#### `contents.setZoomFactor(factor)`
|
#### `contents.setZoomFactor(factor)`
|
||||||
|
|
||||||
* `factor` Number - Zoom factor.
|
* `factor` Number - Zoom factor.
|
||||||
|
|
|
@ -450,6 +450,10 @@ Set guest page muted.
|
||||||
|
|
||||||
Returns `Boolean` - Whether guest page has been muted.
|
Returns `Boolean` - Whether guest page has been muted.
|
||||||
|
|
||||||
|
#### `<webview>.isCurrentlyAudible()`
|
||||||
|
|
||||||
|
Returns `Boolean` - Whether audio is currently playing.
|
||||||
|
|
||||||
### `<webview>.undo()`
|
### `<webview>.undo()`
|
||||||
|
|
||||||
Executes editing command `undo` in page.
|
Executes editing command `undo` in page.
|
||||||
|
|
|
@ -347,6 +347,7 @@ const registerWebViewElement = function () {
|
||||||
'inspectElement',
|
'inspectElement',
|
||||||
'setAudioMuted',
|
'setAudioMuted',
|
||||||
'isAudioMuted',
|
'isAudioMuted',
|
||||||
|
'isCurrentlyAudible',
|
||||||
'undo',
|
'undo',
|
||||||
'redo',
|
'redo',
|
||||||
'cut',
|
'cut',
|
||||||
|
|
|
@ -4,12 +4,18 @@ const assert = require('assert')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const {closeWindow} = require('./window-helpers')
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
const {emittedOnce} = require('./events-helpers')
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
|
||||||
const {ipcRenderer, remote} = require('electron')
|
const {ipcRenderer, remote} = require('electron')
|
||||||
const {BrowserWindow, webContents, ipcMain, session} = remote
|
const {BrowserWindow, webContents, ipcMain, session} = remote
|
||||||
|
const {expect} = chai
|
||||||
|
|
||||||
const isCi = remote.getGlobal('isCi')
|
const isCi = remote.getGlobal('isCi')
|
||||||
|
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
/* The whole webContents API doesn't use standard callbacks */
|
/* The whole webContents API doesn't use standard callbacks */
|
||||||
/* eslint-disable standard/no-callback-literal */
|
/* eslint-disable standard/no-callback-literal */
|
||||||
|
|
||||||
|
@ -116,6 +122,21 @@ describe('webContents module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('isCurrentlyAudible() API', () => {
|
||||||
|
it('returns whether audio is playing', async () => {
|
||||||
|
w.loadURL(`file://${path.join(__dirname, 'fixtures', 'api', 'is-currently-audible.html')}`)
|
||||||
|
w.show()
|
||||||
|
await emittedOnce(w.webContents, 'did-finish-load')
|
||||||
|
|
||||||
|
expect(w.webContents.isCurrentlyAudible()).to.be.false()
|
||||||
|
|
||||||
|
w.webContents.send('play')
|
||||||
|
await emittedOnce(ipcMain, 'playing')
|
||||||
|
|
||||||
|
expect(w.webContents.isCurrentlyAudible()).to.be.true()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('getWebPreferences() API', () => {
|
describe('getWebPreferences() API', () => {
|
||||||
it('should not crash when called for devTools webContents', (done) => {
|
it('should not crash when called for devTools webContents', (done) => {
|
||||||
w.webContents.openDevTools()
|
w.webContents.openDevTools()
|
||||||
|
|
21
spec/fixtures/api/is-currently-audible.html
vendored
Normal file
21
spec/fixtures/api/is-currently-audible.html
vendored
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<div id="video"></div>
|
||||||
|
<script type="text/javascript" charset="utf-8">
|
||||||
|
const {ipcRenderer} = window.top != null ? window.top.require('electron') : require('electron')
|
||||||
|
ipcRenderer.on('play', (event) => {
|
||||||
|
const context = new window.AudioContext();
|
||||||
|
const oscillator = context.createOscillator();
|
||||||
|
|
||||||
|
// A beep
|
||||||
|
oscillator.type = 'sine';
|
||||||
|
oscillator.frequency.value = 440
|
||||||
|
oscillator.connect(context.destination)
|
||||||
|
oscillator.start()
|
||||||
|
|
||||||
|
// It'll take a few ms before the beep shows up
|
||||||
|
setTimeout(() => event.sender.send('playing'), 100)
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue