Added more missing APIs

This commit is contained in:
Florian Rappl
2026-09-04 16:51:51 +02:00
parent 98c72af3d6
commit fffd7dfb79
12 changed files with 831 additions and 1 deletions

View File

@@ -0,0 +1,18 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Options for <see cref="WebContents.AdjustSelection"/>.
/// </summary>
public class AdjustSelectionOptions
{
/// <summary>
/// Amount to shift the start index of the current selection.
/// </summary>
public int? Start { get; set; }
/// <summary>
/// Amount to shift the end index of the current selection.
/// </summary>
public int? End { get; set; }
}
}

View File

@@ -0,0 +1,24 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Options for <see cref="WebContents.FindInPageAsync"/>.
/// </summary>
public class FindInPageOptions
{
/// <summary>
/// Whether to search forward or backward, defaults to true.
/// </summary>
public bool? Forward { get; set; }
/// <summary>
/// Whether to begin a new text finding session with this request. Should be true
/// for initial requests, and false for subsequent requests. Defaults to false.
/// </summary>
public bool? FindNext { get; set; }
/// <summary>
/// Whether search should be case-sensitive, defaults to false.
/// </summary>
public bool? MatchCase { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// The result of a text finding session, reported by the found-in-page event.
/// </summary>
public class FoundInPageResult
{
/// <summary>
/// The identifier of the request returned by FindInPageAsync.
/// </summary>
public int RequestId { get; set; }
/// <summary>
/// Position of the active match.
/// </summary>
public int ActiveMatchOrdinal { get; set; }
/// <summary>
/// Number of matches.
/// </summary>
public int Matches { get; set; }
/// <summary>
/// Coordinates of the first match region.
/// </summary>
public Rectangle SelectionArea { get; set; }
/// <summary>
/// Indicates whether more responses are to follow.
/// </summary>
public bool FinalUpdate { get; set; }
}
}

View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace ElectronNET.API.Entities
{
/// <summary>
/// Options for loading a local file into a web page.
/// </summary>
public class LoadFileOptions
{
/// <summary>
/// Passed to url.format().
/// </summary>
public Dictionary<string, string> Query { get; set; }
/// <summary>
/// Passed to url.format().
/// </summary>
public string Search { get; set; }
/// <summary>
/// Passed to url.format().
/// </summary>
public string Hash { get; set; }
}
}

View File

@@ -0,0 +1,23 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Determines what happens to the selection when a text finding session is stopped.
/// </summary>
public enum StopFindInPageAction
{
/// <summary>
/// Clear the selection.
/// </summary>
ClearSelection,
/// <summary>
/// Translate the selection into a normal selection.
/// </summary>
KeepSelection,
/// <summary>
/// Focus and click the selection node.
/// </summary>
ActivateSelection
}
}

View File

