using System; using System.Linq; using System.Net.Http; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using System.Windows.Forms; using Bunifu.UI.WinForms; using CefSharp; using CefSharp.WinForms; namespace XeroBrowser { public class RequestHandler : CefSharp.Handler.RequestHandler { // Define arrays with domain names only private readonly string[] blockedDrms = { "netflix.com", "www.netflix.com", "disneyplus.com", "twitch.tv", "tiktok.com" }; private readonly string[] incompatibleExtSites = { "chrome.google.com", "addons.mozilla.org", "chromewebstore.google.com" }; private string[] adDomains; public RequestHandler() { // Fetch the blocked domains asynchronously when the RequestHandler is instantiated FetchAdDomainsAsync().ContinueWith(t => adDomains = t.Result); } public async Task FetchAdDomainsAsync() { string[] adDomains = Array.Empty(); try { using (HttpClient client = new HttpClient()) { string url = "https://diamondcreeper.org/web-filtering/lists/ads.txt"; HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); string[] adsDomains = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); } } } catch (Exception ex) { MessageBox.Show($"Error fetching ad domains: {ex.Message}"); } return adDomains; } protected override bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { throw new NotImplementedException(); } protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling) { return null; } protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect) { Uri requestUri = new Uri(request.Url); string domain = requestUri.Host.ToLower(); if (blockedDrms.Contains(domain)) { // Cancel the navigation or sub-request chromiumWebBrowser.LoadHtml("

Error!

The requested site requires a DRM (Digital Rights Management) which Xero Browser does not support!

"); // Clear the txtSearchOrUrl text box //txtSearchOrUrl.Text = string.Empty; } else if (incompatibleExtSites.Contains(domain)) { // Cancel the navigation or sub-request chromiumWebBrowser.LoadHtml("

Error!

Browser extensions are not supported!

"); // Clear the txtSearchOrUrl text box //txtSearchOrUrl.Text = string.Empty; } else if (adDomains.Contains(domain)) { chromiumWebBrowser.LoadHtml("

Ads are blocked!

Ads are blocked in xero browser

"); return true; } return false; } } }