AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.10.1" />

 BrowsingContextExtensions

public static class BrowsingContextExtensions
A set of extensions for the browsing context.
using AngleSharp.Browser; using AngleSharp.Browser.Dom.Events; using AngleSharp.Common; using AngleSharp.Css; using AngleSharp.Dom; using AngleSharp.Io; using AngleSharp.Media; using AngleSharp.Scripting; using AngleSharp.Text; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace AngleSharp { public static class BrowsingContextExtensions { public static Task<IDocument> OpenNewAsync(this IBrowsingContext context, string url = null, CancellationToken cancellation = default(CancellationToken)) { return context.OpenAsync(delegate(VirtualResponse m) { m.Address(url); }, cancellation); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel = default(CancellationToken)) { if (response == null) throw new ArgumentNullException("response"); if (context == null) context = BrowsingContext.New(null); Encoding defaultEncoding = context.GetDefaultEncoding(); IDocumentFactory factory = context.GetFactory<IDocumentFactory>(); CreateDocumentOptions options = new CreateDocumentOptions(response, defaultEncoding, null); return factory.CreateAsync(context, options, cancel); } public static async Task<IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel = default(CancellationToken)) { if (request == null) throw new ArgumentNullException("request"); IDocumentLoader service = context.GetService<IDocumentLoader>(); if (service != null) { IDownload download = service.FetchAsync(request); IDownload download2 = download; cancel.Register(download2.Cancel); using (IResponse response = await download.Task.ConfigureAwait(false)) { if (response != null) return await context.OpenAsync(response, cancel).ConfigureAwait(false); } } return await context.OpenNewAsync(request.Target.Href, cancel).ConfigureAwait(false); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, Url url, CancellationToken cancel = default(CancellationToken)) { if (url == null) throw new ArgumentNullException("url"); DocumentRequest documentRequest = DocumentRequest.Get(url, null, null); if (context != null && context.Active != null) documentRequest.Referer = context.Active.DocumentUri; return context.OpenAsync(documentRequest, cancel); } public static async Task<IDocument> OpenAsync(this IBrowsingContext context, Action<VirtualResponse> request, CancellationToken cancel = default(CancellationToken)) { if (request == null) throw new ArgumentNullException("request"); using (IResponse response = VirtualResponse.Create(request)) return await context.OpenAsync(response, cancel).ConfigureAwait(false); } public static Task<IDocument> OpenAsync(this IBrowsingContext context, string address, CancellationToken cancellation = default(CancellationToken)) { if (address == null) throw new ArgumentNullException("address"); return context.OpenAsync(Url.Create(address), cancellation); } public static void NavigateTo(this IBrowsingContext context, IDocument document) { context.SessionHistory?.PushState(document, document.Title, document.Url); context.Active = document; } public static Encoding GetDefaultEncoding(this IBrowsingContext context) { IEncodingProvider provider = context.GetProvider<IEncodingProvider>(); string language = context.GetLanguage(); return provider?.Suggest(language) ?? Encoding.UTF8; } public static CultureInfo GetCulture(this IBrowsingContext context) { return context.GetService<CultureInfo>() ?? CultureInfo.CurrentUICulture; } public static CultureInfo GetCultureFrom(this IBrowsingContext context, string language) { try { return new CultureInfo(language); } catch (CultureNotFoundException) { return context.GetCulture(); } } public static string GetLanguage(this IBrowsingContext context) { return context.GetCulture().Name; } public static TFactory GetFactory<TFactory>(this IBrowsingContext context) where TFactory : class { return context.GetServices<TFactory>().Single(); } public static TProvider GetProvider<TProvider>(this IBrowsingContext context) where TProvider : class { return context.GetServices<TProvider>().SingleOrDefault(); } public static IResourceService<TResource> GetResourceService<TResource>(this IBrowsingContext context, string type) where TResource : IResourceInfo { foreach (IResourceService<TResource> service in context.GetServices<IResourceService<TResource>>()) { if (service.SupportsType(type)) return service; } return null; } public static string GetCookie(this IBrowsingContext context, Url url) { return context.GetProvider<ICookieProvider>()?.GetCookie(url) ?? string.Empty; } public static void SetCookie(this IBrowsingContext context, Url url, string value) { context.GetProvider<ICookieProvider>()?.SetCookie(url, value); } public static ISpellCheckService GetSpellCheck(this IBrowsingContext context, string language) { ISpellCheckService spellCheckService = null; IEnumerable<ISpellCheckService> services = context.GetServices<ISpellCheckService>(); CultureInfo cultureFrom = context.GetCultureFrom(language); string twoLetterISOLanguageName = cultureFrom.TwoLetterISOLanguageName; foreach (ISpellCheckService item in services) { CultureInfo culture = item.Culture; if (culture != null) { string twoLetterISOLanguageName2 = culture.TwoLetterISOLanguageName; if (culture.Equals(cultureFrom)) return item; if (spellCheckService == null && twoLetterISOLanguageName2.Is(twoLetterISOLanguageName)) spellCheckService = item; } } return spellCheckService; } public static IStylingService GetCssStyling(this IBrowsingContext context) { return context.GetStyling(MimeTypeNames.Css); } public static IStylingService GetStyling(this IBrowsingContext context, string type) { foreach (IStylingService service in context.GetServices<IStylingService>()) { if (service.SupportsType(type)) return service; } return null; } public static bool IsScripting(this IBrowsingContext context) { return context.GetServices<IScriptingService>().Any(); } public static IScriptingService GetJsScripting(this IBrowsingContext context) { return context.GetScripting(MimeTypeNames.DefaultJavaScript); } public static IScriptingService GetScripting(this IBrowsingContext context, string type) { foreach (IScriptingService service in context.GetServices<IScriptingService>()) { if (service.SupportsType(type)) return service; } return null; } public static ICommand GetCommand(this IBrowsingContext context, string commandId) { return context.GetProvider<ICommandProvider>()?.GetCommand(commandId); } public static Task InteractAsync<T>(this IBrowsingContext context, string eventName, T data) { InteractivityEvent<T> interactivityEvent = new InteractivityEvent<T>(eventName, data); context.Fire(interactivityEvent); return interactivityEvent.Result ?? Task.FromResult(false); } public static IBrowsingContext CreateChildFor(this IBrowsingContext context, string target) { Sandboxes security = Sandboxes.None; if (target.Is("_blank")) target = null; return context.CreateChild(target, security); } public static IBrowsingContext FindChildFor(this IBrowsingContext context, string target) { if (string.IsNullOrEmpty(target) || target.Is("_self")) return context; if (target.Is("_parent")) return context.Parent ?? context; if (target.Is("_top")) return context; return context.FindChild(target); } public static IEnumerable<Task> GetDownloads<T>(this IBrowsingContext context) where T : INode { IResourceLoader service = context.GetService<IResourceLoader>(); if (service == null) return Enumerable.Empty<Task>(); return from m in service.GetDownloads() where m.Source is T select ((ICancellable<IResponse>)m).Task; } } }