Develop a chrome core Browser Based on. NET [5]

Source: Internet
Author: User

I. problems solved in this article

This chapter mainly aims to solve the following problems:

1. mismatch of buttons in jsdialog

The buttons for confirmation and cancellation may be misplaced when alert, confirm, and other dialogs are called on some operating systems in the browser we developed.

2. Right-click the menu

Right-click the browser we developed on the webpage and some annoying English menus will appear.

3. Printing Problems

The browser we developed has no response when the webpage calls window. Print.

4. Open the chrome Debugger

Google's browser is very easy to use to debug the web page debugger. We can also use this tool to develop browsers.

Ii. mismatch of jsdialog buttons

First, create a new class in the BS folder named jsdialoghandler so that this class inherits from cefjsdialoghandler.

Then rewrite the onjsdialog function in this class. The Code is as follows:

Protected override bool onjsdialog (cefbrowser browser, string originurl, string acceptlang, includialogtype, string message_text, string callback, callback, out bool suppress_message) {Switch (digalotype) {response. alert: MessageBox. show (message_text, "XXX system prompt"); suppress_message = true; return false; break; Case cefjsdialogtype. confirm: var DR = MessageBox. show (message_text, "XXX system prompt", messageboxbuttons. yesno); If (DR = dialogresult. yes) {callback. continue (true, String. empty); suppress_message = false; return true;} else {callback. continue (false, String. empty); suppress_message = false; return true;} break; Case cefjsdialogtype. prompt: MessageBox. show ("the system does not support prompt boxes in the form of prompt", "utmp system prompt"); break;} suppress_message = true; return false ;}

Next, let's explain the content in the code.

Default_prompt_text parameter:

It is a prompt Type dialog Service (this type of dialog can receive user input, which is rarely seen. We have not implemented this type of DIALOG );

Suppress_message parameters:

If this parameter is set to true and the return value of the function is false, the JS pop-up window is blocked.

If this parameter is set to false and the return value of the function is false, the JS pop-up window is displayed.

Message_text parameters:

Is the content to be displayed in the pop-up window

Dialogtype parameters:

Is the type of the pop-up window (alert, confirm, prompt)

Callback parameters:

When you click the OK button in the pop-up window, you can use callback. Continue (true, String. Empty); callback to determine the function

When you click the cancel button in the pop-up window, you can use callback. Continue (false, String. Empty); callback to cancel the Function

------------------

Inside the function, we use the system pop-up box to replace the CEF pop-up box to solve the problem of the pop-up box button display.

------------------

In this class, you also need to override two virtual methods:

Onresetdialogstate

This method can be used to cancel all the pop-up dialog boxes, which are usually called during page Jump.

Onbeforeunloaddialog

When a user leaves the page, the pop-up dialog box appears. If false is returned, the default pop-up window is used.

You only need to simply rewrite the two methods. No other implementations

-------------------

After this class is created, add a private attribute to the bsclient class.

private readonly CefJSDialogHandler jsDialogHandler;

Then assign a value to this attribute in the constructor.

jsDialogHandler = new JsDialogHandler();

Then rewrite a method of the parent class:

        protected override CefJSDialogHandler GetJSDialogHandler()        {            return jsDialogHandler;        }

So far: Our jsdialoghandler will take effect.

Iii. Right-click menu

To remove the default shortcut menu,

As long as the subclass of cefcontextmenuhandler is implemented

Then rewrite the onbeforecontextmenu method,

Let's take a look at this method:

        protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)        {            model.Clear();        }

The model includes all the items in the default right-click menu. If you do not want to display the right-click menu, just clear it.

Then, like jsdialoghandler, rewrite the getcontextmenuhandler method of cefclient.

Return the instance of this class.

Iv. Printing Problems

This is how I handle it:

In the onjsdialog method mentioned above

Add the following code:

                case CefJSDialogType.Alert:                    if (message_text.StartsWith("$Print$"))                    {                        var str = message_text.Substring(7);                        var ieb = new IEBrow();                        ieb.Print(str);                        ieb.Show();                        suppress_message = true;                        return false;                    }

The content prefix in the pop-up box is $ Print $.

(This is an amazing way !! First, window. Print is useless. You can only use alert ("$ Print $ balabalabala ").)

IEB is an iebrowser

The key code is as follows:

        public void Print(string doc)        {            webBrowser1.DocumentText = doc;        }        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)        {            webBrowser1.Print();        }

V. Open the debugger

If you want to open the debugger, you can see the following code.

/// <Summary> /// display the debugging window /// </Summary> Public void showdevwin () {try {If (string. isnullorempty (devtoolsurl) {devtoolsurl = web_view.browser.gethost (). getdevtoolsurl (true);} var frame = web_view.browser.getmainframe (); // frame. executejavascript (string. format ("window. open ('{0}'); ", devtoolsurl)," about: blank ", 0); var P = process. start (devtoolsurl);} catch {MessageBox. show ("Please wait until the page is loaded before opening the debugger ");}}

Note! Be sure to place the relevant resources in the specified location!

6. Let the browser execute the JS script

/// <Summary> /// execute the JS script /// </Summary> /// <Param name = "JS"> "createpage (, 3 ); "</param> Public void runscirpt (string JS) {var frame = web_view.browser.getmainframe (); frame. executejavascript (JS, frame. URL, 0 );}

In this case, I will not explain it much.

--------------------------------

PS: Note:

Thank you again for your interest in this series.

I think you may be disappointed with this article.

(Print that part is not officially supported, but I think there must be a better solution to this problem. When I was working on a project, I was lazy. Now I am writing articles, and I am also lazy. No in-depth research was conducted. Sorry)

(The article is also written in a hurry. The writing is not detailed enough. It is much worse than the previous articles. I didn't even make a demo or provide the source code)

Next, in the short term, I guess I will not update this series any more. Too busy and too tired.

Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.