using ElectronNET.API.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Threading.Tasks;
using ElectronNET.Common;
// ReSharper disable InconsistentNaming
namespace ElectronNET.API;
///
/// Render and control web pages.
///
public class WebContents
{
///
/// Gets the identifier.
///
///
/// The identifier.
///
public int Id { get; private set; }
///
/// Manage browser sessions, cookies, cache, proxy settings, etc.
///
public Session Session { get; internal set; }
///
/// Emitted when the renderer process crashes or is killed.
///
public event Action OnCrashed
{
add => ApiEventManager.AddEvent("webContents-crashed", Id, _crashed, value, (args) => (bool)args);
remove => ApiEventManager.RemoveEvent("webContents-crashed", Id, _crashed, value);
}
private event Action _crashed;
///
/// Emitted when the navigation is done, i.e. the spinner of the tab has
/// stopped spinning, and the onload event was dispatched.
///
public event Action OnDidFinishLoad
{
add => ApiEventManager.AddEvent("webContents-didFinishLoad", Id, _didFinishLoad, value);
remove => ApiEventManager.RemoveEvent("webContents-didFinishLoad", Id, _didFinishLoad, value);
}
private event Action _didFinishLoad;
///
/// Emitted when any frame (including main) starts navigating.
///
public event Action OnDidStartNavigation
{
add => ApiEventManager.AddEvent("webContents-didStartNavigation", Id, _didStartNavigation, value);
remove => ApiEventManager.RemoveEvent("webContents-didStartNavigation", Id, _didStartNavigation, value);
}
private event Action _didStartNavigation;
///
/// Emitted when a main frame navigation is done.
/// This event is not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.
///
public event Action OnDidNavigate
{
add => ApiEventManager.AddEvent("webContents-didNavigate", Id, _didNavigate, value);
remove => ApiEventManager.RemoveEvent("webContents-didNavigate", Id, _didNavigate, value);
}
private event Action _didNavigate;
///
/// Emitted when a server side redirect occurs during navigation. For example a 302 redirect.
/// This event will be emitted after OnDidStartNavigation and always before the OnDidRedirectNavigation event for the same navigation.
///
public event Action OnWillRedirect
{
add => ApiEventManager.AddEvent("webContents-willRedirect", Id, _willRedirect, value);
remove => ApiEventManager.RemoveEvent("webContents-willRedirect", Id, _willRedirect, value);
}
private event Action _willRedirect;
///
/// Emitted after a server side redirect occurs during navigation. For example a 302 redirect.
///
public event Action OnDidRedirectNavigation
{
add => ApiEventManager.AddEvent("webContents-didRedirectNavigation", Id, _didRedirectNavigation, value);
remove => ApiEventManager.RemoveEvent("webContents-didRedirectNavigation", Id, _didRedirectNavigation, value);
}
private event Action _didRedirectNavigation;
///
/// This event is like OnDidFinishLoad but emitted when the load failed.
///
public event Action OnDidFailLoad
{
add => ApiEventManager.AddEvent("webContents-didFailLoad", Id, _didFailLoad, value, (args) => ((JObject)args).ToObject());
remove => ApiEventManager.RemoveEvent("webContents-didFailLoad", Id, _didFailLoad, value);
}
private event Action _didFailLoad;
///
/// Emitted when an input event is sent to the WebContents.
///
public event Action InputEvent
{
add => ApiEventManager.AddEvent("webContents-input-event", Id, _inputEvent, value, (args) => ((JObject)args).ToObject());
remove => ApiEventManager.RemoveEvent("webContents-input-event", Id, _inputEvent, value);
}
private event Action _inputEvent;
///
/// Emitted when the document in the top-level frame is loaded.
///
public event Action OnDomReady
{
add => ApiEventManager.AddEvent("webContents-domReady", Id, _domReady, value);
remove => ApiEventManager.RemoveEvent("webContents-domReady", Id, _domReady, value);
}
private event Action _domReady;
internal WebContents(int id)
{
Id = id;
Session = new Session(id);
}
///
/// Opens the devtools.
///
public void OpenDevTools()
{
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id);
}
///
/// Opens the devtools.
///
///
public void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)
{
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id, JObject.FromObject(openDevToolsOptions, _jsonSerializer));
}
///
/// Get system printers.
///
/// printers
public Task GetPrintersAsync()
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("webContents-getPrinters-completed", (printers) =>
{
BridgeConnector.Socket.Off("webContents-getPrinters-completed");
taskCompletionSource.SetResult(((Newtonsoft.Json.Linq.JArray)printers).ToObject());
});
BridgeConnector.Socket.Emit("webContents-getPrinters", Id);
return taskCompletionSource.Task;
}
///
/// Prints window's web page.
///
///
/// success
public Task PrintAsync(PrintOptions options = null)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("webContents-print-completed", (success) =>
{
BridgeConnector.Socket.Off("webContents-print-completed");
taskCompletionSource.SetResult((bool)success);
});
if (options == null)
{
BridgeConnector.Socket.Emit("webContents-print", Id, "");
}
else
{
BridgeConnector.Socket.Emit("webContents-print", Id, JObject.FromObject(options, _jsonSerializer));
}
return taskCompletionSource.Task;
}
///
/// Prints window's web page as PDF with Chromium's preview printing custom
/// settings.The landscape will be ignored if @page CSS at-rule is used in the web page.
/// By default, an empty options will be regarded as: Use page-break-before: always;
/// CSS style to force to print to a new page.
///
///
///
/// success
public Task PrintToPDFAsync(string path, PrintToPDFOptions options = null)
{
var taskCompletionSource = new TaskCompletionSource();
BridgeConnector.Socket.On("webContents-printToPDF-completed", (success) =>
{
BridgeConnector.Socket.Off("webContents-printToPDF-completed");
taskCompletionSource.SetResult((bool)success);
});
if (options == null)
{
BridgeConnector.Socket.Emit("webContents-printToPDF", Id, "", path);
}
else
{
BridgeConnector.Socket.Emit("webContents-printToPDF", Id, JObject.FromObject(options, _jsonSerializer), path);
}
return taskCompletionSource.Task;
}
///
/// Evaluates script code in page.
///
/// The code to execute.
/// if set to true simulate a user gesture.
/// The result of the executed code.
///
///
/// In the browser window some HTML APIs like `requestFullScreen` can only be
/// invoked by a gesture from the user. Setting `userGesture` to `true` will remove
/// this limitation.
///
///
/// Code execution will be suspended until web page stop loading.
///
///
public Task