In DotNetBrowser 1.7 several changes to public API have been introduced. These changes may require changes to your application’s source code. This short guide shows how to change your application’s source code written with previous API (DotNetBrowser 1.6.4, for example) to the new API introduced in DotNetBrowser 1.7.


Frames IDs

1.6.4

In 1.6.4 theBrowser.GetFramesIds() method returns IDs of all frames on the currently loaded web page. It doesn’t allow to get IDs of frames located inside another frame. 


var framesIds = browser.GetFramesIds();

1.7

Now, the Browser.GetFramesIds() method returns IDs of the main frame only. To get IDs of frames located inside other frames the Browser.GetFramesIds(long frameId) method has been introduced.


List<long> framesIds = browser.GetFramesIds();

foreach (long framesId in framesIds) {
    List<long> subFramesIds = browser.GetFramesIds(framesId);
}


Working with BrowserFunction

1.6.4

To register global JavaScript function the Browser.RegisterFunction(String functionName, BrowserFunction function) method is used. Every time when the web page is loaded, DotNetBrowser creates a function with the functionName and attaches it to the window JavaScript global object.


public class MyFunction: BrowserFunction{
            public JSValue invoke(params JSValue[] args)
            {
                    return JSValue.CreateNull();
            }
}
// Usage
browser.RegisterFunction("save", new MyrFunction());


The function will be injected into JavaScript of each loaded web page until you un-register it via the Browser.UnregisterFunction(String functionName) method.

1.7

In 1.7 the BrowserFunction API has become a part of JSObject class. Now, to register global JavaScript function you need to use the following code: 


    public class SaveObject
        {
            public void Save(String firstName, String lastName)
            {
                Console.Out.WriteLine("firstName = " + firstName);
                Console.Out.WriteLine("lastName = " + lastName);
            }
        }

// Usage

browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
            {
                if (e.IsMainFrame)
                {
                    JSValue value = browser.ExecuteJavaScriptAndReturnValue("window");
                    value.AsObject().SetProperty("save", new SaveObject());
                }
            };


Every time when you load/reload the web page, you must inject the object into JavaScript. The object will be removed automatically when another web page is loaded or existing one reloaded.


Print to PDF

1.6.4

To print currently loaded web page to PDF document, in DotNetBrowser 1.6.4 the Browser.PrintToPDF() method is used. This method prints web page to PDF document with predefined settings. The following code demonstrates how to print web page to PDF and get a notification when printing has been finished:


browser.PrintToPDF("web_page.pdf", delegate(object sender, PrintJobEventArgs e)
{
    //This handler will be invoked when printing is finished.
});

1.7

In DotNetBrowser 1.7 we extended Printing API and added functionality that allows to decide whether web page should be printed using printer device or saved as PDF document. We removed Browser.PrintToPDF()method. Now, you must decide in your PrintHandlerimplementation whether web page should be saved as PDF or printed using printer device. The following code demonstrates how to handle printing, tell Chromium to save the web page as PDF document with the given print settings, and get a notification when printing has been finished:

 

class MyPDFPrintHandler : PrintHandler
{
    Func<PrintSettings, PrintSettings> func;

    public MyPDFPrintHandler(Func<PrintSettings, PrintSettings> func)
    {
        this.func = func;
    }

    public PrintStatus OnPrint(PrintJob printJob)
    {
        PrintSettings printSettings = func(printJob.PrintSettings);
        printSettings.PrintToPDF = true;
        printSettings.Landscape = true;
        printSettings.PrintBackgrounds = true;
        printSettings.PageMargins = new PageMargins(20, 40, 40, 20);
        printJob.PrintJobEvent += (s, e) =>
        {
            //This handler will be invoked when printing is finished.
        };
        return PrintStatus.CONTINUE;
    }
}

// Usage

browser.PrintHandler = new MyPDFPrintHandler((printSettings) =>
{
    printSettings.PDFFilePath = fileName;
    return printSettings;
});

browser.Print();