Developing Chrome Core Browser "Five" based on. Net

Source: Internet
Author: User

Original: "Five" chrome core browser based on. NET development

One: This article will solve the problem

The main purpose of this chapter is to solve several problems:

The problem of 1.JsDialog button dislocation

We developed the browser, in some of the system calls Alert,confirm, such as the dialog box, determine and cancel the button will appear dislocation situation

2. Right-click menu problem

We developed the browser, on the Web page right click, will appear some nasty English menu.

3. Problems with printing

We developed the browser, the Web page in the call Window.print, there is no response.

4. Open Chrome's debugger

Google browser Debugging Web page debugger is very useful, we developed a browser can also use this tool.

Two: Jsdialog button dislocation problem

First create a new class in the BS folder, named Jsdialoghandler, and let this class inherit from Cefjsdialoghandler

Then rewrite the Onjsdialog function in this class, with the following code:

protected Override BOOLOnjsdialog (Cefbrowser Browser,stringOriginUrl,stringAcceptLang, Cefjsdialogtype DialogType,stringMessage_text,stringDefault_prompt_text, Cefjsdialogcallback Callback, out BOOLsuppress_message) {            Switch(dialogtype) { CaseCefJSDialogType.Alert:MessageBox.Show (Message_text,"XXXSystem Tips"); Suppress_message=true; return false;  Break;  Casecefjsdialogtype.confirm:varDr = MessageBox.Show (Message_text,"XXX System Tips", 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;  CaseCefJSDialogType.Prompt:MessageBox.Show ("the system does not support the prompt form of a prompt box","utmp System Tips");  Break; } suppress_message=true; return false; }

Let's explain what's in the code.

Default_prompt_text Parameters:

For the prompt type of dialog service (this kind of dialog can receive the user's input, it is generally rare to see, we do not implement this type of dialog);

Suppress_message Parameters:

If this parameter is set to True, and the function returns a value of false, the page will be blocked from opening the JS pop-up window.

If this parameter is set to False and the function return value is false, the page will open the JS pop-up window.

Message_text Parameters:

is what the popup window will display

DialogType Parameters:

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

Callback Parameters:

When the user clicks the OK button of the pop-up window, you can use callback. Continue (True, String. EMPTY); Callback Determination function

When the user clicks the pop-up window's Cancel button, you can use callback. Continue (False, String. Empty); callback cancellation function

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

Inside the function, we replaced the CEF popup box with the system popup, from the problem of displaying the popup button to the solution.

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

There are also two virtual methods that need to be rewritten in this class:

Onresetdialogstate

This method cancels out all the upcoming dialog boxes, which are usually called when the page jumps.

Onbeforeunloaddialog

When the user leaves the page, the query dialog box pops up, returning false will use the default pop-up window

These two methods are just a simple rewrite. There's no other implementation.

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

After this class is created, to add a private property in the Bsclient class

Private ReadOnly Cefjsdialoghandler Jsdialoghandler;

Then assign a value to the property in the constructor

New Jsdialoghandler ();

Then rewrite a method of the parent class:

        protected Override Cefjsdialoghandler Getjsdialoghandler ()        {            return  jsdialoghandler;        }

At this point: our jsdialoghandler will take effect.

Three: Problems with the right-click menu

To remove the system's default right-click 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, and if you want to not display the right-click menu, just clear it.

And then, like Jsdialoghandler, rewrite the cefclient Getcontextmenuhandler method

Returning an instance of this class is possible.

Four: Problems with printing

That's how I deal with it:

In the Onjsdialog method mentioned earlier

Add the following code

                 CaseCefjsdialogtype.alert:if(Message_text. StartsWith ("$Print $"))                    {                        varstr = Message_text. Substring (7); varIEB =NewIebrow (); Ieb.                        Print (str); Ieb.                        Show (); Suppress_message=true; return false; }

The content prefix of the popup box if it is "$Print $" to enter the printing process

(This is how the egg hurts the practice!!) First Window.print is not available, only with alert ("$Print $balabalabala"). )

Ieb is a iebrowser

The key code inside is as follows:

         Public void Print (string  doc)        {            = doc;        }         Private void webbrowser1_documentcompleted (object  sender, WebBrowserDocumentCompletedEventArgs e)        {            webbrowser1.print ();        }

Five: Open Debugger

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

        /// <summary>        ///Show Debug Window/// </summary>         Public voidShowdevwin () {Try            {                if(string. IsNullOrEmpty (Devtoolsurl)) {Devtoolsurl= Web_view. Browser.gethost (). Getdevtoolsurl (true); }                varframe =Web_view.                Browser.getmainframe (); //frame. Executejavascript (String. Format ("window.open (' {0} ');", Devtoolsurl), "About:blank", 0);                varp =Process.Start (Devtoolsurl); }            Catch{MessageBox.Show ("Please wait until the page loads and then open the debugger"); }        }

Attention! Be sure to put the relevant resources in the specified location!

VI: let the browser execute the JS script

 /// <SUMMARY>  ///  Execute JS script  /// </summary>  /// <param name= " JS " " Createpage (a); "        </PARAM>  public  void  Runscirpt (string   JS) { var  frame = Web_view.            Browser.getmainframe (); Frame. Executejavascript (JS, frame.        URL,  0  ); }

That's it, not much to explain.

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

PS: Description:

Thank you again for your interest in this series of friends.

I think you might be more disappointed with this article.

(Printing that part although the official did not support, but I think there is a better way to solve the problem, in the project, I lazy, now write articles, I am lazy again.) Did not do in-depth research. Sorry

(The article is also a bit of a hurry, write not detailed enough, more than the previous few, I did not even do demo, also can not provide source code)

Next, in the short term, I guess I'm not going to update this series anymore. Too busy, too tired.

Thank you, everyone!

Developing Chrome Core Browser "Five" based on. Net

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.