mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-02-04 05:34:51 +00:00
implement Session-API, fix code selection problem for the Demo Web-App.
This commit is contained in:
24
ElectronNET.API/Entities/ClearStorageDataOptions.cs
Normal file
24
ElectronNET.API/Entities/ClearStorageDataOptions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class ClearStorageDataOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Should follow window.location.origin’s representation scheme://host:port.
|
||||
/// </summary>
|
||||
public string Origin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The types of storages to clear, can contain: appcache, cookies, filesystem,
|
||||
/// indexdb, localstorage, shadercache, websql, serviceworkers, cachestorage.
|
||||
/// </summary>
|
||||
public string[] Storages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The types of quotas to clear, can contain: temporary, persistent, syncable.
|
||||
/// </summary>
|
||||
public string[] Quotas { get; set; }
|
||||
}
|
||||
}
|
||||
67
ElectronNET.API/Entities/CreateInterruptedDownloadOptions.cs
Normal file
67
ElectronNET.API/Entities/CreateInterruptedDownloadOptions.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class CreateInterruptedDownloadOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Absolute path of the download.
|
||||
/// </summary>
|
||||
public string Path { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Complete URL chain for the download.
|
||||
/// </summary>
|
||||
public string[] UrlChain { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string MimeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Start range for the download.
|
||||
/// </summary>
|
||||
public int Offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Total length of the download.
|
||||
/// </summary>
|
||||
public int Length { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Last-Modified header value.
|
||||
/// </summary>
|
||||
public string LastModified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// ETag header value.
|
||||
/// </summary>
|
||||
public string ETag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Time when download was started in number of seconds since UNIX epoch.
|
||||
/// </summary>
|
||||
public int StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="path">Absolute path of the download.</param>
|
||||
/// <param name="urlChain">Complete URL chain for the download.</param>
|
||||
/// <param name="offset">Start range for the download.</param>
|
||||
/// <param name="length">Total length of the download.</param>
|
||||
/// <param name="lastModified">Last-Modified header value.</param>
|
||||
/// <param name="eTag">ETag header value.</param>
|
||||
public CreateInterruptedDownloadOptions(string path, string[] urlChain, int offset, int length, string lastModified, string eTag)
|
||||
{
|
||||
Path = path;
|
||||
UrlChain = urlChain;
|
||||
Offset = offset;
|
||||
Length = length;
|
||||
LastModified = lastModified;
|
||||
ETag = eTag;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
ElectronNET.API/Entities/EnableNetworkEmulationOptions.cs
Normal file
32
ElectronNET.API/Entities/EnableNetworkEmulationOptions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class EnableNetworkEmulationOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether to emulate network outage. Defaults to false.
|
||||
/// </summary>
|
||||
public bool Offline { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// RTT in ms. Defaults to 0 which will disable latency throttling.
|
||||
/// </summary>
|
||||
public int Latency { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Download rate in Bps. Defaults to 0 which will disable download throttling.
|
||||
/// </summary>
|
||||
public int DownloadThroughput { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Upload rate in Bps. Defaults to 0 which will disable upload throttling.
|
||||
/// </summary>
|
||||
public int UploadThroughput { get; set; }
|
||||
}
|
||||
}
|
||||
40
ElectronNET.API/Entities/ProxyConfig.cs
Normal file
40
ElectronNET.API/Entities/ProxyConfig.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class ProxyConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// The URL associated with the PAC file.
|
||||
/// </summary>
|
||||
public string PacScript { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Rules indicating which proxies to use.
|
||||
/// </summary>
|
||||
public string ProxyRules { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Rules indicating which URLs should bypass the proxy settings.
|
||||
/// </summary>
|
||||
public string ProxyBypassRules { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="pacScript">The URL associated with the PAC file.</param>
|
||||
/// <param name="proxyRules">Rules indicating which proxies to use.</param>
|
||||
/// <param name="proxyBypassRules">Rules indicating which URLs should bypass the proxy settings.</param>
|
||||
public ProxyConfig(string pacScript, string proxyRules, string proxyBypassRules)
|
||||
{
|
||||
PacScript = pacScript;
|
||||
ProxyRules = proxyRules;
|
||||
ProxyBypassRules = proxyBypassRules;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
ElectronNET.API/Entities/RemoveClientCertificate.cs
Normal file
31
ElectronNET.API/Entities/RemoveClientCertificate.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class RemoveClientCertificate
|
||||
{
|
||||
/// <summary>
|
||||
/// Origin of the server whose associated client certificate must be removed from
|
||||
/// the cache.
|
||||
/// </summary>
|
||||
public string Origin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// clientCertificate.
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="origin">Origin of the server whose associated client certificate
|
||||
/// must be removed from the cache.</param>
|
||||
/// <param name="type">clientCertificate.</param>
|
||||
public RemoveClientCertificate(string origin, string type)
|
||||
{
|
||||
Origin = origin;
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
ElectronNET.API/Entities/RemovePassword.cs
Normal file
53
ElectronNET.API/Entities/RemovePassword.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class RemovePassword
|
||||
{
|
||||
/// <summary>
|
||||
/// When provided, the authentication info related to the origin will only be
|
||||
/// removed otherwise the entire cache will be cleared.
|
||||
/// </summary>
|
||||
public string Origin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Credentials of the authentication. Must be provided if removing by origin.
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Realm of the authentication. Must be provided if removing by origin.
|
||||
/// </summary>
|
||||
public string Realm { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Scheme of the authentication. Can be basic, digest, ntlm, negotiate.
|
||||
/// Must be provided if removing by origin.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public Scheme Scheme { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// password.
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Credentials of the authentication. Must be provided if removing by origin.
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="type">password.</param>
|
||||
public RemovePassword(string type)
|
||||
{
|
||||
Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
ElectronNET.API/Entities/Scheme.cs
Normal file
28
ElectronNET.API/Entities/Scheme.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public enum Scheme
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
basic,
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
digest,
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
ntlm,
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
negotiate
|
||||
}
|
||||
}
|
||||
380
ElectronNET.API/Session.cs
Normal file
380
ElectronNET.API/Session.cs
Normal file
@@ -0,0 +1,380 @@
|
||||
using ElectronNET.API.Entities;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
/// <summary>
|
||||
/// Manage browser sessions, cookies, cache, proxy settings, etc.
|
||||
/// </summary>
|
||||
public class Session
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the identifier.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The identifier.
|
||||
/// </value>
|
||||
public int Id { get; private set; }
|
||||
|
||||
internal Session(int id)
|
||||
{
|
||||
Id = id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dynamically sets whether to always send credentials for HTTP NTLM or Negotiate authentication.
|
||||
/// </summary>
|
||||
/// <param name="domains">A comma-separated list of servers for which integrated authentication is enabled.</param>
|
||||
public void AllowNTLMCredentialsForDomains(string domains)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-allowNTLMCredentialsForDomains", Id, domains);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the session’s HTTP authentication cache.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public Task ClearAuthCacheAsync(RemovePassword options)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearAuthCache-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearAuthCache-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearAuthCache", Id, JObject.FromObject(options, _jsonSerializer), guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the session’s HTTP authentication cache.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
public Task ClearAuthCacheAsync(RemoveClientCertificate options)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearAuthCache-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearAuthCache-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearAuthCache", Id, JObject.FromObject(options, _jsonSerializer), guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the session’s HTTP cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task ClearCacheAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearCache-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearCache-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearCache", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the host resolver cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task ClearHostResolverCacheAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearHostResolverCache-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearHostResolverCache-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearHostResolverCache", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the data of web storages.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task ClearStorageDataAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearStorageData-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearStorageData-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearStorageData", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the data of web storages.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public Task ClearStorageDataAsync(ClearStorageDataOptions options)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-clearStorageData-options-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-clearStorageData-options-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-clearStorageData-options", Id, JObject.FromObject(options, _jsonSerializer), guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows resuming cancelled or interrupted downloads from previous Session. The
|
||||
/// API will generate a DownloadItem that can be accessed with the will-download
|
||||
/// event. The DownloadItem will not have any WebContents associated with it and the
|
||||
/// initial state will be interrupted. The download will start only when the resume
|
||||
/// API is called on the DownloadItem.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
public void CreateInterruptedDownload(CreateInterruptedDownloadOptions options)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-createInterruptedDownload", Id, JObject.FromObject(options, _jsonSerializer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disables any network emulation already active for the session. Resets to the
|
||||
/// original network configuration.
|
||||
/// </summary>
|
||||
public void DisableNetworkEmulation()
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-disableNetworkEmulation", Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Emulates network with the given configuration for the session.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
public void EnableNetworkEmulation(EnableNetworkEmulationOptions options)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-enableNetworkEmulation", Id, JObject.FromObject(options, _jsonSerializer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes any unwritten DOMStorage data to disk.
|
||||
/// </summary>
|
||||
public void FlushStorageData()
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-flushStorageData", Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <returns></returns>
|
||||
public Task<int[]> GetBlobDataAsync(string identifier)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<int[]>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-getBlobData-completed" + guid, (buffer) =>
|
||||
{
|
||||
var result = ((JArray)buffer).ToObject<int[]>();
|
||||
|
||||
BridgeConnector.Socket.Off("webContents-session-getBlobData-completed" + guid);
|
||||
taskCompletionSource.SetResult(result);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-getBlobData", Id, identifier, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get session's current cache size.
|
||||
/// </summary>
|
||||
/// <returns>Callback is invoked with the session's current cache size.</returns>
|
||||
public Task<int> GetCacheSizeAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<int>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-getCacheSize-completed" + guid, (size) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-getCacheSize-completed" + guid);
|
||||
taskCompletionSource.SetResult((int)size);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-getCacheSize", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<string[]> GetPreloadsAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<string[]>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-getPreloads-completed" + guid, (preloads) =>
|
||||
{
|
||||
var result = ((JArray)preloads).ToObject<string[]>();
|
||||
BridgeConnector.Socket.Off("webContents-session-getPreloads-completed" + guid);
|
||||
taskCompletionSource.SetResult(result);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-getPreloads", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<string> GetUserAgent()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<string>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-getUserAgent-completed" + guid, (userAgent) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-getUserAgent-completed" + guid);
|
||||
taskCompletionSource.SetResult(userAgent.ToString());
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-getUserAgent", Id, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the proxy information for url. The callback will be called with
|
||||
/// callback(proxy) when the request is performed.
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
public Task<string> ResolveProxyAsync(string url)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<string>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-resolveProxy-completed" + guid, (proxy) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-resolveProxy-completed" + guid);
|
||||
taskCompletionSource.SetResult(proxy.ToString());
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-resolveProxy", Id, url, guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets download saving directory. By default, the download directory will be the
|
||||
/// Downloads under the respective app folder.
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
public void SetDownloadPath(string path)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-setDownloadPath", Id, path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds scripts that will be executed on ALL web contents that are associated with
|
||||
/// this session just before normal preload scripts run.
|
||||
/// </summary>
|
||||
/// <param name="preloads"></param>
|
||||
public void SetPreloads(string[] preloads)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-setPreloads", Id, preloads);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the proxy settings. When pacScript and proxyRules are provided together,
|
||||
/// the proxyRules option is ignored and pacScript configuration is applied.
|
||||
/// </summary>
|
||||
/// <param name="config"></param>
|
||||
/// <returns></returns>
|
||||
public Task SetProxyAsync(ProxyConfig config)
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<object>();
|
||||
string guid = Guid.NewGuid().ToString();
|
||||
|
||||
BridgeConnector.Socket.On("webContents-session-setProxy-completed" + guid, () =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("webContents-session-setProxy-completed" + guid);
|
||||
taskCompletionSource.SetResult(null);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("webContents-session-setProxy", Id, JObject.FromObject(config, _jsonSerializer), guid);
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the userAgent for this session. This doesn't affect existing WebContents, and
|
||||
/// each WebContents can use webContents.setUserAgent to override the session-wide
|
||||
/// user agent.
|
||||
/// </summary>
|
||||
/// <param name="userAgent"></param>
|
||||
public void SetUserAgent(string userAgent)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-setUserAgent", Id, userAgent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the userAgent and acceptLanguages for this session. The
|
||||
/// acceptLanguages must a comma separated ordered list of language codes, for
|
||||
/// example "en-US,fr,de,ko,zh-CN,ja". This doesn't affect existing WebContents, and
|
||||
/// each WebContents can use webContents.setUserAgent to override the session-wide
|
||||
/// user agent.
|
||||
/// </summary>
|
||||
/// <param name="userAgent"></param>
|
||||
/// <param name="acceptLanguages">The
|
||||
/// acceptLanguages must a comma separated ordered list of language codes, for
|
||||
/// example "en-US,fr,de,ko,zh-CN,ja".</param>
|
||||
public void SetUserAgent(string userAgent, string acceptLanguages)
|
||||
{
|
||||
BridgeConnector.Socket.Emit("webContents-session-setUserAgent", Id, userAgent, acceptLanguages);
|
||||
}
|
||||
|
||||
private JsonSerializer _jsonSerializer = new JsonSerializer()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
DefaultValueHandling = DefaultValueHandling.Ignore
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,11 @@ namespace ElectronNET.API
|
||||
/// </value>
|
||||
public int Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Manage browser sessions, cookies, cache, proxy settings, etc.
|
||||
/// </summary>
|
||||
public Session Session { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Emitted when the renderer process crashes or is killed.
|
||||
/// </summary>
|
||||
@@ -82,6 +87,7 @@ namespace ElectronNET.API
|
||||
internal WebContents(int id)
|
||||
{
|
||||
Id = id;
|
||||
Session = new Session(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -45,6 +45,102 @@ module.exports = (socket) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());
|
||||
});
|
||||
socket.on('webContents-session-allowNTLMCredentialsForDomains', (id, domains) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.allowNTLMCredentialsForDomains(domains);
|
||||
});
|
||||
socket.on('webContents-session-clearAuthCache', (id, options, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearAuthCache(options, () => {
|
||||
electronSocket.emit('webContents-session-clearAuthCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-clearCache', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearCache(() => {
|
||||
electronSocket.emit('webContents-session-clearCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-clearHostResolverCache', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearHostResolverCache(() => {
|
||||
electronSocket.emit('webContents-session-clearHostResolverCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-clearStorageData', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearStorageData({}, () => {
|
||||
electronSocket.emit('webContents-session-clearStorageData-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-clearStorageData-options', (id, options, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearStorageData(options, () => {
|
||||
electronSocket.emit('webContents-session-clearStorageData-options-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-createInterruptedDownload', (id, options) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.createInterruptedDownload(options);
|
||||
});
|
||||
socket.on('webContents-session-disableNetworkEmulation', (id) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.disableNetworkEmulation();
|
||||
});
|
||||
socket.on('webContents-session-enableNetworkEmulation', (id, options) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.enableNetworkEmulation(options);
|
||||
});
|
||||
socket.on('webContents-session-flushStorageData', (id) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.flushStorageData();
|
||||
});
|
||||
socket.on('webContents-session-getBlobData', (id, identifier, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.getBlobData(identifier, (buffer) => {
|
||||
electronSocket.emit('webContents-session-getBlobData-completed' + guid, buffer.buffer);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-getCacheSize', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.getCacheSize((size) => {
|
||||
electronSocket.emit('webContents-session-getCacheSize-completed' + guid, size);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-getPreloads', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
const preloads = browserWindow.webContents.session.getPreloads();
|
||||
electronSocket.emit('webContents-session-getPreloads-completed' + guid, preloads);
|
||||
});
|
||||
socket.on('webContents-session-getUserAgent', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
const userAgent = browserWindow.webContents.session.getUserAgent();
|
||||
electronSocket.emit('webContents-session-getUserAgent-completed' + guid, userAgent);
|
||||
});
|
||||
socket.on('webContents-session-resolveProxy', (id, url, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.resolveProxy(url, (proxy) => {
|
||||
electronSocket.emit('webContents-session-resolveProxy-completed' + guid, proxy);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-setDownloadPath', (id, path) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setDownloadPath(path);
|
||||
});
|
||||
socket.on('webContents-session-setPreloads', (id, preloads) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setPreloads(preloads);
|
||||
});
|
||||
socket.on('webContents-session-setProxy', (id, configuration, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setProxy(configuration, () => {
|
||||
electronSocket.emit('webContents-session-setProxy-completed' + guid);
|
||||
});
|
||||
});
|
||||
socket.on('webContents-session-setUserAgent', (id, userAgent, acceptLanguages) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setUserAgent(userAgent, acceptLanguages);
|
||||
});
|
||||
function getWindowById(id) {
|
||||
return electron_1.BrowserWindow.fromId(id);
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -37,11 +37,11 @@ export = (socket: SocketIO.Socket) => {
|
||||
}
|
||||
|
||||
fs.writeFile(path, data, (error) => {
|
||||
if (error) {
|
||||
electronSocket.emit('webContents-printToPDF-completed', false);
|
||||
} else {
|
||||
electronSocket.emit('webContents-printToPDF-completed', true);
|
||||
}
|
||||
if (error) {
|
||||
electronSocket.emit('webContents-printToPDF-completed', false);
|
||||
} else {
|
||||
electronSocket.emit('webContents-printToPDF-completed', true);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -51,6 +51,123 @@ export = (socket: SocketIO.Socket) => {
|
||||
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());
|
||||
});
|
||||
|
||||
socket.on('webContents-session-allowNTLMCredentialsForDomains', (id, domains) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.allowNTLMCredentialsForDomains(domains);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-clearAuthCache', (id, options, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearAuthCache(options, () => {
|
||||
electronSocket.emit('webContents-session-clearAuthCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-clearCache', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearCache(() => {
|
||||
electronSocket.emit('webContents-session-clearCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-clearHostResolverCache', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearHostResolverCache(() => {
|
||||
electronSocket.emit('webContents-session-clearHostResolverCache-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-clearStorageData', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearStorageData({}, () => {
|
||||
electronSocket.emit('webContents-session-clearStorageData-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-clearStorageData-options', (id, options, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.clearStorageData(options, () => {
|
||||
electronSocket.emit('webContents-session-clearStorageData-options-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-createInterruptedDownload', (id, options) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.createInterruptedDownload(options);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-disableNetworkEmulation', (id) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.disableNetworkEmulation();
|
||||
});
|
||||
|
||||
socket.on('webContents-session-enableNetworkEmulation', (id, options) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.enableNetworkEmulation(options);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-flushStorageData', (id) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.flushStorageData();
|
||||
});
|
||||
|
||||
socket.on('webContents-session-getBlobData', (id, identifier, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.getBlobData(identifier, (buffer) => {
|
||||
electronSocket.emit('webContents-session-getBlobData-completed' + guid, buffer.buffer);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-getCacheSize', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.getCacheSize((size) => {
|
||||
electronSocket.emit('webContents-session-getCacheSize-completed' + guid, size);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-getPreloads', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
const preloads = browserWindow.webContents.session.getPreloads();
|
||||
|
||||
electronSocket.emit('webContents-session-getPreloads-completed' + guid, preloads);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-getUserAgent', (id, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
const userAgent = browserWindow.webContents.session.getUserAgent();
|
||||
|
||||
electronSocket.emit('webContents-session-getUserAgent-completed' + guid, userAgent);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-resolveProxy', (id, url, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.resolveProxy(url, (proxy) => {
|
||||
electronSocket.emit('webContents-session-resolveProxy-completed' + guid, proxy);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-setDownloadPath', (id, path) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setDownloadPath(path);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-setPreloads', (id, preloads) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setPreloads(preloads);
|
||||
});
|
||||
|
||||
socket.on('webContents-session-setProxy', (id, configuration, guid) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setProxy(configuration, () => {
|
||||
electronSocket.emit('webContents-session-setProxy-completed' + guid);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('webContents-session-setUserAgent', (id, userAgent, acceptLanguages) => {
|
||||
const browserWindow = getWindowById(id);
|
||||
browserWindow.webContents.session.setUserAgent(userAgent, acceptLanguages);
|
||||
});
|
||||
|
||||
function getWindowById(id: number): Electron.BrowserWindow {
|
||||
return BrowserWindow.fromId(id);
|
||||
}
|
||||
|
||||
@@ -61,6 +61,8 @@ namespace ElectronNET.WebApp
|
||||
Show = false
|
||||
});
|
||||
|
||||
await browserWindow.WebContents.Session.ClearCacheAsync();
|
||||
|
||||
browserWindow.OnReadyToShow += () => browserWindow.Show();
|
||||
browserWindow.SetTitle(Configuration["DemoTitleInSettings"]);
|
||||
}
|
||||
|
||||
@@ -30,8 +30,7 @@
|
||||
<p>This example execute the TypeScript code, that listening on "create-excel". The TypeScript code use a NPM Package names exceljs, to create a Excel file and reply a success message.</p>
|
||||
|
||||
<h5>Main Process (C#)</h5>
|
||||
<pre><code class="csharp">
|
||||
Electron.IpcMain.On("start-hoosthook", async (args) =>
|
||||
<pre><code class="csharp">Electron.IpcMain.On("start-hoosthook", async (args) =>
|
||||
{
|
||||
var mainWindow = Electron.WindowManager.BrowserWindows.First();
|
||||
var options = new OpenDialogOptions
|
||||
@@ -45,13 +44,11 @@ Electron.IpcMain.On("start-hoosthook", async (args) =>
|
||||
|
||||
var resultFromTypeScript = await Electron.HostHook.CallAsync<string>("create-excel-file", folderPath);
|
||||
Electron.IpcMain.Send(mainWindow, "excel-file-created", resultFromTypeScript);
|
||||
});
|
||||
</code>
|
||||
});</code>
|
||||
</pre>
|
||||
<h5>index.ts from ElectronHostHook-Folder (TypeScript)</h5>
|
||||
<pre>
|
||||
<code class="typescript">
|
||||
import * as Electron from "electron";
|
||||
<code class="typescript">import * as Electron from "electron";
|
||||
import { Connector } from "./connector";
|
||||
import { ExcelCreator } from "./excelCreator";
|
||||
|
||||
@@ -69,8 +66,7 @@ export class HookService extends Connector {
|
||||
done(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
</code>
|
||||
}</code>
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -17,10 +17,12 @@ body {
|
||||
|
||||
/* enable text selection */
|
||||
|
||||
.is-selectable,
|
||||
/*.is-selectable,*/
|
||||
|
||||
pre,
|
||||
code {
|
||||
-webkit-user-select: auto;
|
||||
user-select: text;
|
||||
cursor: auto;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user