2017-10-21 04:37:01 +02:00
|
|
|
|
using ElectronNET.API.Entities;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using System;
|
2017-10-23 19:08:10 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Render and control web pages.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class WebContents
|
|
|
|
|
|
{
|
2017-10-25 21:28:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the identifier.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <value>
|
|
|
|
|
|
/// The identifier.
|
|
|
|
|
|
/// </value>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
2019-05-16 18:03:31 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manage browser sessions, cookies, cache, proxy settings, etc.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Session Session { get; internal set; }
|
|
|
|
|
|
|
2017-10-21 04:37:01 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Emitted when the renderer process crashes or is killed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event Action<bool> OnCrashed
|
|
|
|
|
|
{
|
|
|
|
|
|
add
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_crashed == null)
|
|
|
|
|
|
{
|
2021-07-12 21:27:40 +02:00
|
|
|
|
BridgeConnector.On<bool>("webContents-crashed" + Id, (killed) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 21:33:35 +02:00
|
|
|
|
_crashed(killed);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("register-webContents-crashed", Id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
_crashed += value;
|
|
|
|
|
|
}
|
|
|
|
|
|
remove
|
|
|
|
|
|
{
|
|
|
|
|
|
_crashed -= value;
|
2017-10-25 21:28:43 +02:00
|
|
|
|
|
|
|
|
|
|
if (_crashed == null)
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("webContents-crashed" + Id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private event Action<bool> _crashed;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Emitted when the navigation is done, i.e. the spinner of the tab has
|
|
|
|
|
|
/// stopped spinning, and the onload event was dispatched.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event Action OnDidFinishLoad
|
|
|
|
|
|
{
|
|
|
|
|
|
add
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_didFinishLoad == null)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.On("webContents-didFinishLoad" + Id, () =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
_didFinishLoad();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("register-webContents-didFinishLoad", Id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
_didFinishLoad += value;
|
|
|
|
|
|
}
|
|
|
|
|
|
remove
|
|
|
|
|
|
{
|
|
|
|
|
|
_didFinishLoad -= value;
|
2017-10-25 21:28:43 +02:00
|
|
|
|
|
|
|
|
|
|
if (_didFinishLoad == null)
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("webContents-didFinishLoad" + Id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private event Action _didFinishLoad;
|
|
|
|
|
|
|
|
|
|
|
|
internal WebContents(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = id;
|
2019-05-16 18:03:31 +02:00
|
|
|
|
Session = new Session(id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Opens the devtools.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void OpenDevTools()
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("webContentsOpenDevTools", Id);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Opens the devtools.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="openDevToolsOptions"></param>
|
|
|
|
|
|
public void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)
|
|
|
|
|
|
{
|
2021-08-23 11:57:42 +02:00
|
|
|
|
BridgeConnector.Emit("webContentsOpenDevTools", Id, openDevToolsOptions);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-12-15 09:56:14 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get system printers.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>printers</returns>
|
|
|
|
|
|
public Task<PrinterInfo[]> GetPrintersAsync()
|
|
|
|
|
|
{
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<PrinterInfo[]>("webContents-getPrinters", "webContents-getPrinters-completed", Id);
|
2019-12-15 09:56:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Prints window's web page.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns>success</returns>
|
2021-08-23 11:57:42 +02:00
|
|
|
|
public Task<bool> PrintAsync(PrintOptions options = null) => options is null ? BridgeConnector.OnResult<bool>("webContents-print", "webContents-print-completed", Id, "")
|
|
|
|
|
|
: BridgeConnector.OnResult<bool>("webContents-print", "webContents-print-completed", Id, options);
|
2019-12-15 09:56:14 +01:00
|
|
|
|
|
2017-10-23 19:08:10 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path"></param>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
/// <returns>success</returns>
|
2021-08-23 11:57:42 +02:00
|
|
|
|
public Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null) => options is null ? BridgeConnector.OnResult<bool>("webContents-printToPDF", "webContents-printToPDF-completed", Id, "", path)
|
|
|
|
|
|
: BridgeConnector.OnResult<bool>("webContents-printToPDF", "webContents-printToPDF-completed", Id, options, path);
|
2017-10-23 19:08:10 +02:00
|
|
|
|
|
2018-12-30 23:18:18 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Is used to get the Url of the loaded page.
|
|
|
|
|
|
/// It's usefull if a web-server redirects you and you need to know where it redirects. For instance, It's useful in case of Implicit Authorization.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>URL of the loaded page</returns>
|
2018-03-16 22:32:56 +03:00
|
|
|
|
public Task<string> GetUrl()
|
|
|
|
|
|
{
|
2021-08-18 10:24:12 +02:00
|
|
|
|
return BridgeConnector.OnResult<string>("webContents-getUrl", "webContents-getUrl" + Id, Id);
|
2018-03-16 22:32:56 +03:00
|
|
|
|
}
|
2020-04-23 03:29:52 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The async method will resolve when the page has finished loading,
|
|
|
|
|
|
/// and rejects if the page fails to load.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// A noop rejection handler is already attached, which avoids unhandled rejection
|
|
|
|
|
|
/// errors.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Loads the `url` in the window. The `url` must contain the protocol prefix, e.g.
|
|
|
|
|
|
/// the `http://` or `file://`. If the load should bypass http cache then use the
|
|
|
|
|
|
/// `pragma` header to achieve it.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
|
public Task LoadURLAsync(string url)
|
|
|
|
|
|
{
|
|
|
|
|
|
return LoadURLAsync(url, new LoadURLOptions());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The async method will resolve when the page has finished loading,
|
|
|
|
|
|
/// and rejects if the page fails to load.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// A noop rejection handler is already attached, which avoids unhandled rejection
|
|
|
|
|
|
/// errors.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Loads the `url` in the window. The `url` must contain the protocol prefix, e.g.
|
|
|
|
|
|
/// the `http://` or `file://`. If the load should bypass http cache then use the
|
|
|
|
|
|
/// `pragma` header to achieve it.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
|
/// <param name="options"></param>
|
|
|
|
|
|
public Task LoadURLAsync(string url, LoadURLOptions options)
|
2021-08-18 10:24:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
2020-04-23 03:29:52 +02:00
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.On("webContents-loadURL-complete" + Id, () =>
|
2020-04-23 03:29:52 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("webContents-loadURL-complete" + Id);
|
|
|
|
|
|
BridgeConnector.Off("webContents-loadURL-error" + Id);
|
2021-08-18 10:24:12 +02:00
|
|
|
|
taskCompletionSource.SetResult();
|
2020-04-23 03:29:52 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 21:27:40 +02:00
|
|
|
|
BridgeConnector.On<string>("webContents-loadURL-error" + Id, (error) =>
|
2020-04-23 03:29:52 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("webContents-loadURL-error" + Id);
|
2021-08-18 10:24:12 +02:00
|
|
|
|
BridgeConnector.Off("webContents-loadURL-complete" + Id);
|
2020-04-23 03:29:52 +02:00
|
|
|
|
taskCompletionSource.SetException(new InvalidOperationException(error.ToString()));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2021-08-23 11:57:42 +02:00
|
|
|
|
BridgeConnector.Emit("webContents-loadURL", Id, url, options);
|
2020-04-23 03:29:52 +02:00
|
|
|
|
|
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-26 12:41:14 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Inserts CSS into the web page.
|
|
|
|
|
|
/// See: https://www.electronjs.org/docs/api/web-contents#contentsinsertcsscss-options
|
|
|
|
|
|
/// Works for both BrowserWindows and BrowserViews.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="isBrowserWindow">Whether the webContents belong to a BrowserWindow or not (the other option is a BrowserView)</param>
|
|
|
|
|
|
/// <param name="path">Absolute path to the CSS file location</param>
|
|
|
|
|
|
public void InsertCSS(bool isBrowserWindow, string path)
|
|
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("webContents-insertCSS", Id, isBrowserWindow, path);
|
2021-04-26 12:41:14 -04:00
|
|
|
|
}
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|