You've already forked CEF4Delphi
							
							
				mirror of
				https://github.com/salvadordf/CEF4Delphi.git
				synced 2025-10-30 23:07:52 +02:00 
			
		
		
		
	The JSExtensionWithFunction demo now sends a text message back to the main process
This commit is contained in:
		| @@ -56,11 +56,26 @@ object JSExtensionWithFunctionFrm: TJSExtensionWithFunctionFrm | ||||
|     Left = 0 | ||||
|     Top = 30 | ||||
|     Width = 978 | ||||
|     Height = 559 | ||||
|     Height = 540 | ||||
|     Align = alClient | ||||
|     TabOrder = 1 | ||||
|     ExplicitHeight = 559 | ||||
|   end | ||||
|   object StatusBar1: TStatusBar | ||||
|     Left = 0 | ||||
|     Top = 570 | ||||
|     Width = 978 | ||||
|     Height = 19 | ||||
|     Panels = < | ||||
|       item | ||||
|         Width = 500 | ||||
|       end> | ||||
|     ExplicitLeft = 336 | ||||
|     ExplicitTop = 544 | ||||
|     ExplicitWidth = 0 | ||||
|   end | ||||
|   object Chromium1: TChromium | ||||
|     OnProcessMessageReceived = Chromium1ProcessMessageReceived | ||||
|     OnBeforePopup = Chromium1BeforePopup | ||||
|     OnAfterCreated = Chromium1AfterCreated | ||||
|     Left = 32 | ||||
|   | ||||
| @@ -59,6 +59,7 @@ type | ||||
|     CEFWindowParent1: TCEFWindowParent; | ||||
|     Chromium1: TChromium; | ||||
|     Timer1: TTimer; | ||||
|     StatusBar1: TStatusBar; | ||||
|     procedure FormShow(Sender: TObject); | ||||
|     procedure GoBtnClick(Sender: TObject); | ||||
|     procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser); | ||||
| @@ -70,6 +71,9 @@ type | ||||
|       var popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; | ||||
|       var client: ICefClient; var settings: TCefBrowserSettings; | ||||
|       var noJavascriptAccess: Boolean; out Result: Boolean); | ||||
|     procedure Chromium1ProcessMessageReceived(Sender: TObject; | ||||
|       const browser: ICefBrowser; sourceProcess: TCefProcessId; | ||||
|       const message: ICefProcessMessage; out Result: Boolean); | ||||
|   protected | ||||
|     procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED; | ||||
|     procedure WMMove(var aMessage : TWMMove); message WM_MOVE; | ||||
| @@ -143,6 +147,18 @@ begin | ||||
|   Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]); | ||||
| end; | ||||
|  | ||||
| procedure TJSExtensionWithFunctionFrm.Chromium1ProcessMessageReceived( | ||||
|   Sender: TObject; const browser: ICefBrowser; | ||||
|   sourceProcess: TCefProcessId; const message: ICefProcessMessage; | ||||
|   out Result: Boolean); | ||||
| begin | ||||
|   if (message.Name = TEST_MESSAGE_NAME) then | ||||
|     begin | ||||
|       StatusBar1.Panels[0].Text := DateTimeToStr(now) + ' - ' + message.ArgumentList.GetString(0); // this doesn't create/destroy components | ||||
|       Result := True; | ||||
|     end | ||||
| end; | ||||
|  | ||||
| procedure TJSExtensionWithFunctionFrm.FormShow(Sender: TObject); | ||||
| begin | ||||
|   // GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser | ||||
|   | ||||
| @@ -42,7 +42,10 @@ unit uMyV8Handler; | ||||
| interface | ||||
|  | ||||
| uses | ||||
|   uCEFTypes, uCEFInterfaces, uCEFv8Value, uCEFv8Handler; | ||||
|   uCEFTypes, uCEFInterfaces, uCEFv8Value, uCEFProcessMessage, uCEFv8Handler, uCEFv8Context; | ||||
|  | ||||
| const | ||||
|   TEST_MESSAGE_NAME = 'test_message'; | ||||
|  | ||||
| type | ||||
|   TMyV8Handler = class(TCefv8HandlerOwn) | ||||
| @@ -57,9 +60,15 @@ function TMyV8Handler.Execute(const name      : ustring; | ||||
|                               const arguments : TCefv8ValueArray; | ||||
|                               var   retval    : ICefv8Value; | ||||
|                               var   exception : ustring): Boolean; | ||||
| var | ||||
|   msg: ICefProcessMessage; | ||||
| begin | ||||
|   if (name = 'myfunc') then | ||||
|     begin | ||||
|       msg := TCefProcessMessageRef.New(TEST_MESSAGE_NAME); | ||||
|       msg.ArgumentList.SetString(0, 'Message received!'); | ||||
|       TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, msg); | ||||
|  | ||||
|       retval := TCefv8ValueRef.NewString('My Value!'); | ||||
|       Result := True; | ||||
|     end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user