Skip to content

Commit a04ca0b

Browse files
committed
Completed APIs from #956
1 parent fffd7df commit a04ca0b

6 files changed

Lines changed: 150 additions & 1 deletion

File tree

‎Changelog.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- Added `WebContents.InsertCSSAsync` and `WebContents.RemoveInsertedCSSAsync` for dynamic CSS (#956)
1818
- Added `WebContents` editing and selection APIs (undo, redo, cut, copy, paste, insert text, select, ...) (#956)
1919
- Added `WebContents.FindInPageAsync`, `WebContents.StopFindInPage` and the `OnFoundInPage` event (#956)
20+
- Added `WebContents` audio events `OnAudioStateChanged`, `OnMediaStartedPlaying` and `OnMediaPaused` (#956)
21+
- Added `WebContents.ScrollToTop` and `WebContents.ScrollToBottom` (#956)
2022
- Added target framework customization (#1095) @epsnm
2123
- Added configurable Electron root directory for custom packaging layouts (#1106) @DYH1319
2224
- Added ability for `custom_main.js` to modify command line switches (#1029) @AeonSake

‎docs/API/WebContents.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ Loads the given HTML file, relative to the root of the application.
162162
- `filePath` - Path to the HTML file
163163
- `options` - Optional `Query`, `Search` and `Hash` parts of the resulting URL
164164

165+
#### 🧊 `void SetAudioMuted(bool muted)`
166+
Mutes or unmutes the audio on the current web page.
167+
168+
#### 🧊 `Task<bool> IsAudioMutedAsync()`
169+
Whether this page has been muted.
170+
171+
#### 🧊 `Task<bool> IsCurrentlyAudibleAsync()`
172+
Whether audio is currently playing.
173+
174+
#### 🧊 `Task<string> GetUserAgentAsync()` / `void SetUserAgent(string userAgent)`
175+
Gets or overrides the user agent for this web page.
176+
165177
#### 🧊 `Task<bool> IsLoadingAsync()`
166178
Whether the web page is still loading resources.
167179

@@ -192,6 +204,9 @@ Copies the image at the given position to the clipboard.
192204
#### 🧊 `void SelectAll()` / `void Unselect()` / `void CenterSelection()`
193205
Selects all content, clears the selection, or scrolls to the current selection.
194206

207+
#### 🧊 `void ScrollToTop()` / `void ScrollToBottom()`
208+
Scrolls to the top or the bottom of the current web page.
209+
195210
#### 🧊 `void AdjustSelection(AdjustSelectionOptions options)`
196211
Adjusts the start and end points of the current text selection by the given amounts. Negative amounts move towards the beginning of the document.
197212

@@ -250,6 +265,15 @@ Emitted when the user changes the zoom level using the mouse wheel or the keyboa
250265
#### ⚡ `OnFoundInPage`
251266
Emitted when a result is available for a `FindInPageAsync` request. The handler receives a `FoundInPageResult`.
252267

268+
#### ⚡ `OnAudioStateChanged`
269+
Emitted when media becomes audible or inaudible. The handler receives `true` if one or more frames or child web contents are emitting audio.
270+
271+
#### ⚡ `OnMediaStartedPlaying`
272+
Emitted when media starts playing.
273+
274+
#### ⚡ `OnMediaPaused`
275+
Emitted when media is paused or done playing.
276+
253277
## Usage Examples
254278

255279
### Page Loading
@@ -446,6 +470,23 @@ webContents.OnFoundInPage += (result) =>
446470
var requestId = await webContents.FindInPageAsync("electron", new FindInPageOptions { MatchCase = false });
447471
```
448472

473+
### Audio
474+
475+
```csharp
476+
webContents.SetAudioMuted(true);
477+
478+
var muted = await webContents.IsAudioMutedAsync();
479+
var audible = await webContents.IsCurrentlyAudibleAsync();
480+
481+
webContents.OnAudioStateChanged += (isAudible) =>
482+
{
483+
Console.WriteLine(isAudible ? "Page started emitting audio" : "Page went silent");
484+
};
485+
486+
webContents.OnMediaStartedPlaying += () => Console.WriteLine("Media playing");
487+
webContents.OnMediaPaused += () => Console.WriteLine("Media paused");
488+
```
489+
449490
## Related APIs
450491

451492
- [Electron.WindowManager](WindowManager.md) - Windows containing web contents

‎src/ElectronNET.API/API/WebContents.cs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ public event Action<FoundInPageResult> OnFoundInPage
131131
remove => RemoveEvent(value, Id);
132132
}
133133

134+
/// <summary>
135+
/// Emitted when media becomes audible or inaudible. The parameter is true if one or more
136+
/// frames or child web contents are emitting audio.
137+
/// </summary>
138+
public event Action<bool> OnAudioStateChanged
139+
{
140+
add => AddEvent(value, Id);
141+
remove => RemoveEvent(value, Id);
142+
}
143+
144+
/// <summary>
145+
/// Emitted when media starts playing.
146+
/// </summary>
147+
public event Action OnMediaStartedPlaying
148+
{
149+
add => AddEvent(value, Id);
150+
remove => RemoveEvent(value, Id);
151+
}
152+
153+
/// <summary>
154+
/// Emitted when media is paused or done playing.
155+
/// </summary>
156+
public event Action OnMediaPaused
157+
{
158+
add => AddEvent(value, Id);
159+
remove => RemoveEvent(value, Id);
160+
}
161+
134162
internal WebContents(int id)
135163
{
136164
Id = id;
@@ -611,6 +639,22 @@ public void CenterSelection()
611639
BridgeConnector.Socket.Emit("webContents-centerSelection", Id);
612640
}
613641

642+
/// <summary>
643+
/// Scrolls to the top of the current web page.
644+
/// </summary>
645+
public void ScrollToTop()
646+
{
647+
BridgeConnector.Socket.Emit("webContents-scrollToTop", Id);
648+
}
649+
650+
/// <summary>
651+
/// Scrolls to the bottom of the current web page.
652+
/// </summary>
653+
public void ScrollToBottom()
654+
{
655+
BridgeConnector.Socket.Emit("webContents-scrollToBottom", Id);
656+
}
657+
614658
/// <summary>
615659
/// Inserts text to the focused element.
616660
/// </summary>

‎src/ElectronNET.Host/api/webContents.js‎

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/ElectronNET.Host/api/webContents.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/ElectronNET.Host/api/webContents.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,33 @@ export = (socket: Socket) => {
121121
});
122122
});
123123

124+
socket.on("register-webContents-audioStateChanged", (id) => {
125+
const browserWindow = getWindowById(id);
126+
127+
browserWindow.webContents.removeAllListeners("audio-state-changed");
128+
browserWindow.webContents.on("audio-state-changed", (event) => {
129+
electronSocket.emit("webContents-audioStateChanged" + id, event.audible);
130+
});
131+
});
132+
133+
socket.on("register-webContents-mediaStartedPlaying", (id) => {
134+
const browserWindow = getWindowById(id);
135+
136+
browserWindow.webContents.removeAllListeners("media-started-playing");
137+
browserWindow.webContents.on("media-started-playing", () => {
138+
electronSocket.emit("webContents-mediaStartedPlaying" + id);
139+
});
140+
});
141+
142+
socket.on("register-webContents-mediaPaused", (id) => {
143+
const browserWindow = getWindowById(id);
144+
145+
browserWindow.webContents.removeAllListeners("media-paused");
146+
browserWindow.webContents.on("media-paused", () => {
147+
electronSocket.emit("webContents-mediaPaused" + id);
148+
});
149+
});
150+
124151
socket.on("webContents-openDevTools", (id, options) => {
125152
if (options) {
126153
getWindowById(id).webContents.openDevTools(options);
@@ -682,6 +709,14 @@ export = (socket: Socket) => {
682709
getWindowById(id).webContents.centerSelection();
683710
});
684711

712+
socket.on("webContents-scrollToTop", (id) => {
713+
getWindowById(id).webContents.scrollToTop();
714+
});
715+
716+
socket.on("webContents-scrollToBottom", (id) => {
717+
getWindowById(id).webContents.scrollToBottom();
718+
});
719+
685720
socket.on("webContents-insertText", async (id, text) => {
686721
await getWindowById(id).webContents.insertText(text);
687722
electronSocket.emit("webContents-insertText-completed" + id);

0 commit comments

Comments
 (0)