unit uMiniBrowser; {$I ..\..\..\source\cef.inc} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Menus, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Types, ComCtrls, ClipBrd, ActiveX, ShlObj, uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFApplication, uCEFTypes, uCEFConstants, uCEFWinControl, uCEFChromiumEvents; const MINIBROWSER_SHOWDEVTOOLS = WM_APP + $101; MINIBROWSER_HIDEDEVTOOLS = WM_APP + $102; MINIBROWSER_COPYHTML = WM_APP + $103; MINIBROWSER_SHOWRESPONSE = WM_APP + $104; MINIBROWSER_COPYFRAMEIDS = WM_APP + $105; MINIBROWSER_COPYFRAMENAMES = WM_APP + $106; MINIBROWSER_SAVEPREFERENCES = WM_APP + $107; MINIBROWSER_COPYALLTEXT = WM_APP + $108; MINIBROWSER_TAKESNAPSHOT = WM_APP + $109; MINIBROWSER_SHOWNAVIGATION = WM_APP + $10A; MINIBROWSER_COOKIESFLUSHED = WM_APP + $10B; MINIBROWSER_PDFPRINT_END = WM_APP + $10C; MINIBROWSER_PREFS_AVLBL = WM_APP + $10D; MINIBROWSER_DTDATA_AVLBL = WM_APP + $10E; MINIBROWSER_MEDIAACCESSRQST = WM_APP + $10F; MINIBROWSER_HOMEPAGE = 'https://www.google.com'; MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS = MENU_ID_USER_FIRST + 1; MINIBROWSER_CONTEXTMENU_HIDEDEVTOOLS = MENU_ID_USER_FIRST + 2; MINIBROWSER_CONTEXTMENU_COPYHTML = MENU_ID_USER_FIRST + 3; MINIBROWSER_CONTEXTMENU_JSWRITEDOC = MENU_ID_USER_FIRST + 4; MINIBROWSER_CONTEXTMENU_JSPRINTDOC = MENU_ID_USER_FIRST + 5; MINIBROWSER_CONTEXTMENU_SHOWRESPONSE = MENU_ID_USER_FIRST + 6; MINIBROWSER_CONTEXTMENU_COPYFRAMEIDS = MENU_ID_USER_FIRST + 7; MINIBROWSER_CONTEXTMENU_COPYFRAMENAMES = MENU_ID_USER_FIRST + 8; MINIBROWSER_CONTEXTMENU_SAVEPREFERENCES = MENU_ID_USER_FIRST + 9; MINIBROWSER_CONTEXTMENU_COPYALLTEXT = MENU_ID_USER_FIRST + 10; MINIBROWSER_CONTEXTMENU_TAKESNAPSHOT = MENU_ID_USER_FIRST + 11; MINIBROWSER_CONTEXTMENU_GETNAVIGATION = MENU_ID_USER_FIRST + 12; MINIBROWSER_CONTEXTMENU_MUTEAUDIO = MENU_ID_USER_FIRST + 13; MINIBROWSER_CONTEXTMENU_UNMUTEAUDIO = MENU_ID_USER_FIRST + 14; MINIBROWSER_CONTEXTMENU_INCZOOM = MENU_ID_USER_FIRST + 15; MINIBROWSER_CONTEXTMENU_DECZOOM = MENU_ID_USER_FIRST + 16; MINIBROWSER_CONTEXTMENU_RESETZOOM = MENU_ID_USER_FIRST + 17; MINIBROWSER_CONTEXTMENU_EXITFULLSCREEN = MENU_ID_USER_FIRST + 18; DEVTOOLS_SCREENSHOT_MSGID = 1; DEVTOOLS_MHTML_MSGID = 2; type { TMiniBrowserFrm } TMiniBrowserFrm = class(TForm) MenuItem1: TMenuItem; MenuItem2: TMenuItem; MenuItem3: TMenuItem; MenuItem4: TMenuItem; MenuItem5: TMenuItem; MenuItem6: TMenuItem; Allowdownloads1: TMenuItem; NavControlPnl: TPanel; NavButtonPnl: TPanel; StatusPnl: TPanel; URLEditPnl: TPanel; BackBtn: TButton; ForwardBtn: TButton; ReloadBtn: TButton; CEFWindowParent1: TCEFWindowParent; Chromium1: TChromium; StopBtn: TButton; DevTools: TCEFWindowParent; Splitter1: TSplitter; URLCbx: TComboBox; ConfigPnl: TPanel; ConfigBtn: TButton; PopupMenu1: TPopupMenu; DevTools1: TMenuItem; N1: TMenuItem; Preferences1: TMenuItem; GoBtn: TButton; N2: TMenuItem; PrintinPDF1: TMenuItem; Print1: TMenuItem; N3: TMenuItem; Zoom1: TMenuItem; Inczoom1: TMenuItem; Deczoom1: TMenuItem; Resetzoom1: TMenuItem; SaveDialog1: TSaveDialog; ApplicationEvents1: TApplicationProperties; OpenDialog1: TOpenDialog; N4: TMenuItem; Openfile1: TMenuItem; Resolvehost1: TMenuItem; Timer1: TTimer; OpenfilewithaDAT1: TMenuItem; N5: TMenuItem; Memoryinfo1: TMenuItem; procedure Allowdownloads1Click(Sender: TObject); procedure CEFWindowParent1DragDrop(Sender, Source: TObject; X, Y: Integer); procedure Chromium1CanDownload(Sender: TObject; const browser: ICefBrowser; const url, request_method: ustring; var aResult: boolean); procedure Chromium1CookiesFlushed(Sender: TObject); procedure Chromium1CursorChange(Sender: TObject; const browser: ICefBrowser; cursor_: TCefCursorHandle; cursorType: TCefCursorType; const customCursorInfo: PCefCursorInfo; var aResult: boolean); procedure Chromium1DevToolsMethodResult(Sender: TObject; const browser: ICefBrowser; message_id: integer; success: boolean; const result: ICefValue); procedure Chromium1DownloadImageFinished(Sender: TObject; const imageUrl: ustring; httpStatusCode: Integer; const image: ICefImage); procedure Chromium1MediaAccessChange(Sender: TObject; const browser: ICefBrowser; has_video_access, has_audio_access: boolean); procedure Chromium1RequestMediaAccessPermission(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const requesting_origin: ustring; requested_permissions: cardinal; const callback: ICefMediaAccessCallback; var aResult: boolean); procedure Chromium1ZoomPctAvailable(Sender: TObject; const aZoomPct: double ); procedure FormShow(Sender: TObject); procedure BackBtnClick(Sender: TObject); procedure ForwardBtnClick(Sender: TObject); procedure MenuItem1Click(Sender: TObject); procedure MenuItem2Click(Sender: TObject); procedure MenuItem3Click(Sender: TObject); procedure MenuItem4Click(Sender: TObject); procedure MenuItem5Click(Sender: TObject); procedure MenuItem6Click(Sender: TObject); procedure ReloadBtnClick(Sender: TObject); procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser); procedure Chromium1LoadingStateChange(Sender: TObject; const browser: ICefBrowser; isLoading, canGoBack, canGoForward: Boolean); procedure Chromium1TitleChange(Sender: TObject; const browser: ICefBrowser; const title: ustring); procedure Chromium1AddressChange(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const url: ustring); procedure Chromium1BeforeContextMenu(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); procedure Chromium1StatusMessage(Sender: TObject; const browser: ICefBrowser; const value: ustring); procedure Chromium1TextResultAvailable(Sender: TObject; const aText: ustring); procedure PopupMenu1Popup(Sender: TObject); procedure DevTools1Click(Sender: TObject); procedure Preferences1Click(Sender: TObject); procedure ConfigBtnClick(Sender: TObject); procedure GoBtnClick(Sender: TObject); procedure PrintinPDF1Click(Sender: TObject); procedure Print1Click(Sender: TObject); procedure Inczoom1Click(Sender: TObject); procedure Deczoom1Click(Sender: TObject); procedure Resetzoom1Click(Sender: TObject); procedure Chromium1FullScreenModeChange(Sender: TObject; const browser: ICefBrowser; fullscreen: Boolean); procedure Chromium1PreKeyEvent(Sender: TObject; const browser: ICefBrowser; const event: PCefKeyEvent; osEvent: TCefEventHandle; out isKeyboardShortcut, Result: Boolean); procedure Chromium1KeyEvent(Sender: TObject; const browser: ICefBrowser; const event: PCefKeyEvent; osEvent: TCefEventHandle; out Result: Boolean); procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); procedure Openfile1Click(Sender: TObject); procedure Chromium1ContextMenuCommand(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; eventFlags: Cardinal; out Result: Boolean); procedure Chromium1PdfPrintFinished(Sender: TObject; aResultOK: Boolean); procedure Chromium1ResourceResponse(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const response: ICefResponse; out Result: Boolean); procedure StopBtnClick(Sender: TObject); procedure Resolvehost1Click(Sender: TObject); procedure Chromium1ResolvedHostAvailable(Sender: TObject; result: Integer; const resolvedIps: TStrings); procedure Timer1Timer(Sender: TObject); procedure Chromium1PrefsAvailable(Sender: TObject; aResultOK: Boolean); procedure Chromium1BeforeDownload(Sender: TObject; const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback); procedure Chromium1DownloadUpdated(Sender: TObject; const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Chromium1BeforeResourceLoad(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const callback: ICefCallback; out Result: TCefReturnValue); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction); procedure Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser); procedure Chromium1RenderCompMsg(Sender: TObject; var aMessage : TMessage; var aHandled: Boolean); procedure Chromium1LoadingProgressChange(Sender: TObject; const browser: ICefBrowser; const progress: Double); procedure OpenfilewithaDAT1Click(Sender: TObject); procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer); procedure Memoryinfo1Click(Sender: TObject); procedure Chromium1LoadError(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; errorCode: Integer; const errorText, failedUrl: ustring); procedure Chromium1CertificateError(Sender: TObject; const browser: ICefBrowser; certError: Integer; const requestUrl: ustring; const sslInfo: ICefSslInfo; const callback: ICefCallback; out Result: Boolean); procedure Chromium1NavigationVisitorResultAvailable(Sender: TObject; const entry: ICefNavigationEntry; current: Boolean; index, total: Integer; var aResult: Boolean); protected FResponse : TStringList; FRequest : TStringList; FNavigation : TStringList; FShutdownReason : string; FHasShutdownReason : boolean; FAllowDownloads : boolean; FMediaAccessCallback : ICefMediaAccessCallback; FRequestingOrigin : string; FRequestedPermissions : cardinal; // 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. FPendingMsgID : integer; FDevToolsMsgValue : ustring; procedure AddURL(const aURL : string); procedure ShowDevTools(aPoint : TPoint); overload; procedure ShowDevTools; overload; procedure HideDevTools; procedure HandleKeyUp(const aMsg : TMsg; var aHandled : boolean); procedure HandleKeyDown(const aMsg : TMsg; var aHandled : boolean); procedure InspectRequest(const aRequest : ICefRequest); procedure InspectResponse(const aResponse : ICefResponse); procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED; procedure BrowserDestroyMsg(var aMessage : TMessage); message CEF_DESTROY; procedure ShowDevToolsMsg(var aMessage : TMessage); message MINIBROWSER_SHOWDEVTOOLS; procedure HideDevToolsMsg(var aMessage : TMessage); message MINIBROWSER_HIDEDEVTOOLS; procedure CopyAllTextMsg(var aMessage : TMessage); message MINIBROWSER_COPYALLTEXT; procedure CopyHTMLMsg(var aMessage : TMessage); message MINIBROWSER_COPYHTML; procedure CopyFramesIDsMsg(var aMessage : TMessage); message MINIBROWSER_COPYFRAMEIDS; procedure CopyFramesNamesMsg(var aMessage : TMessage); message MINIBROWSER_COPYFRAMENAMES; procedure ShowResponseMsg(var aMessage : TMessage); message MINIBROWSER_SHOWRESPONSE; procedure ShowNavigationMsg(var aMessage : TMessage); message MINIBROWSER_SHOWNAVIGATION; procedure SavePreferencesMsg(var aMessage : TMessage); message MINIBROWSER_SAVEPREFERENCES; procedure TakeSnapshotMsg(var aMessage : TMessage); message MINIBROWSER_TAKESNAPSHOT; procedure CookiesFlushedMsg(var aMessage : TMessage); message MINIBROWSER_COOKIESFLUSHED; procedure PrintPDFEndMsg(var aMessage : TMessage); message MINIBROWSER_PDFPRINT_END; procedure PreferencesAvailableMsg(var aMessage : TMessage); message MINIBROWSER_PREFS_AVLBL; procedure DevToolsDataAvailableMsg(var aMessage : TMessage); message MINIBROWSER_DTDATA_AVLBL; procedure MediaAccessRequestMsg(var aMessage : TMessage); message MINIBROWSER_MEDIAACCESSRQST; procedure WMMove(var aMessage : TWMMove); message WM_MOVE; procedure WMMoving(var aMessage : TMessage); message WM_MOVING; procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP; procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP; procedure WMQueryEndSession(var aMessage: TWMQueryEndSession); message WM_QUERYENDSESSION; public procedure ShowStatusText(const aText : string); end; var MiniBrowserFrm : TMiniBrowserFrm; procedure CreateGlobalCEFApp; implementation {$R *.lfm} uses uPreferences, uCefStringMultimap, uCEFMiscFunctions, uSimpleTextViewer, uCefClient, uCEFDictionaryValue; // 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. procedure CreateGlobalCEFApp; begin GlobalCEFApp := TCefApplication.Create; GlobalCEFApp.cache := 'cache'; GlobalCEFApp.LogFile := 'debug.log'; GlobalCEFApp.LogSeverity := LOGSEVERITY_INFO; GlobalCEFApp.EnablePrintPreview := True; GlobalCEFApp.EnableGPU := True; end; procedure TMiniBrowserFrm.BackBtnClick(Sender: TObject); begin Chromium1.GoBack; end; procedure TMiniBrowserFrm.ForwardBtnClick(Sender: TObject); begin Chromium1.GoForward; end; procedure TMiniBrowserFrm.MenuItem1Click(Sender: TObject); var TempURL : string; begin TempURL := InputBox('Download Image', 'URL:', 'https://www.briskbard.com/images/logo.png'); if (length(TempURL) > 0) then Chromium1.DownloadImage(TempURL, False, 0, False); end; procedure TMiniBrowserFrm.MenuItem2Click(Sender: TObject); const SIMULATED_KEY_PRESSES = 'QWERTY'; var i : integer; TempKeyEvent : TCefKeyEvent; begin // This procedure is extremely simplified. // Use the SimpleOSRBrowser demo to log the real TCefKeyEvent values // if you use anything different than uppercase letters. for i := 1 to length(SIMULATED_KEY_PRESSES) do begin // WM_KEYDOWN TempKeyEvent.kind := KEYEVENT_RAWKEYDOWN; TempKeyEvent.modifiers := 0; TempKeyEvent.windows_key_code := ord(SIMULATED_KEY_PRESSES[i]); TempKeyEvent.native_key_code := 0; TempKeyEvent.is_system_key := ord(False); TempKeyEvent.character := #0; TempKeyEvent.unmodified_character := #0; TempKeyEvent.focus_on_editable_field := ord(False); Chromium1.SendKeyEvent(@TempKeyEvent); // WM_CHAR TempKeyEvent.kind := KEYEVENT_CHAR; Chromium1.SendKeyEvent(@TempKeyEvent); // WM_KEYUP TempKeyEvent.kind := KEYEVENT_KEYUP; Chromium1.SendKeyEvent(@TempKeyEvent); end; end; procedure TMiniBrowserFrm.MenuItem3Click(Sender: TObject); begin if not(Chromium1.FlushCookieStore(False)) then showmessage('There was a problem flushing the cookies.'); end; procedure TMiniBrowserFrm.MenuItem4Click(Sender: TObject); var TempLanguageList : ustring; begin TempLanguageList := Chromium1.AcceptLanguageList; if (length(TempLanguageList) = 0) then TempLanguageList := GlobalCEFApp.AcceptLanguageList; Chromium1.AcceptLanguageList := InputBox('Language', 'Accept language list', TempLanguageList); end; procedure TMiniBrowserFrm.MenuItem5Click(Sender: TObject); var TempInfo : string; begin TempInfo := 'libcef.dll : ' + CRLF + GlobalCEFApp.LibCefVersion + CRLF + CRLF + 'chrome_elf.dll : ' + CRLF + GlobalCEFApp.ChromeVersion + CRLF + CRLF + 'Universal API hash : ' + CRLF + GlobalCEFApp.ApiHashUniversal + CRLF + CRLF + 'Platform API hash : ' + CRLF + GlobalCEFApp.ApiHashPlatform + CRLF + CRLF + 'Commit API hash : ' + CRLF + GlobalCEFApp.ApiHashCommit; showmessage(TempInfo); end; procedure TMiniBrowserFrm.MenuItem6Click(Sender: TObject); var TempParams : ICefDictionaryValue; begin try TempParams := TCefDictionaryValueRef.New; TempParams.SetString('format', 'mhtml'); FPendingMsgID := DEVTOOLS_MHTML_MSGID; Chromium1.ExecuteDevToolsMethod(0, 'Page.captureSnapshot', TempParams); finally TempParams := nil; end; end; procedure TMiniBrowserFrm.GoBtnClick(Sender: TObject); begin Chromium1.LoadURL(URLCbx.Text); end; procedure TMiniBrowserFrm.ReloadBtnClick(Sender: TObject); begin Chromium1.Reload; end; procedure TMiniBrowserFrm.Resetzoom1Click(Sender: TObject); begin Chromium1.ResetZoomLevel; end; procedure TMiniBrowserFrm.Resolvehost1Click(Sender: TObject); var TempURL : string; begin TempURL := inputbox('Resolve host', 'URL :', 'http://google.com'); if (length(TempURL) > 0) then Chromium1.ResolveHost(TempURL); end; procedure TMiniBrowserFrm.Chromium1AddressChange(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const url: ustring); begin if Chromium1.IsSameBrowser(browser) then AddURL(url); end; procedure TMiniBrowserFrm.Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser); begin if Chromium1.IsSameBrowser(browser) then PostMessage(Handle, CEF_AFTERCREATED, 0, 0) else SendMessage(browser.Host.WindowHandle, WM_SETICON, 1, application.Icon.Handle); // Use the same icon in the popup window end; procedure TMiniBrowserFrm.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser); begin // The main browser is being destroyed if (Chromium1.BrowserId = 0) then begin FCanClose := True; PostMessage(Handle, WM_CLOSE, 0, 0); end; end; procedure TMiniBrowserFrm.Chromium1BeforeContextMenu(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); begin if Chromium1.IsSameBrowser(browser) then begin model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_TAKESNAPSHOT, 'Take snapshot...'); model.AddItem(MINIBROWSER_CONTEXTMENU_GETNAVIGATION, 'Get navigation entries'); model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_COPYALLTEXT, 'Copy displayed text to clipboard'); model.AddItem(MINIBROWSER_CONTEXTMENU_COPYHTML, 'Copy HTML to clipboard'); model.AddItem(MINIBROWSER_CONTEXTMENU_COPYFRAMEIDS, 'Copy HTML frame identifiers to clipboard'); model.AddItem(MINIBROWSER_CONTEXTMENU_COPYFRAMENAMES, 'Copy HTML frame names to clipboard'); model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_SAVEPREFERENCES, 'Save preferences as...'); model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_JSWRITEDOC, 'Modify HTML document'); model.AddItem(MINIBROWSER_CONTEXTMENU_JSPRINTDOC, 'Print using Javascript'); model.AddItem(MINIBROWSER_CONTEXTMENU_SHOWRESPONSE, 'Show server headers'); if DevTools.Visible then model.AddItem(MINIBROWSER_CONTEXTMENU_HIDEDEVTOOLS, 'Hide DevTools') else model.AddItem(MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS, 'Show DevTools'); if Chromium1.AudioMuted then model.AddItem(MINIBROWSER_CONTEXTMENU_UNMUTEAUDIO, 'Unmute audio') else model.AddItem(MINIBROWSER_CONTEXTMENU_MUTEAUDIO, 'Mute audio'); model.AddSeparator; if Chromium1.CanIncZoom then model.AddItem(MINIBROWSER_CONTEXTMENU_INCZOOM, 'Increment zoom'); if Chromium1.CanDecZoom then model.AddItem(MINIBROWSER_CONTEXTMENU_DECZOOM, 'Decrement zoom'); if Chromium1.CanResetZoom then model.AddItem(MINIBROWSER_CONTEXTMENU_RESETZOOM, 'Reset zoom'); if Chromium1.Fullscreen then begin model.AddSeparator; model.AddItem(MINIBROWSER_CONTEXTMENU_EXITFULLSCREEN, 'Exit fullscreen'); end; end else model.AddItem(MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS, 'Show DevTools'); end; function PathToMyDocuments : string; var Allocator : IMalloc; Path : pchar; idList : PItemIDList; begin Result := ''; Path := nil; idList := nil; try if (SHGetMalloc(Allocator) = S_OK) then begin GetMem(Path, MAX_PATH); if (SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, idList) = S_OK) and SHGetPathFromIDList(idList, Path) then Result := string(Path); end; finally if (Path <> nil) then FreeMem(Path); if (idList <> nil) then Allocator.Free(idList); end; end; procedure TMiniBrowserFrm.Chromium1BeforeDownload(Sender: TObject; const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback); var TempMyDocuments, TempFullPath, TempName : string; begin if not(Chromium1.IsSameBrowser(browser)) or (downloadItem = nil) or not(downloadItem.IsValid) then exit; TempMyDocuments := PathToMyDocuments; if (length(suggestedName) > 0) then TempName := suggestedName else TempName := 'DownloadedFile'; if (length(TempMyDocuments) > 0) then TempFullPath := IncludeTrailingPathDelimiter(TempMyDocuments) + TempName else TempFullPath := TempName; callback.cont(TempFullPath, False); end; procedure TMiniBrowserFrm.Chromium1BeforeResourceLoad(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const callback: ICefCallback; out Result: TCefReturnValue); begin Result := RV_CONTINUE; if Chromium1.IsSameBrowser(browser) and (frame <> nil) and frame.IsValid and frame.IsMain then InspectRequest(request); end; procedure TMiniBrowserFrm.Chromium1CertificateError(Sender: TObject; const browser: ICefBrowser; certError: Integer; const requestUrl: ustring; const sslInfo: ICefSslInfo; const callback: ICefCallback; out Result: Boolean); begin CefDebugLog('Certificate error code:' + inttostr(certError) + ' - URL:' + requestUrl, CEF_LOG_SEVERITY_ERROR); Result := False; end; procedure TMiniBrowserFrm.Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction); begin if (browser <> nil) and (Chromium1.BrowserId = browser.Identifier) and (CEFWindowParent1 <> nil) then begin PostMessage(Handle, CEF_DESTROY, 0, 0); aAction := cbaDelay; end; end; procedure TMiniBrowserFrm.Chromium1ContextMenuCommand(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; eventFlags: Cardinal; out Result: Boolean); var TempParam : WParam; TempInfo : TCefWindowInfo; TempClient : ICefClient; TempSettings : TCefBrowserSettings; begin Result := False; if Chromium1.IsSameBrowser(browser) then case commandId of MINIBROWSER_CONTEXTMENU_HIDEDEVTOOLS : PostMessage(Handle, MINIBROWSER_HIDEDEVTOOLS, 0, 0); MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS : begin TempParam := ((params.XCoord and $FFFF) shl 16) or (params.YCoord and $FFFF); PostMessage(Handle, MINIBROWSER_SHOWDEVTOOLS, TempParam, 0); end; MINIBROWSER_CONTEXTMENU_COPYALLTEXT : PostMessage(Handle, MINIBROWSER_COPYALLTEXT, 0, 0); MINIBROWSER_CONTEXTMENU_COPYHTML : PostMessage(Handle, MINIBROWSER_COPYHTML, 0, 0); MINIBROWSER_CONTEXTMENU_COPYFRAMEIDS : PostMessage(Handle, MINIBROWSER_COPYFRAMEIDS, 0, 0); MINIBROWSER_CONTEXTMENU_COPYFRAMENAMES : PostMessage(Handle, MINIBROWSER_COPYFRAMENAMES, 0, 0); MINIBROWSER_CONTEXTMENU_SHOWRESPONSE : PostMessage(Handle, MINIBROWSER_SHOWRESPONSE, 0, 0); MINIBROWSER_CONTEXTMENU_SAVEPREFERENCES : PostMessage(Handle, MINIBROWSER_SAVEPREFERENCES, 0, 0); MINIBROWSER_CONTEXTMENU_TAKESNAPSHOT : PostMessage(Handle, MINIBROWSER_TAKESNAPSHOT, 0, 0); MINIBROWSER_CONTEXTMENU_GETNAVIGATION : begin FNavigation.Clear; Chromium1.GetNavigationEntries(False); end; MINIBROWSER_CONTEXTMENU_JSWRITEDOC : if (frame <> nil) and frame.IsValid then frame.ExecuteJavaScript( 'var css = ' + chr(39) + '@page {size: A4; margin: 0;} @media print {html, body {width: 210mm; height: 297mm;}}' + chr(39) + '; ' + 'var style = document.createElement(' + chr(39) + 'style' + chr(39) + '); ' + 'style.type = ' + chr(39) + 'text/css' + chr(39) + '; ' + 'style.appendChild(document.createTextNode(css)); ' + 'document.head.appendChild(style);', 'about:blank', 0); MINIBROWSER_CONTEXTMENU_JSPRINTDOC : if (frame <> nil) and frame.IsValid then frame.ExecuteJavaScript('window.print();', 'about:blank', 0); MINIBROWSER_CONTEXTMENU_UNMUTEAUDIO : Chromium1.AudioMuted := False; MINIBROWSER_CONTEXTMENU_MUTEAUDIO : Chromium1.AudioMuted := True; MINIBROWSER_CONTEXTMENU_INCZOOM : Chromium1.IncZoomCommand; MINIBROWSER_CONTEXTMENU_DECZOOM : Chromium1.DecZoomCommand; MINIBROWSER_CONTEXTMENU_RESETZOOM : Chromium1.ResetZoomCommand; MINIBROWSER_CONTEXTMENU_EXITFULLSCREEN : Chromium1.ExitFullscreen(True); end else case commandId of MINIBROWSER_CONTEXTMENU_SHOWDEVTOOLS : try WindowInfoAsPopUp(TempInfo, browser.Host.WindowHandle, 'DevTools'); TempClient := TCustomClientHandler.Create(Chromium1, True); FillChar(TempSettings, SizeOf(TCefBrowserSettings), 0); browser.Host.ShowDevTools(@TempInfo, TempClient, @TempSettings, nil); finally TempClient := nil end; end; end; procedure TMiniBrowserFrm.Chromium1DownloadUpdated(Sender: TObject; const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); var TempString : string; begin if not(Chromium1.IsSameBrowser(browser)) then exit; if downloadItem.IsComplete then ShowStatusText(downloadItem.FullPath + ' completed') else if downloadItem.IsCanceled then ShowStatusText(downloadItem.FullPath + ' canceled') else if downloadItem.IsInterrupted then ShowStatusText(downloadItem.FullPath + ' interrupted') else if downloadItem.IsInProgress then begin if (downloadItem.PercentComplete >= 0) then TempString := downloadItem.FullPath + ' : ' + inttostr(downloadItem.PercentComplete) + '%' else TempString := downloadItem.FullPath + ' : ' + inttostr(downloadItem.ReceivedBytes) + ' bytes received'; ShowStatusText(TempString); end; end; procedure TMiniBrowserFrm.Chromium1FullScreenModeChange(Sender: TObject; const browser: ICefBrowser; fullscreen: Boolean); begin if not(Chromium1.IsSameBrowser(browser)) then exit; // This event is executed in a CEF thread and this can cause problems when // you change the 'Enabled' and 'Visible' properties from VCL components. // It's recommended to change the 'Enabled' and 'Visible' properties // in the main application thread and not in a CEF thread. // It's much safer to use PostMessage to send a message to the main form with // all this information and update those properties in the procedure handling // that message. if fullscreen then begin NavControlPnl.Visible := False; if (WindowState = wsMaximized) then WindowState := wsNormal; BorderIcons := []; BorderStyle := bsNone; WindowState := wsMaximized; end else begin BorderIcons := [biSystemMenu, biMinimize, biMaximize]; BorderStyle := bsSizeable; WindowState := wsNormal; NavControlPnl.Visible := True; end; end; procedure TMiniBrowserFrm.Chromium1KeyEvent(Sender: TObject; const browser: ICefBrowser; const event: PCefKeyEvent; osEvent: TCefEventHandle; out Result: Boolean); var TempMsg : TMsg; begin Result := False; if (event <> nil) and (osEvent <> nil) then case osEvent.Message of WM_KEYUP : begin TempMsg := osEvent^; HandleKeyUp(TempMsg, Result); end; WM_KEYDOWN : begin TempMsg := osEvent^; HandleKeyDown(TempMsg, Result); end; end; end; procedure TMiniBrowserFrm.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean); begin case Msg.message of WM_KEYUP : HandleKeyUp(Msg, Handled); WM_KEYDOWN : HandleKeyDown(Msg, Handled); end; end; procedure TMiniBrowserFrm.HandleKeyUp(const aMsg : TMsg; var aHandled : boolean); var TempMessage : TMessage; TempKeyMsg : TWMKey; begin TempMessage.Msg := aMsg.message; TempMessage.wParam := aMsg.wParam; TempMessage.lParam := aMsg.lParam; TempKeyMsg := TWMKey(TempMessage); if (TempKeyMsg.CharCode = VK_F12) then begin aHandled := True; if DevTools.Visible then PostMessage(Handle, MINIBROWSER_HIDEDEVTOOLS, 0, 0) else PostMessage(Handle, MINIBROWSER_SHOWDEVTOOLS, 0, 0); end; end; procedure TMiniBrowserFrm.HandleKeyDown(const aMsg : TMsg; var aHandled : boolean); var TempMessage : TMessage; TempKeyMsg : TWMKey; begin TempMessage.Msg := aMsg.message; TempMessage.wParam := aMsg.wParam; TempMessage.lParam := aMsg.lParam; TempKeyMsg := TWMKey(TempMessage); if (TempKeyMsg.CharCode = VK_F12) then aHandled := True; end; procedure TMiniBrowserFrm.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer); var TempHandle : THandle; begin if FClosing or (frame = nil) or not(frame.IsValid) or (browser = nil) then exit; if Chromium1.IsSameBrowser(browser) then begin if frame.IsMain then StatusPnl.Caption := 'main frame loaded : ' + quotedstr(frame.name) else StatusPnl.Caption := 'frame loaded : ' + quotedstr(frame.name); end else begin // This is a workaround for a focus issue in popup windows handled by CEF TempHandle := Windows.GetWindow(Browser.Host.WindowHandle, GW_OWNER); if (TempHandle <> Handle) then Windows.SetFocus(TempHandle); end; end; procedure TMiniBrowserFrm.Chromium1LoadError(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; errorCode: Integer; const errorText, failedUrl: ustring); var TempString : string; begin if (errorCode = ERR_ABORTED) then exit; TempString := '
' + '