Skip to content

Commit fffd7df

Browse files
committed
Added more missing APIs
1 parent 98c72af commit fffd7df

12 files changed

Lines changed: 831 additions & 1 deletion

File tree

‎Changelog.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
- Fixed resolution of unrelated target (#1099) @epsnm
1414
- Fixed false alarm for `ELECTRON001` on a root `package-lock.json` (#946)
1515
- Added `WebContents.OnZoomChanged` event (#956)
16+
- Added `WebContents` page loading APIs `LoadFileAsync`, `IsLoadingAsync`, `IsLoadingMainFrameAsync`, `IsWaitingForResponseAsync`, `Reload`, `ReloadIgnoringCache` and `Stop` (#956)
17+
- Added `WebContents.InsertCSSAsync` and `WebContents.RemoveInsertedCSSAsync` for dynamic CSS (#956)
18+
- Added `WebContents` editing and selection APIs (undo, redo, cut, copy, paste, insert text, select, ...) (#956)
19+
- Added `WebContents.FindInPageAsync`, `WebContents.StopFindInPage` and the `OnFoundInPage` event (#956)
1620
- Added target framework customization (#1095) @epsnm
1721
- Added configurable Electron root directory for custom packaging layouts (#1106) @DYH1319
1822
- Added ability for `custom_main.js` to modify command line switches (#1029) @AeonSake

‎docs/API/WebContents.md‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ Works for both BrowserWindows and BrowserViews.
5858
- `isBrowserWindow` - Whether the webContents belong to a BrowserWindow or not (the other option is a BrowserView)
5959
- `path` - Absolute path to the CSS file location
6060

61+
#### 🧊 `Task<string> InsertCSSAsync(string css, string cssOrigin = null)`
62+
Injects CSS into the current web page and returns a unique key for the inserted style sheet.
63+
64+
**Parameters:**
65+
- `css` - The style sheet to inject
66+
- `cssOrigin` - Can be either `user` or `author`, defaults to `author`
67+
68+
**Returns:**
69+
70+
The key of the inserted style sheet, to be used with `RemoveInsertedCSSAsync`.
71+
72+
#### 🧊 `Task RemoveInsertedCSSAsync(string key)`
73+
Removes a previously inserted style sheet from the current web page.
74+
75+
**Parameters:**
76+
- `key` - The key returned by `InsertCSSAsync`
77+
6178
#### 🧊 `Task LoadURLAsync(string url)`
6279
Loads the url in the window. The url must contain the protocol prefix.
6380

@@ -138,6 +155,66 @@ Sets the maximum and minimum pinch-to-zoom level.
138155
- `minimumLevel` - The minimum pinch-to-zoom level
139156
- `maximumLevel` - The maximum pinch-to-zoom level
140157

158+
#### 🧊 `Task LoadFileAsync(string filePath, LoadFileOptions options = null)`
159+
Loads the given HTML file, relative to the root of the application.
160+
161+
**Parameters:**
162+
- `filePath` - Path to the HTML file
163+
- `options` - Optional `Query`, `Search` and `Hash` parts of the resulting URL
164+
165+
#### 🧊 `Task<bool> IsLoadingAsync()`
166+
Whether the web page is still loading resources.
167+
168+
#### 🧊 `Task<bool> IsLoadingMainFrameAsync()`
169+
Whether the main frame (and not just iframes or frames within it) is still loading.
170+
171+
#### 🧊 `Task<bool> IsWaitingForResponseAsync()`
172+
Whether the web page is waiting for a first response from the main resource of the page.
173+
174+
#### 🧊 `void Reload()`
175+
Reloads the current web page.
176+
177+
#### 🧊 `void ReloadIgnoringCache()`
178+
Reloads the current web page and ignores the cache.
179+
180+
#### 🧊 `void Stop()`
181+
Stops any pending navigation.
182+
183+
#### 🧊 `void Undo()` / `void Redo()`
184+
Executes the `undo` / `redo` editing command in the web page.
185+
186+
#### 🧊 `void Cut()` / `void Copy()` / `void Paste()` / `void PasteAndMatchStyle()` / `void Delete()`
187+
Executes the corresponding editing command in the web page.
188+
189+
#### 🧊 `void CopyImageAt(int x, int y)`
190+
Copies the image at the given position to the clipboard.
191+
192+
#### 🧊 `void SelectAll()` / `void Unselect()` / `void CenterSelection()`
193+
Selects all content, clears the selection, or scrolls to the current selection.
194+
195+
#### 🧊 `void AdjustSelection(AdjustSelectionOptions options)`
196+
Adjusts the start and end points of the current text selection by the given amounts. Negative amounts move towards the beginning of the document.
197+
198+
#### 🧊 `Task InsertTextAsync(string text)`
199+
Inserts text into the focused element.
200+
201+
#### 🧊 `void Replace(string text)` / `void ReplaceMisspelling(string text)`
202+
Replaces the current selection, or the currently misspelled word, with the given text.
203+
204+
#### 🧊 `Task<int> FindInPageAsync(string text, FindInPageOptions options = null)`
205+
Starts a request to find all matches for the text in the web page. Results are reported via the `OnFoundInPage` event.
206+
207+
**Parameters:**
208+
- `text` - Content to be searched, must not be empty
209+
- `options` - Optional `Forward`, `FindNext` and `MatchCase` flags
210+
211+
**Returns:**
212+
213+
The request id of the find request.
214+
215+
#### 🧊 `void StopFindInPage(StopFindInPageAction action)`
216+
Stops any `FindInPageAsync` request with the given action (`ClearSelection`, `KeepSelection` or `ActivateSelection`).
217+
141218
## Events
142219

143220
#### ⚡ `InputEvent`
@@ -170,6 +247,9 @@ Emitted when a server side redirect occurs during navigation.
170247
#### ⚡ `OnZoomChanged`
171248
Emitted when the user changes the zoom level using the mouse wheel or the keyboard. The handler receives the `ZoomDirection` (`In` or `Out`).
172249

250+
#### ⚡ `OnFoundInPage`
251+
Emitted when a result is available for a `FindInPageAsync` request. The handler receives a `FoundInPageResult`.
252+
173253
## Usage Examples
174254

175255
### Page Loading
@@ -331,6 +411,41 @@ webContents.OnZoomChanged += (direction) =>
331411

332412
> **Note:** The zoom factor is shared by all windows using the same session partition. Assign a unique `WebPreferences.Partition` per window if each window should keep its own zoom.
333413
414+
### Dynamic CSS
415+
416+
```csharp
417+
var key = await webContents.InsertCSSAsync("body { background-color: #202020; }");
418+
419+
// ... later
420+
await webContents.RemoveInsertedCSSAsync(key);
421+
```
422+
423+
### Editing and Selection
424+
425+
```csharp
426+
await webContents.InsertTextAsync("Hello from .NET");
427+
428+
webContents.SelectAll();
429+
webContents.Copy();
430+
webContents.Unselect();
431+
```
432+
433+
### Find in Page
434+
435+
```csharp
436+
webContents.OnFoundInPage += (result) =>
437+
{
438+
Console.WriteLine($"{result.ActiveMatchOrdinal}/{result.Matches} matches");
439+
440+
if (result.FinalUpdate)
441+
{
442+
webContents.StopFindInPage(StopFindInPageAction.ClearSelection);
443+
}
444+
};
445+
446+
var requestId = await webContents.FindInPageAsync("electron", new FindInPageOptions { MatchCase = false });
447+
```
448+
334449
## Related APIs
335450

336451
- [Electron.WindowManager](WindowManager.md) - Windows containing web contents
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Options for <see cref="WebContents.AdjustSelection"/>.
5+
/// </summary>
6+
public class AdjustSelectionOptions
7+
{
8+
/// <summary>
9+
/// Amount to shift the start index of the current selection.
10+
/// </summary>
11+
public int? Start { get; set; }
12+
13+
/// <summary>
14+
/// Amount to shift the end index of the current selection.
15+
/// </summary>
16+
public int? End { get; set; }
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Options for <see cref="WebContents.FindInPageAsync"/>.
5+
/// </summary>
6+
public class FindInPageOptions
7+
{
8+
/// <summary>
9+
/// Whether to search forward or backward, defaults to true.
10+
/// </summary>
11+
public bool? Forward { get; set; }
12+
13+
/// <summary>
14+
/// Whether to begin a new text finding session with this request. Should be true
15+
/// for initial requests, and false for subsequent requests. Defaults to false.
16+
/// </summary>
17+
public bool? FindNext { get; set; }
18+
19+
/// <summary>
20+
/// Whether search should be case-sensitive, defaults to false.
21+
/// </summary>
22+
public bool? MatchCase { get; set; }
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// The result of a text finding session, reported by the found-in-page event.
5+
/// </summary>
6+
public class FoundInPageResult
7+
{
8+
/// <summary>
9+
/// The identifier of the request returned by FindInPageAsync.
10+
/// </summary>
11+
public int RequestId { get; set; }
12+
13+
/// <summary>
14+
/// Position of the active match.
15+
/// </summary>
16+
public int ActiveMatchOrdinal { get; set; }
17+
18+
/// <summary>
19+
/// Number of matches.
20+
/// </summary>
21+
public int Matches { get; set; }
22+
23+
/// <summary>
24+
/// Coordinates of the first match region.
25+
/// </summary>
26+
public Rectangle SelectionArea { get; set; }
27+
28+
/// <summary>
29+
/// Indicates whether more responses are to follow.
30+
/// </summary>
31+
public bool FinalUpdate { get; set; }
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
namespace ElectronNET.API.Entities
4+
{
5+
/// <summary>
6+
/// Options for loading a local file into a web page.
7+
/// </summary>
8+
public class LoadFileOptions
9+
{
10+
/// <summary>
11+
/// Passed to url.format().
12+
/// </summary>
13+
public Dictionary<string, string> Query { get; set; }
14+
15+
/// <summary>
16+
/// Passed to url.format().
17+
/// </summary>
18+
public string Search { get; set; }
19+
20+
/// <summary>
21+
/// Passed to url.format().
22+
/// </summary>
23+
public string Hash { get; set; }
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Determines what happens to the selection when a text finding session is stopped.
5+
/// </summary>
6+
public enum StopFindInPageAction
7+
{
8+
/// <summary>
9+
/// Clear the selection.
10+
/// </summary>
11+
ClearSelection,
12+
13+
/// <summary>
14+
/// Translate the selection into a normal selection.
15+
/// </summary>
16+
KeepSelection,
17+
18+
/// <summary>
19+
/// Focus and click the selection node.
20+
/// </summary>
21+
ActivateSelection
22+
}
23+
}

0 commit comments

Comments
 (0)