DotNetBrowser 1.8.1 introduced a new API that allows to work with the back-forward navigation history. Now it is possible to navigate to any entry in this history and even remove some entries from it.


All this functionality is available through the Browser.BackForwardNavigator property.


The controller’s interface allows to perform the following actions:

  • Get the current navigation entry using the IBackForwardNavigator.CurrentEntry property;

  • Get the collection of all entries using the IBackForwardNavigator.Entries property;

  • Load the specific entry using the IBackForwardNavigator.GoTo() method;

  • Load the entry by its offset using the IBackForwardNavigator.Forward() and IBackForwardNavigator.Back() methods;

  • Remove navigation entries from the history using the IBackForwardNavigator.Remove() method.

The following sample demonstrates how to clear the navigation history. Please note that the Chromium engine may discard the navigation entry removal, e.g. if you are trying to delete the current entry.


C#

class Program
{
    private static Browser browser;

    private static readonly ManualResetEvent loadedEvent = new ManualResetEvent(false);

    private static readonly List<string> urls = new List<string>()
    {
        "google.gom",
        "teamdev.com",
        "bing.com",
        "gmail.com",            
        "github.com"
    };

    static void Main(string[] args)
    {
        CreateBrowser();

        Console.WriteLine("\nInitial navigation entries:");
        PrintNavigationEntries();

        Console.WriteLine("\nLoading pages");
        LoadUrls();

        Console.WriteLine("\nNavigation entries after the pages have been loaded:");
        PrintNavigationEntries();

        Console.WriteLine("\nRemoving navigation entries");
        RemoveNavigationEntries();

        Console.WriteLine("\nNavigation entries after cleanup:");
        PrintNavigationEntries();

        Console.ReadKey();
    }

    private static void CreateBrowser()
    {
        browser = BrowserFactory.Create();

        browser.FinishLoadingFrameEvent += (s, e) =>
        {
            if (e.IsMainFrame)
            {
                loadedEvent.Set();
            }
        };
    }

    private static void LoadUrls()
    {
        foreach (string url in urls)
        {
            loadedEvent.Reset();
            Console.Write("Loading '{0}'...", url);
            browser.LoadURL(url);
            loadedEvent.WaitOne();
            Console.WriteLine("Completed");
        }
    }

    private static void PrintNavigationEntries()
    {
        foreach (INavigationEntry navigationEntry in browser.BackForwardNavigator.Entries)
        {
            Console.WriteLine(navigationEntry.Url);
        }
    }

    private static void RemoveNavigationEntries()
    {
        int count = browser.BackForwardNavigator.Entries.Count;
        for (int i = count - 2; i >= 0; i--)
        {
            INavigationEntry navigationEntry = browser.BackForwardNavigator.Entries[i];
            bool removed = browser.BackForwardNavigator.Remove(i);
            Console.WriteLine("Entry with url '{0}' {1}", navigationEntry.Url, removed ? "removed" : "is not removed");
        }
    }
}


VB.NET

Imports System.Threading
Imports DotNetBrowser
Imports DotNetBrowser.Events

Module MainModule

    Dim browser as Browser
    Dim loadedEvent as ManualResetEvent

    Dim Urls as List(Of String)

    Sub Main()
        InitializeBrowserAndUrls()

        Console.WriteLine("Initial navigation entries:")
        PrintNavigationEntries()

        Console.WriteLine("Loading pages")
        LoadUrls()

        Console.WriteLine("Navigation entries after the pages have been loaded:")
        PrintNavigationEntries()

        Console.WriteLine("Removing navigation entries")
        RemoveNavigationEntries()

        Console.WriteLine("Navigation entries after cleanup:")
        PrintNavigationEntries()

        Console.ReadKey()
    End Sub

    Sub InitializeBrowserAndUrls()
        loadedEvent = New ManualResetEvent(False)

        browser = BrowserFactory.Create()
        AddHandler browser.FinishLoadingFrameEvent, AddressOf FinishLoadingFrameEventHandler

        Urls = New List(Of String)
        Urls.Add("google.gom")
        Urls.Add("teamdev.com")
        Urls.Add("bing.com")
        Urls.Add("gmail.com")
        Urls.Add("github.com")
    End Sub

    Sub FinishLoadingFrameEventHandler(sender As Object, args As FinishLoadingEventArgs)
        loadedEvent.Set()
    End Sub

    Sub PrintNavigationEntries()
        For Each navigationEntry As INavigationEntry In browser.BackForwardNavigator.Entries
            Console.WriteLine(navigationEntry.Url)
        Next
    End Sub

    Sub LoadUrls()
        For Each url As String In Urls
            loadedEvent.Reset()
            Console.Write("Loading '{0}'...", url)
            browser.LoadURL(url)
            loadedEvent.WaitOne()
            Console.WriteLine("Completed")
        Next
    End Sub

    Sub RemoveNavigationEntries()
        Dim count = browser.BackForwardNavigator.Entries.Count
        For index = count - 2 To 0 Step -1
            Dim navigationEntry As INavigationEntry = browser.BackForwardNavigator.Entries(index)
            Dim removed As Boolean = browser.BackForwardNavigator.Remove(index)
            Console.WriteLine("Entry with url '{0}' {1}", navigationEntry.Url, If(removed, "removed", "is not removed"))
        Next
    End Sub
End Module