1)从 APP将 值传入到 html JS中
/*APP代码*/
procedure GlobalCEFApp_OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
var
TempValue : ICEFv8Value;
begin
TempValue := TCefv8ValueRef.NewString('将值从Delphi窗口传入到 JS变量 windows.myval!');
//绑定JS中 window.myval 的值
context.Global.SetValueByKey('mysetval', TempValue, V8_PROPERTY_ATTRIBUTE_NONE);
end;
/*JS代码*/
2) APP注册一个函数myfunc ,JS调用该函数并传入入参,APP实现函数的方法。唤醒APP
//windows全部函数注册
procedure GlobalCEFApp_OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
var
TempHandler : ICefv8Handler;
TempFunction : ICefv8Value;
begin
TempHandler := TMyV8Handler.Create;
TempFunction := TCefv8ValueRef.NewFunction('myfunc', TempHandler); //能够通过JS wiondws.myfunc 调用
context.Global.SetValueByKey('myfunc', TempFunction, V8_PROPERTY_ATTRIBUTE_NONE); //绑定myfunc
end;
//具体APP定义
type
TMyV8Handler = class(TCefv8HandlerOwn)
protected
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
i:integer;
begin
if (name = 'myfunc') then
begin
i:= length(arguments) ; // arguments[0].IsFunction
retval := TCefv8ValueRef.NewString('My Value!arguments 参数 length '+inttostr(i)
+#13#10+ arguments[0].GetStringValue //arguments[0].IsString 参数1 字符串,通过JS传入进来
+#13#10+ formatdatetime('yyyymmdd-hh:mm:ss',now)
);
Result := True;
end
else
Result := False;
end;
3)APP 唤醒网页,执行JS.
uses
uCEFProcessMessage, uMyV8Handler;
//注册
procedure GlobalCEFApp_OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
var
TempHandler : ICefv8Handler;
TempFunction : ICefv8Value;
begin
TempHandler := TMyV8Handler.Create;
TempFunction := TCefv8ValueRef.NewFunction('register', TempHandler);
//注册全局windows.register 操作方法 ,浏览器唤醒 app
context.Global.SetValueByKey('register', TempFunction, V8_PROPERTY_ATTRIBUTE_NONE);
end;
//监听,执行
procedure GlobalCEFApp_OnProcessMessageReceived(const browser : ICefBrowser;
sourceProcess : TCefProcessId;
const aMessage : ICefProcessMessage;
var aHandled : boolean);
var
arguments: TCefv8ValueArray;
Tempval : ICefv8Value;
begin
if (aMessage.name = EXECFUNCTION_MSGNAME) then
begin
Tempval :=TCefv8ValueRef.NewString('参数1');
if (GlobalCallbackFunc <> nil) then
GlobalCallbackFunc.ExecuteFunctionWithContext(GlobalCallbackContext, nil, [Tempval]);
aHandled := True;
end
else
aHandled := False;
end;
procedure TJSExecutingFunctionsFrm.btn1Click(Sender: TObject);
var
TempMsg : ICefProcessMessage;
arguments: TCefv8ValueArray;
begin
//方法1 找到方法,并进行调用
//找到注册的myfunc方法
TempMsg := TCefProcessMessageRef.New(EXECFUNCTION_MSGNAME);
//向浏览器发消息myfunc
Chromium1.SendProcessMessage(PID_RENDERER, TempMsg);
// GlobalCEFApp_OnProcessMessageReceived,去唤起调用,实现如何调用
end;
/* JS代码*/
function myFunc(s) {
alert("This is myFunc executing!成功通过app唤醒网页,myfunc("+s+") ");
}
function RegisterJSFunction() {
window.register(myFunc);
}