//具体的例子从源码的 demo javascript 中看使用用例
// JS绑定delphi的方法
//procedure(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context)
procedure TfrmOutSideMain.GlobalCEFApp_OnContextCreated(const browser: ICefBrowser;
const frame: ICefFrame; const context: ICefv8Context);
var
cefHandler: ICefv8Handler;
cefFunction: ICefv8Value;
begin
cefHandler := TMyV8Handler.Create;
//关闭退出
cefFunction := TCefv8ValueRef.NewFunction('ResClose', cefHandler);
context.Global.SetValueByKey('ResClose', cefFunction, V8_PROPERTY_ATTRIBUTE_NONE);
//浏览器调用系统的微软的TTS语音
//网页调用调用方法 MSSpeechVoice('请xx到诊室就诊',40,0,'js的回调方法')
cefFunction := TCefv8ValueRef.NewFunction('MSSpeechVoice', cefHandler);
context.Global.SetValueByKey('MSSpeechVoice', cefFunction, V8_PROPERTY_ATTRIBUTE_NONE);
end;
--------------------------------------------------------------------------
unit uMyV8Handler;
{$I cef.inc}
interface
uses
uCEFTypes, uCEFInterfaces, uCEFv8Value, uCEFProcessMessage, uCEFv8Handler,
uCEFv8Context, Windows, Messages, SysUtils,Dialogs,uSysLogClass;
type
TMyV8Handler = class(TCefv8HandlerOwn)
protected
FMyParam: string;
function Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray;
var retval: ICefv8Value; var exception: ustring): Boolean; override;
end;
implementation
function TMyV8Handler.Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray;
var retval: ICefv8Value; var exception: ustring): Boolean;
var
cefPromsg: ICefProcessMessage;
// CefFrame: ICefFrame;
iHandle: THandle;
sResult: string;
SpeedStr,Volume,Speed,SpeakCompletedCallName:String;
begin
Result := False;
try
if name = 'ResClose' then
begin
iHandle := FindWindow(PChar('TfrmOutSideMain'), nil);
PostMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
Result := True;
end
else if name = 'MSSpeechVoice' then
begin
if Length(arguments) = 4 then
begin
cefPromsg := TCefProcessMessageRef.New(name);
cefPromsg.ArgumentList.SetString(0, arguments[0].GetStringValue);
cefPromsg.ArgumentList.SetString(1, arguments[1].GetStringValue);
cefPromsg.ArgumentList.SetString(2, arguments[2].GetStringValue);
cefPromsg.ArgumentList.SetString(3, arguments[3].GetStringValue);
//老版本的在Browser上 触发后,会进入chromum1.OnProcessMessageReceived 事件中
//具体的业务中实现调用语音
TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, cefPromsg);
//新版本的为cef4delphi最新版本为
{
CefFrame := TCefv8ContextRef.Current.Browser.MainFrame;
if (CefFrame <> nil) and CefFrame.IsValid then
begin
CefFrame.SendProcessMessage(PID_BROWSER, cefPromsg);
end;
}
end;
sResult := '调用成功';
retval := TCefv8ValueRef.NewString(sResult);
Result := True;
end
else if name = 'ResPrint' then
begin
retval := TCefv8ValueRef.NewString(sResult);
Result := True;
end;
finally
cefPromsg := nil;
end;
end;
end.