2017-08-12 16:22:34 +02:00
// ************************************************************************
// ***************************** CEF4Delphi *******************************
// ************************************************************************
//
// CEF4Delphi is based on DCEF3 which uses CEF3 to embed a chromium-based
// browser in Delphi applications.
//
// The original license of DCEF3 still applies to CEF4Delphi.
//
// For more information about CEF4Delphi visit :
// https://www.briskbard.com/index.php?lang=en&pageid=cef
//
2018-05-12 14:50:54 +02:00
// Copyright <20> 2018 Salvador Diaz Fau. All rights reserved.
2017-08-12 16:22:34 +02:00
//
// ************************************************************************
// ************ vvvv Original license and comments below vvvv *************
// ************************************************************************
( *
* Delphi Chromium Embedded 3
*
* Usage allowed under the restrictions of the Lesser GNU General Public License
* or alternatively the restrictions of the Mozilla Public License 1.1
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* Unit owner : Henri Gourvest < hgourvest@ gmail. com>
* Web site : http: //www.progdigy.com
* Repository : http: //code.google.com/p/delphichromiumembedded/
* Group : http: //groups.google.com/group/delphichromiumembedded
*
* Embarcadero Technologies, Inc is not permitted to use or redistribute
* this source code without explicit permission.
*
* )
unit uDOMVisitor;
{$I cef.inc}
interface
uses
{$IFDEF DELPHI16_UP}
Winapi . Windows, Winapi . Messages, System. SysUtils, System. Variants, System. Classes, Vcl. Graphics, Vcl. Menus,
Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. StdCtrls, Vcl. ExtCtrls, System. Types, Vcl. ComCtrls, Vcl. ClipBrd,
System. UITypes,
{$ELSE}
Windows, Messages, SysUtils, Variants, Classes, Graphics, Menus,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Types, ComCtrls, ClipBrd,
{$ENDIF}
uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFApplication, uCEFTypes, uCEFConstants;
const
2018-04-27 17:42:03 +02:00
MINIBROWSER_VISITDOM_PARTIAL = WM_APP + $101 ;
MINIBROWSER_VISITDOM_FULL = WM_APP + $102 ;
2017-08-12 16:22:34 +02:00
2018-04-27 17:42:03 +02:00
MINIBROWSER_CONTEXTMENU_VISITDOM_PARTIAL = MENU_ID_USER_FIRST + 1 ;
MINIBROWSER_CONTEXTMENU_VISITDOM_FULL = MENU_ID_USER_FIRST + 2 ;
2017-08-12 16:22:34 +02:00
2018-04-27 17:42:03 +02:00
DOMVISITOR_MSGNAME_PARTIAL = 'domvisitorpartial' ;
DOMVISITOR_MSGNAME_FULL = 'domvisitorfull' ;
RETRIEVEDOM_MSGNAME_PARTIAL = 'retrievedompartial' ;
RETRIEVEDOM_MSGNAME_FULL = 'retrievedomfull' ;
2017-08-23 09:50:38 +02:00
2017-08-12 16:22:34 +02:00
type
TDOMVisitorFrm = class( TForm)
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
AddressBarPnl: TPanel;
AddressEdt: TEdit;
StatusBar1: TStatusBar;
2017-11-04 18:32:29 +01:00
Timer1: TTimer;
2018-01-15 10:02:38 +01:00
Panel1: TPanel;
GoBtn: TButton;
VisitDOMBtn: TButton;
2017-08-12 16:22:34 +02:00
procedure GoBtnClick( Sender: TObject) ;
procedure FormShow( Sender: TObject) ;
procedure Chromium1AfterCreated( Sender: TObject;
const browser: ICefBrowser) ;
procedure Chromium1BeforeContextMenu( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel) ;
procedure Chromium1ContextMenuCommand( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer ;
eventFlags: Cardinal ; out Result : Boolean ) ;
procedure Chromium1ProcessMessageReceived( Sender: TObject;
const browser: ICefBrowser; sourceProcess: TCefProcessId;
const message : ICefProcessMessage; out Result : Boolean ) ;
2017-11-04 18:32:29 +01:00
procedure Timer1Timer( Sender: TObject) ;
2018-01-15 10:02:38 +01:00
procedure VisitDOMBtnClick( Sender: TObject) ;
2018-02-16 18:41:13 +01:00
procedure Chromium1BeforePopup( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
targetFrameName: ustring;
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean ;
2018-03-29 20:02:04 +02:00
const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
2018-02-16 18:41:13 +01:00
var client: ICefClient; var settings: TCefBrowserSettings;
2018-03-29 20:02:04 +02:00
var noJavascriptAccess: Boolean ; var Result : Boolean ) ;
2018-03-31 18:08:18 +02:00
procedure FormCreate( Sender: TObject) ;
procedure FormCloseQuery( Sender: TObject; var CanClose: Boolean ) ;
procedure Chromium1Close( Sender: TObject; const browser: ICefBrowser;
out Result : Boolean ) ;
procedure Chromium1BeforeClose( Sender: TObject;
const browser: ICefBrowser) ;
2017-08-12 16:22:34 +02:00
private
{ Private declarations }
protected
2018-03-31 18:08:18 +02:00
// Variables to control when can we destroy the form safely
FCanClose : boolean ; // Set to True in TChromium.OnBeforeClose
FClosing : boolean ; // Set to True in the CloseQuery event.
2017-09-07 10:58:09 +02:00
procedure BrowserCreatedMsg( var aMessage : TMessage) ; message CEF_AFTERCREATED;
2018-03-31 18:08:18 +02:00
procedure BrowserDestroyMsg( var aMessage : TMessage) ; message CEF_DESTROY;
2018-04-27 17:42:03 +02:00
procedure VisitDOMMsg( var aMessage : TMessage) ; message MINIBROWSER_VISITDOM_PARTIAL;
procedure VisitDOM2Msg( var aMessage : TMessage) ; message MINIBROWSER_VISITDOM_FULL;
2017-08-12 16:22:34 +02:00
procedure WMMove( var aMessage : TWMMove) ; message WM_MOVE;
procedure WMMoving( var aMessage : TMessage) ; message WM_MOVING;
procedure ShowStatusText( const aText : string ) ;
public
{ Public declarations }
end ;
var
DOMVisitorFrm: TDOMVisitorFrm;
2018-06-17 14:18:11 +02:00
procedure CreateGlobalCEFApp;
2017-12-18 19:38:56 +01:00
2017-08-12 16:22:34 +02:00
implementation
{$R *.dfm}
uses
2017-12-18 19:38:56 +01:00
uCEFProcessMessage, uCEFMiscFunctions, uCEFSchemeRegistrar, uCEFRenderProcessHandler,
uCEFv8Handler, uCEFDomVisitor, uCEFDomNode, uCEFTask;
2017-12-30 09:54:26 +01:00
// This demo sends messages from the browser process to the render process,
// and from the render process to the browser process.
// To send a message from the browser process you must use the TChromium.SendProcessMessage
// procedure with a PID_RENDERER parameter. The render process receives those messages in
2018-02-03 17:52:48 +01:00
// the GlobalCEFApp.OnProcessMessageReceived event.
2017-12-30 09:54:26 +01:00
// To send messages from the render process you must use the browser.SendProcessMessage
// procedure with a PID_BROWSER parameter. The browser process receives those messages in
// the TChromium.OnProcessMessageReceived event.
// message.name is used to identify different messages sent with SendProcessMessage.
// The OnProcessMessageReceived event can recognize any number of messages identifying them
// by message.name
2018-03-31 18:08:18 +02:00
// Destruction steps
// =================
// 1. FormCloseQuery sets CanClose to FALSE calls TChromium.CloseBrowser which triggers the TChromium.OnClose event.
// 2. TChromium.OnClose sends a CEFBROWSER_DESTROY message to destroy CEFWindowParent1 in the main thread, which triggers the TChromium.OnBeforeClose event.
// 3. TChromium.OnBeforeClose sets FCanClose := True and sends WM_CLOSE to the form.
2017-12-18 19:38:56 +01:00
procedure SimpleDOMIteration( const aDocument: ICefDomDocument) ;
var
TempHead, TempChild : ICefDomNode;
begin
try
if ( aDocument < > nil ) then
begin
TempHead : = aDocument. Head;
if ( TempHead < > nil ) then
begin
TempChild : = TempHead. FirstChild;
while ( TempChild < > nil ) do
begin
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'Head child element : ' + TempChild. Name ) ;
TempChild : = TempChild. NextSibling;
end ;
end ;
end ;
except
on e : exception do
if CustomExceptionHandler( 'SimpleDOMIteration' , e) then raise ;
end ;
end ;
procedure SimpleNodeSearch( const aDocument: ICefDomDocument) ;
const
2018-01-25 21:34:04 +01:00
NODE_ID = 'lst-ib' ; // input box node found in google.com homepage
2017-12-18 19:38:56 +01:00
var
TempNode : ICefDomNode;
begin
try
if ( aDocument < > nil ) then
begin
TempNode : = aDocument. GetElementById( NODE_ID) ;
if ( TempNode < > nil ) then
2018-01-25 21:34:04 +01:00
begin
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, NODE_ID + ' element name : ' + TempNode. Name ) ;
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, NODE_ID + ' element value : ' + TempNode. GetValue) ;
end ;
2017-12-18 19:38:56 +01:00
TempNode : = aDocument. GetFocusedNode;
if ( TempNode < > nil ) then
2018-01-25 21:34:04 +01:00
begin
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'Focused element name : ' + TempNode. Name ) ;
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'Focused element inner text : ' + TempNode. ElementInnerText) ;
end ;
2017-12-18 19:38:56 +01:00
end ;
except
on e : exception do
if CustomExceptionHandler( 'SimpleNodeSearch' , e) then raise ;
end ;
end ;
procedure DOMVisitor_OnDocAvailable( const browser: ICefBrowser; const document: ICefDomDocument) ;
var
msg: ICefProcessMessage;
begin
// This function is called from a different process.
// document is only valid inside this function.
// As an example, this function only writes the document title to the 'debug.log' file.
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'document.Title : ' + document. Title) ;
2018-01-31 16:23:32 +01:00
if document. HasSelection then
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'document.SelectionAsText : ' + quotedstr( document. SelectionAsText) )
else
CefLog( 'CEF4Delphi' , 1 , CEF_LOG_SEVERITY_ERROR, 'document.HasSelection : False' ) ;
2017-12-18 19:38:56 +01:00
// Simple DOM iteration example
SimpleDOMIteration( document) ;
// Simple DOM searches
SimpleNodeSearch( document) ;
// Sending back some custom results to the browser process
2018-04-27 17:42:03 +02:00
// Notice that the DOMVISITOR_MSGNAME_PARTIAL message name needs to be recognized in
2017-12-18 19:38:56 +01:00
// Chromium1ProcessMessageReceived
2018-04-27 17:42:03 +02:00
msg : = TCefProcessMessageRef. New( DOMVISITOR_MSGNAME_PARTIAL) ;
2017-12-18 19:38:56 +01:00
msg. ArgumentList. SetString( 0 , 'document.Title : ' + document. Title) ;
browser. SendProcessMessage( PID_BROWSER, msg) ;
end ;
2018-04-27 17:42:03 +02:00
procedure DOMVisitor_OnDocAvailableFullMarkup( const browser: ICefBrowser; const document: ICefDomDocument) ;
var
msg: ICefProcessMessage;
begin
// Sending back some custom results to the browser process
// Notice that the DOMVISITOR_MSGNAME_FULL message name needs to be recognized in
// Chromium1ProcessMessageReceived
msg : = TCefProcessMessageRef. New( DOMVISITOR_MSGNAME_FULL) ;
msg. ArgumentList. SetString( 0 , document. Body. AsMarkup) ;
browser. SendProcessMessage( PID_BROWSER, msg) ;
end ;
2017-12-18 19:38:56 +01:00
procedure GlobalCEFApp_OnProcessMessageReceived( const browser : ICefBrowser;
sourceProcess : TCefProcessId;
const message : ICefProcessMessage;
var aHandled : boolean ) ;
var
TempFrame : ICefFrame;
TempVisitor : TCefFastDomVisitor2;
begin
2018-04-27 17:42:03 +02:00
aHandled : = False ;
2017-12-18 19:38:56 +01:00
2018-04-27 17:42:03 +02:00
if ( browser < > nil ) then
begin
if ( message . name = RETRIEVEDOM_MSGNAME_PARTIAL) then
2017-12-18 19:38:56 +01:00
begin
2018-04-27 17:42:03 +02:00
TempFrame : = browser. MainFrame;
if ( TempFrame < > nil ) then
begin
TempVisitor : = TCefFastDomVisitor2. Create( browser, DOMVisitor_OnDocAvailable) ;
TempFrame. VisitDom( TempVisitor) ;
end ;
aHandled : = True ;
end
else
if ( message . name = RETRIEVEDOM_MSGNAME_FULL) then
begin
TempFrame : = browser. MainFrame;
2017-12-18 19:38:56 +01:00
2018-04-27 17:42:03 +02:00
if ( TempFrame < > nil ) then
begin
TempVisitor : = TCefFastDomVisitor2. Create( browser, DOMVisitor_OnDocAvailableFullMarkup) ;
TempFrame. VisitDom( TempVisitor) ;
end ;
aHandled : = True ;
end ;
end ;
2017-12-18 19:38:56 +01:00
end ;
2017-08-12 16:22:34 +02:00
2018-06-17 14:18:11 +02:00
procedure CreateGlobalCEFApp;
begin
GlobalCEFApp : = TCefApplication. Create;
GlobalCEFApp. RemoteDebuggingPort : = 9 0 0 0 ;
GlobalCEFApp. OnProcessMessageReceived : = GlobalCEFApp_OnProcessMessageReceived;
// Enabling the debug log file for then DOM visitor demo.
// This adds lots of warnings to the console, specially if you run this inside VirtualBox.
// Remove it if you don't want to use the DOM visitor
GlobalCEFApp. LogFile : = 'debug.log' ;
GlobalCEFApp. LogSeverity : = LOGSEVERITY_ERROR;
end ;
2017-09-07 10:58:09 +02:00
procedure TDOMVisitorFrm. Chromium1AfterCreated( Sender: TObject; const browser: ICefBrowser) ;
2017-08-12 16:22:34 +02:00
begin
2017-09-07 10:58:09 +02:00
PostMessage( Handle, CEF_AFTERCREATED, 0 , 0 ) ;
2017-08-12 16:22:34 +02:00
end ;
2018-03-31 18:08:18 +02:00
procedure TDOMVisitorFrm. Chromium1BeforeClose( Sender: TObject;
const browser: ICefBrowser) ;
begin
FCanClose : = True ;
PostMessage( Handle, WM_CLOSE, 0 , 0 ) ;
end ;
2017-08-12 16:22:34 +02:00
procedure TDOMVisitorFrm. Chromium1BeforeContextMenu( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; const model: ICefMenuModel) ;
begin
2018-04-27 17:42:03 +02:00
model. AddItem( MINIBROWSER_CONTEXTMENU_VISITDOM_PARTIAL, 'Visit DOM in CEF (only Title)' ) ;
model. AddItem( MINIBROWSER_CONTEXTMENU_VISITDOM_FULL, 'Visit DOM in CEF (BODY HTML)' ) ;
2017-08-12 16:22:34 +02:00
end ;
2018-02-16 18:41:13 +01:00
procedure TDOMVisitorFrm. Chromium1BeforePopup( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
2018-03-29 20:02:04 +02:00
userGesture: Boolean ; const popupFeatures: TCefPopupFeatures;
2018-02-16 18:41:13 +01:00
var windowInfo: TCefWindowInfo; var client: ICefClient;
var settings: TCefBrowserSettings; var noJavascriptAccess: Boolean ;
2018-03-29 20:02:04 +02:00
var Result : Boolean ) ;
2018-02-16 18:41:13 +01:00
begin
// For simplicity, this demo blocks all popup windows and new tabs
Result : = ( targetDisposition in [ WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW] ) ;
end ;
2018-03-31 18:08:18 +02:00
procedure TDOMVisitorFrm. Chromium1Close( Sender: TObject;
const browser: ICefBrowser; out Result : Boolean ) ;
begin
PostMessage( Handle, CEF_DESTROY, 0 , 0 ) ;
Result : = True ;
end ;
2017-08-12 16:22:34 +02:00
procedure TDOMVisitorFrm. Chromium1ContextMenuCommand( Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const params: ICefContextMenuParams; commandId: Integer ;
eventFlags: Cardinal ; out Result : Boolean ) ;
begin
Result : = False ;
case commandId of
2018-04-27 17:42:03 +02:00
MINIBROWSER_CONTEXTMENU_VISITDOM_PARTIAL :
PostMessage( Handle, MINIBROWSER_VISITDOM_PARTIAL, 0 , 0 ) ;
MINIBROWSER_CONTEXTMENU_VISITDOM_FULL :
PostMessage( Handle, MINIBROWSER_VISITDOM_FULL, 0 , 0 ) ;
2017-08-12 16:22:34 +02:00
end ;
end ;
procedure TDOMVisitorFrm. Chromium1ProcessMessageReceived( Sender: TObject;
const browser: ICefBrowser; sourceProcess: TCefProcessId;
const message : ICefProcessMessage; out Result : Boolean ) ;
begin
2017-09-08 17:27:05 +02:00
Result : = False ;
2017-08-12 16:22:34 +02:00
if ( message = nil ) or ( message . ArgumentList = nil ) then exit;
2018-04-27 17:42:03 +02:00
// Message received from the DOMVISITOR in CEF
if ( message . Name = DOMVISITOR_MSGNAME_PARTIAL) then
2017-08-12 16:22:34 +02:00
begin
ShowStatusText( 'DOM Visitor result text : ' + message . ArgumentList. GetString( 0 ) ) ;
Result : = True ;
2018-04-27 17:42:03 +02:00
end
else
if ( message . Name = DOMVISITOR_MSGNAME_FULL) then
begin
Clipboard. AsText : = message . ArgumentList. GetString( 0 ) ;
ShowStatusText( 'HTML copied to the clipboard' ) ;
Result : = True ;
end ;
2017-08-12 16:22:34 +02:00
end ;
2018-03-31 18:08:18 +02:00
procedure TDOMVisitorFrm. FormCloseQuery( Sender: TObject;
var CanClose: Boolean ) ;
begin
CanClose : = FCanClose;
if not( FClosing) then
begin
FClosing : = True ;
Visible : = False ;
Chromium1. CloseBrowser( True ) ;
end ;
end ;
procedure TDOMVisitorFrm. FormCreate( Sender: TObject) ;
begin
FCanClose : = False ;
FClosing : = False ;
end ;
2017-08-12 16:22:34 +02:00
procedure TDOMVisitorFrm. FormShow( Sender: TObject) ;
begin
2017-11-04 18:32:29 +01:00
// GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser
// If it's not initialized yet, we use a simple timer to create the browser later.
if not( Chromium1. CreateBrowser( CEFWindowParent1, '' ) ) then Timer1. Enabled : = True ;
2017-08-12 16:22:34 +02:00
end ;
procedure TDOMVisitorFrm. GoBtnClick( Sender: TObject) ;
begin
Chromium1. LoadURL( AddressEdt. Text ) ;
end ;
procedure TDOMVisitorFrm. BrowserCreatedMsg( var aMessage : TMessage) ;
begin
2017-08-23 12:28:45 +02:00
CEFWindowParent1. UpdateSize;
2017-08-12 16:22:34 +02:00
AddressBarPnl. Enabled : = True ;
GoBtn. Click;
end ;
2018-03-31 18:08:18 +02:00
procedure TDOMVisitorFrm. BrowserDestroyMsg( var aMessage : TMessage) ;
begin
CEFWindowParent1. Free;
end ;
2018-01-15 10:02:38 +01:00
procedure TDOMVisitorFrm. VisitDOMBtnClick( Sender: TObject) ;
begin
2018-04-27 17:42:03 +02:00
PostMessage( Handle, MINIBROWSER_VISITDOM_PARTIAL, 0 , 0 ) ;
2018-01-15 10:02:38 +01:00
end ;
2017-08-12 16:22:34 +02:00
procedure TDOMVisitorFrm. VisitDOMMsg( var aMessage : TMessage) ;
var
TempMsg : ICefProcessMessage;
begin
// Use the ArgumentList property if you need to pass some parameters.
2018-04-27 17:42:03 +02:00
TempMsg : = TCefProcessMessageRef. New( RETRIEVEDOM_MSGNAME_PARTIAL) ; // Same name than TCefCustomRenderProcessHandler.MessageName
Chromium1. SendProcessMessage( PID_RENDERER, TempMsg) ;
end ;
procedure TDOMVisitorFrm. VisitDOM2Msg( var aMessage : TMessage) ;
var
TempMsg : ICefProcessMessage;
begin
// Use the ArgumentList property if you need to pass some parameters.
TempMsg : = TCefProcessMessageRef. New( RETRIEVEDOM_MSGNAME_FULL) ; // Same name than TCefCustomRenderProcessHandler.MessageName
2017-08-12 16:22:34 +02:00
Chromium1. SendProcessMessage( PID_RENDERER, TempMsg) ;
end ;
procedure TDOMVisitorFrm. WMMove( var aMessage : TWMMove) ;
begin
inherited ;
if ( Chromium1 < > nil ) then Chromium1. NotifyMoveOrResizeStarted;
end ;
procedure TDOMVisitorFrm. WMMoving( var aMessage : TMessage) ;
begin
inherited ;
if ( Chromium1 < > nil ) then Chromium1. NotifyMoveOrResizeStarted;
end ;
procedure TDOMVisitorFrm. ShowStatusText( const aText : string ) ;
begin
StatusBar1. Panels[ 0 ] . Text : = aText;
end ;
2017-11-04 18:32:29 +01:00
procedure TDOMVisitorFrm. Timer1Timer( Sender: TObject) ;
begin
Timer1. Enabled : = False ;
2017-11-16 12:49:15 +01:00
if not( Chromium1. CreateBrowser( CEFWindowParent1, '' ) ) and not( Chromium1. Initialized) then
Timer1. Enabled : = True ;
2017-11-04 18:32:29 +01:00
end ;
2017-08-12 16:22:34 +02:00
end .