CefSharp Integrated Google Browser Details (4) --Official Web Example Resolution 3 RegisterJsObject CefSharpSchemeHandlerFactory

This chapter focuses on custom javascript functions for making local calls and customizing local domain names.

1. Custom javascript functions

1) Get the browser object and register your own js object.The following:

//Registering usbjs objects
browser.RegisterJsObject("usbKey", new UsbKeyBound());
//Note that in the js function only the hump notation begins. If you want to use the C#notation, use the following registration method.
//BindingOptions bo = new BindingOptions();
//bo.CamelCaseJavascriptNames = false;
//browser.RegisterJsObject("usbKey", new UsbKeyBound(), bo);

2) The USBKEY is the name of the js object, so let's look at the UsbKeyBound.

public class UsbKeyBound
{
    public void ShowTest()
    {
        MessageBox.Show("This one test");
    }
}

3) Let's see how to call a custom js method in html.

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<button type="button" onclick="test()">click here</button>
</body>
</html>
<script type="text/javascript">
function test() {
    //Note that only lowercase start is recognized in js function
    usbKey.showTest();
}
</script>

The effect is as follows:

4) Custom js functions also have a way of asynchronous registration of js and invocation with parameters, specific methods please ask the honored lady.

You can see this domain name access in the pictureCustom://cefsharp/zpy.htmlNow let's see how to register a custom domain name and access local html files.

Register local html files

This line of code registers Scheme in settings:

settings.RegisterScheme(new CefCustomScheme
        {
            //CefSharpSchemeHandlerFactory.SchemeName The value of is "custom"
            SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
            SchemeHandlerFactory = new CefSharpSchemeHandlerFactory(),
            IsSecure = true //treated with the same security rules as those applied to "https" URLs
        });

Take a look at the CefSharpSchemeHandlerFactory

The following are registered local html files so that they can be accessed through a custom url.

static CefSharpSchemeHandlerFactory()
    {
        ResourceDictionary = new Dictionary<string, string>
        {
            { "/home.html", Resources.home_html },

            { "/assets/css/shCore.css", Resources.assets_css_shCore_css },
            { "/assets/css/shCoreDefault.css", Resources.assets_css_shCoreDefault_css },
            { "/assets/css/docs.css", Resources.assets_css_docs_css },
            { "/assets/js/application.js", Resources.assets_js_application_js },
            { "/assets/js/jquery.js", Resources.assets_js_jquery_js },
            { "/assets/js/shBrushCSharp.js", Resources.assets_js_shBrushCSharp_js },
            { "/assets/js/shBrushJScript.js", Resources.assets_js_shBrushJScript_js },
            { "/assets/js/shCore.js", Resources.assets_js_shCore_js },

            { "/bootstrap/bootstrap-theme.min.css", Resources.bootstrap_theme_min_css },
            { "/bootstrap/bootstrap.min.css", Resources.bootstrap_min_css },
            { "/bootstrap/bootstrap.min.js", Resources.bootstrap_min_js },

            { "/BindingTest.html", Resources.BindingTest },
            { "/ExceptionTest.html", Resources.ExceptionTest },
            { "/PopupTest.html", Resources.PopupTest },
            { "/SchemeTest.html", Resources.SchemeTest },
            { "/TooltipTest.html", Resources.TooltipTest },
            { "/FramedWebGLTest.html", Resources.FramedWebGLTest },
            { "/MultiBindingTest.html", Resources.MultiBindingTest },
            { "/ScriptedMethodsTest.html", Resources.ScriptedMethodsTest },
            { "/ResponseFilterTest.html", Resources.ResponseFilterTest },
            { "/DraggableRegionTest.html", Resources.DraggableRegionTest },
            { "/CssAnimationTest.html", Resources.CssAnimation },
            { "/CdmSupportTest.html", Resources.CdmSupportTest },
            { "/Recaptcha.html", Resources.Recaptcha },
            { "/zpy.html", Resources.zpy }
        };
    }

The following is the directory where the resource files are located:

Last CefSharp Integrated Google Browser Details (3) - Official Web Sample Resolution 2 Introduction to CefSettings

Next CefSharp Integrated Google Browser Details (5) - Official Web Sample Resolution 2 winform Captures Chromium WebBrowser Messages

Tags: Javascript Google JQuery

Posted on Thu, 04 Jun 2020 14:32:35 -0400 by mdub2112