@@ -122,6 +122,15 @@ public class WebContents : ApiBase
remove => RemoveEvent(value, Id);
}
/// <summary>
/// Emitted when a result is available for a <see cref="FindInPageAsync(string, FindInPageOptions)"/> request.
/// </summary>
public event Action<FoundInPageResult> OnFoundInPage
{
add => AddEvent(value, Id);
remove => RemoveEvent(value, Id);
}
internal WebContents(int id)
{
Id = id;
@@ -313,6 +322,68 @@ public class WebContents : ApiBase
return tcs.Task;
}
/// <summary>
/// Loads the given file in the window. The file path must be a path to an HTML file
/// relative to the root of your application.
/// </summary>
/// <param name="filePath">Path to the HTML file.</param>
/// <param name="options">Optional query, search and hash parts of the resulting URL.</param>
public Task LoadFileAsync(string filePath, LoadFileOptions options = null)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("webContents-loadFile-complete" + Id, () =>
{
BridgeConnector.Socket.Off("webContents-loadFile-error" + Id);
tcs.SetResult();
});
BridgeConnector.Socket.Once<string>("webContents-loadFile-error" + Id, (error) => { tcs.SetException(new InvalidOperationException(error)); });
BridgeConnector.Socket.Emit("webContents-loadFile", Id, filePath, options);
return tcs.Task;
}
/// <summary>
/// Returns boolean - Whether web page is still loading resources.
/// </summary>
public Task<bool> IsLoadingAsync() => InvokeAsync<bool>();
/// <summary>
/// Returns boolean - Whether the main frame (and not just iframes or frames within it) is still loading.
/// </summary>
public Task<bool> IsLoadingMainFrameAsync() => InvokeAsync<bool>();
/// <summary>
/// Returns boolean - Whether the web page is waiting for a first-response from the main resource of the page.
/// </summary>
public Task<bool> IsWaitingForResponseAsync() => InvokeAsync<bool>();
/// <summary>
/// Reloads the current web page.
/// </summary>
public void Reload()
{
BridgeConnector.Socket.Emit("webContents-reload", Id);
}
/// <summary>
/// Reloads the current page and ignores cache.
/// </summary>
public void ReloadIgnoringCache()
{
BridgeConnector.Socket.Emit("webContents-reloadIgnoringCache", Id);
}
/// <summary>
/// Stops any pending navigation.
/// </summary>
public void Stop()
{
BridgeConnector.Socket.Emit("webContents-stop", Id);
}
/// <summary>
/// Inserts CSS into the web page.
/// See: https://www.electronjs.org/docs/api/web-contents#contentsinsertcsscss-options
@@ -325,6 +396,37 @@ public class WebContents : ApiBase
BridgeConnector.Socket.Emit("webContents-insertCSS", Id, isBrowserWindow, path);
}
/// <summary>
/// Injects CSS into the current web page and returns a unique key for the inserted
/// stylesheet, which can be used with <see cref="RemoveInsertedCSSAsync"/>.
/// </summary>
/// <param name="css">The style sheet to inject.</param>
/// <param name="cssOrigin">Can be either 'user' or 'author'. Defaults to 'author'.</param>
/// <returns>The key of the inserted style sheet.</returns>
public Task<string> InsertCSSAsync(string css, string cssOrigin = null)
{
var tcs = new TaskCompletionSource<string>();
BridgeConnector.Socket.Once<string>("webContents-insertCSSText-completed" + Id, tcs.SetResult);
BridgeConnector.Socket.Emit("webContents-insertCSSText", Id, css, cssOrigin);
return tcs.Task;
}
/// <summary>
/// Removes the inserted CSS from the current web page.
/// </summary>
/// <param name="key">The key returned by <see cref="InsertCSSAsync"/>.</param>
public Task RemoveInsertedCSSAsync(string key)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("webContents-removeInsertedCSS-completed" + Id, tcs.SetResult);
BridgeConnector.Socket.Emit("webContents-removeInsertedCSS", Id, key);
return tcs.Task;
}
/// <summary>
/// Returns number - The current zoom factor.
/// </summary>
@@ -408,4 +510,162 @@ public class WebContents : ApiBase
{
BridgeConnector.Socket.Emit("webContents-setUserAgent", Id, userAgent);
}
/// <summary>
/// Executes the editing command undo in web page.
/// </summary>
public void Undo()
{
BridgeConnector.Socket.Emit("webContents-undo", Id);
}
/// <summary>
/// Executes the editing command redo in web page.
/// </summary>
public void Redo()
{
BridgeConnector.Socket.Emit("webContents-redo", Id);
}
/// <summary>
/// Executes the editing command cut in web page.
/// </summary>
public void Cut()
{
BridgeConnector.Socket.Emit("webContents-cut", Id);
}
/// <summary>
/// Executes the editing command copy in web page.
/// </summary>
public void Copy()
{
BridgeConnector.Socket.Emit("webContents-copy", Id);
}
/// <summary>
/// Copies the image at the given position to the clipboard.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void CopyImageAt(int x, int y)
{
BridgeConnector.Socket.Emit("webContents-copyImageAt", Id, x, y);
}
/// <summary>
/// Executes the editing command paste in web page.
/// </summary>
public void Paste()
{
BridgeConnector.Socket.Emit("webContents-paste", Id);
}
/// <summary>
/// Executes the editing command pasteAndMatchStyle in web page.
/// </summary>
public void PasteAndMatchStyle()
{
BridgeConnector.Socket.Emit("webContents-pasteAndMatchStyle", Id);
}
/// <summary>
/// Executes the editing command delete in web page.
/// </summary>
public void Delete()
{
BridgeConnector.Socket.Emit("webContents-delete", Id);
}
/// <summary>
/// Executes the editing command selectAll in web page.
/// </summary>
public void SelectAll()
{
BridgeConnector.Socket.Emit("webContents-selectAll", Id);
}
/// <summary>
/// Executes the editing command unselect in web page.
/// </summary>
public void Unselect()
{
BridgeConnector.Socket.Emit("webContents-unselect", Id);
}
/// <summary>
/// Adjusts the current text selection starting and ending points by the given amounts.
/// A negative amount moves the selection towards the beginning of the document.
/// </summary>
/// <param name="options"></param>
public void AdjustSelection(AdjustSelectionOptions options)
{
BridgeConnector.Socket.Emit("webContents-adjustSelection", Id, options);
}
/// <summary>
/// Scrolls to the current text selection.
/// </summary>
public void CenterSelection()
{
BridgeConnector.Socket.Emit("webContents-centerSelection", Id);
}
/// <summary>
/// Inserts text to the focused element.
/// </summary>
/// <param name="text">The text to be inserted.</param>
public Task InsertTextAsync(string text)
{
var tcs = new TaskCompletionSource();
BridgeConnector.Socket.Once("webContents-insertText-completed" + Id, tcs.SetResult);
BridgeConnector.Socket.Emit("webContents-insertText", Id, text);
return tcs.Task;
}
/// <summary>
/// Executes the editing command replace in web page.
/// </summary>
/// <param name="text"></param>
public void Replace(string text)
{
BridgeConnector.Socket.Emit("webContents-replace", Id, text);
}
/// <summary>
/// Executes the editing command replaceMisspelling in web page.
/// </summary>
/// <param name="text"></param>
public void ReplaceMisspelling(string text)
{
BridgeConnector.Socket.Emit("webContents-replaceMisspelling", Id, text);
}
/// <summary>
/// Starts a request to find all matches for the text in the web page.
/// The result of the request can be obtained by subscribing to the <see cref="OnFoundInPage"/> event.
/// </summary>
/// <param name="text">Content to be searched, must not be empty.</param>
/// <param name="options"></param>
/// <returns>The request id used for the request.</returns>
public Task<int> FindInPageAsync(string text, FindInPageOptions options = null)
{
var tcs = new TaskCompletionSource<int>();
BridgeConnector.Socket.Once<int>("webContents-findInPage-completed" + Id, tcs.SetResult);
BridgeConnector.Socket.Emit("webContents-findInPage", Id, text, options);
return tcs.Task;
}
/// <summary>
/// Stops any findInPage request for the web contents with the provided action.
/// </summary>
/// <param name="action"></param>
public void StopFindInPage(StopFindInPageAction action)
{
BridgeConnector.Socket.Emit("webContents-stopFindInPage", Id, action);
}
}