Browser Integration Teaching - Script Browser This tutorial provides methods for adding scripts in browser programs, you will see how to add script support to MFC programs. This article also discusses how to extend the ability of the CHTMLVIEW in the VC6, how to implement the MDI structure browser, and how to analyze the DHTML document structure. Click here to download the code of this article This article is divided into the following
Prerequisites and requirements Introduction Activity script is an application to add script to support security ChtmlView's enhancement script example conclude reference premise and needs before reading this article, it is recommended first
For Microsoft Basics (MFC) and Component Object Models (COM), familiar with the Active Template Library (ATL) Installing Microsoft's Internet Explorer (IE) 6.0 or later has IE6.0 or higher in the development environment Header file and library file introduction Integrated browser control For fast application development (RAD) is a powerful tool; you can use dynamic HTML (DHTML), or Scalable Markup Language (XML) to display your user interface . A usual use is to use it to display the form and then process the form by analyzing the form page and processing the submitted event. However, if you want to analyze the form of the form, the analysis is completely dependent on the structure of the page, that is, if you analyze the web page through the interface provided in the application, then for each web structure, you have to write and compile once. Code. This is completely no problem with the application and form page, but is on the remote server, but sometimes modify, or if you want to make the application valid for other websites, you must change and Reissue the app. To avoid repeatedly modifying applications, you can use-activity scripts to use activity scripts to write flexible processing code without recompilating programs. You may have already seen the active script in many applications, such as IE, Microsoft Office, and Visual Studio. In the Microsoft Windows Script Interface Interface Introduction, the Microsoft Windows Script Interface Interface Text Introduction, describes the concept, background, architecture, and call steps of the event script. If sample code is required, you can find KBAXScript keywords in Microsoft Knowledge Bases (KB). Here are some examples
Q223139 HOWTO: Add Support for Hosting VBScript to an ATL Application Q183698 SAMPLE: AXSH.exe Demonstrates Implementing ActiveX Script Hosts Q168214 SAMPLE: MFCAxs.exe Implements an ActiveX Script Host Using MFC Q223389 FILE: Scriptng.exe Contains the Files Necessary for Implementing ActiveX Script Hosts and Engines Q232394 HOWTO: CATCH RUN-TIME ERRORS IN AN ActiveX Script Host This document is based on MFC6, so the code provided in Q168214 is used. Adding scripts for your application Support Automation To implement scripting support, we need to allow your application to have automation server support. The easiest way to achieve this support is to select the Automation support in the MDI wizard when you create an IEAUTOMATION MDI application using the Application Wizard, an Application Wizard. Wizard automatically generated CHTMLVIEW Derife CieAutomationView is not an automated object, so removes CIEAUTOMATIONVIEW definition and implementation file, then delete information in the class wizard, recreate the CIEAUTOMATIONVIEW class, and specify the base class when creating is chtmlView and supports automation. . The script interpreter in CIEAUTOMATIONVIEW is modified from the sample code of Q168214, removes some objects, increasing the implementation of the DOM extension object. The Scripter object script engine object can be accessed with the name scripter. Provide methods for creating objects. WebBrowser object browser control objects can be accessed with name webbrowser. You can use to access the document object model. Viewing the source code feature is also enhanced to display the document object model. For more information, you can view the reference to the article. External Objects DOM extension objects can be accessed with name external. Objects for the expansion of the document object model for extending the browser. In this example, I also forwarded the event of the WebBrowser object with this object. Although most of the functions are implemented, the auto-completion function seems to have a bit problem, it seems to be related to ISHELLUIHELPER AutoCompleteTtatch. The type library supports the type library information of the object to access the object, method, and events of the object. When adding type information to an object, refer to the method in Q185720 HOWTO: Provide Type Information from AN MFC Automation Server, add the application's type library to the resource. Safety Although automated browsers can provide more flexibility, this also exposes part of the application to the user. For example, the user may modify the script so that the application cannot work properly. Also, if the user can view the script, you can understand the program structure, and it can take this attack without a careful design. Use scripts to create objects or security issues. Some objects are not secure, such as malicious or incorrect COM objects. If the DOM is extended, the script on the web can access the application's feature, then make sure that the script is secure or from a dependent site. The following function is used to check security before accessing the extension attribute in the DOM.
BOOL CIEAutomationView :: CanAccessExternal () {// if the dispatch we have is safe, // we allow accessif (IsExternalDispatchSafe ()) return TRUE; // the external dispatch is not safe, so we check // whether the current zone allows for scripting // of objects that are not safe for scriptingif (m_spHtmlDoc == NULL) return FALSE; CComPtr
{
m_pcustomimpl = null;
CWND * PWND = PCNT-> m_pwnd;
IF (PWND) {
IF (PWND-> iskindof (runtime_class (cieautomationView)))) {
CIEAUTOMATIONVIEW * PVIEW = (CIEAUTOMATIONVIEW *) PWND;
m_pcustomimpl = pVIEW;
}
} This can call the control host's corresponding processing in the IDochostuiHandler2 implementation of the control customer site, such as HRESULT FAR EXPORT CCUSTOMCONTROLSIS :: XDochostuiHandler2 :: gethostinfo (DochostuiInfo * Pinfo)
{
Method_ProLogue (CCustomControlsite, DochostuiHandler2)
IF (pthis-> m_pcustomimpl) {
Return PTHIS-> M_PCUSTOMPL-> gethostinfo (PINFO);
Return S_OK;
} One of the applications of advanced host features is to extend the DOM, so that scripts on the web page can use Window.external to access the DOM extension object. The DOM extension object that IE implemented has a Menuarguments property and the ISHELLUIHELPER interface. Control a new window By default, the browser receives a new window in IE when you receive a new window request. You can handle the NewWindow2 event to open the requested page in your designated window. void CIEAutomationView :: OnNewWindow2 (LPDISPATCH * ppDisp, BOOL * Cancel) {// Get a pointer to the application object CWinApp * pApp = AfxGetApp ();. // Get the correct document template POSITION pos = pApp-> GetFirstDocTemplatePosition (). ; CDocTemplate * pDocTemplate = pApp-> GetNextDocTemplate (pos);. // Create a new frame CFrameWnd * pFrame = pDocTemplate-> CreateNewFrame (GetDocument (), (CFrameWnd *) AfxGetMainWnd ());. // Activate the frame pDocTemplate- > InitialUpdateFrame (pFrame, NULL); CIEAutomationView * pView = (CIEAutomationView *) pFrame-> GetActiveView (); // Pass pointer of WebBrowser object pView-> SetRegisterAsBrowser (TRUE);. * ppDisp = pView-> GetApplication ();} If you need more information, see Q184876 HOWTO: Use The WebBrowser Control NewWindow2 Event. . MDI Browser This document is based on a browser. In order to save trouble, modifications are made directly on the MFC's MFCIE example, and it is changed to the MDI structure. Mfcie itself is a simple browser, but after moving the code from the main frame to the child framework, the dynamically established Favorites menu is invisible. This is because the MDI framework menu replacement mechanism restores the default menu when the framework is activated, so I overloaded CDocument :: getDefaultMenu, replaced me in the MDI framework (see Q145857 How to Use Multiple Menus In MFC App That Uses getDefaultmenu). In order to obtain a document pointer during the creation of the subframe to modify the menu saved in the document, you can get the MDI to create the context from the creation structure.
Mdicreatestruct * pmdicreatestruct = (mdicreateStruct *) lpcreateStruct-> lpcreateparams;
CcreateContext * pcreateContext = (ccreatecontext *) pmdicreatestruct-> lparam;
PMENU = (CIEAUTOMATIONDOC *) PCReateContext-> m_pcurrentdoc) -> m_menudefault.getsubMenu (3); Favorites MFCIE example demonstrates how to create a favorites menu, but display Favorites in the porting toolbar to MDI sub-frame When I met a question, the location of the favorite collection clip menu is not fixed. But by finding the new "Add to Favorites" command, you can find this menu. The function added to the favorites and manages favorites is implemented by creating a shelluiHelper object. Automatic completion is convenient to use, I also added automatic completion functions in the application. The automatic completion of the address bar is relatively simple, and the API Shautocomplete of the calling system is OK. In the case where I have expanded the DOM, the automatic completion of the form seems to have some problems. If you need more information, you can refer to the 9CBS Document Center article I translated to integrate automatic completion in the application. Access requires some sites that some sites need to authenticate user identity, but by default, the browser control does not prompt the user to enter the username and password when the user cannot verify the user identity. Implement the IServiceProvider interface through the customer site of the control, and simultaneously implement the IAUThenticate interface, the application has the function of entering authentication information. For more information, see Microsoft Knowledge Base Article Q329802 Error: Agent authentication performed by IAUThenticate may fail on the security URL. Commonly used commands For the convenience of use, add the call to the Find dialog, view the source code, and set the Internet option. This is achieved by querying the iOleCommandTarget interface of the browser control, execute the command group CGID_WEBBROWSER command. The method of implementing such a command is not only one, for example, can load INETCPL.CPL, call the function launchinternetControlPanel to implement the Internet option; perform search, positioning, and selection using the IMARKUPServices interface, and the content of the web page to get / set the web page. In the sample code, demonstrate how to analyze document structures, and how to edit the selected web page elements of HTML code or the source file of the frame. MFC6bug's repair Although the application can have a relatively complete function, in order to make the application work for a long time, you need to fix some of the questions contained in the MFC6. I only list the title title here. If you are interested, you can go to see the Microsoft Knowledge Base article or this document.
Q241750 BUG: CHtmlView Leaks Memory by Not Releasing BSTRs in Several Methods Q220021 BUG: CHtmlView Does Not Repaint Correctly in an AppWizard-Generated Application Q253219 PRB MDI: WebBrowser Control Disappears When Script Calls window.close () Script Example WebBrowser.Navigate "About:
SET MSWORD = Scripter.hostcreateObject ("word.basic")
Msword.Appshow
Msword.filenew
Msword.insert "Hello"
Sub External_BeforenaviGate2 (PDISP, URL, Flags, TargetFramen, PostData, Headers, Cancel)
MSGBOX URL
End Sub If you write a script for an event for the WebBrowser object, you will find that these event processing code will not be executed, because ChtmlView processes these events. You can trigger the corresponding event of custom objects in your CHTMLVIEW derived class. In the sample code, I forwarded the event to the BeforenaviGate2 event to the custom object. Conclusion Adding scripts to applications can significantly increase the flexibility and scalability of the program. Although some performance, security, and add some code are sacrificed, this sacrifice is worth it. Although I didn't forward the DocumentComplete event in the sample code, this is only based on security considerations. Automated browsers can easily implement advertising window filtration, automatically fill out the form, page analysis and other scripts. How to write these scripts depends on your own needs. Reference browser
About The Browser Reusing Mshtml Webbrowser Customization (Custom Browser) Loading HTML Content from A Stream
About Cross-Frame Scripting and Security Document Object Model
About The DHTML Object Model About The W3c Document Object Model Working with Windows, Frames, and Dialog Boxes
Using AutoComplete In Html Forms Integrates Auto Finishing Features in your application
Author Blog:
http://blog.9cbs.net/jiangsheng/
related articles
How to detect if an application has stopped responding When the screen contains the mouse in the RicheDit control inserts dynamic GIF GIOD BYE MFC? Even 9CBS favorites (most of them are antiques ...)
Comments on this article
JIANGSHENG (2004-04-28) http://www.9cbs.net/develop/read_article.asp?id=23322 Enable Automatic completion method
JIANGSHENG (2004-03-25) Knowledge base Q199315 FIX: METHOD with BOOL * Parameter Type Overwriting Memory In Visual Basic
Cpunion (2003-11-26) is very strange, I want to know the reason. Generate 2 MDI programs, and the view classes are ChtmlView, A uses automation, b is not used. The result is exited when A does not operate, and the B is operated.
CPUNION (2003-11-25) Quote: An illegal operation occurs when the window (finally captured the WindowClosing event, Cancel After the system's processing, you turn it off) In addition to this approach, is there any other solution?
Killkillcat (2003-11-03) Jiang Ge's article all collection
【comment】
【shut down】