Delphi中Chrome Chromium、Cef3学习笔记(三)

原文   http://blog.csdn.net/xtfnpgy/article/details/46635871

Delphi与JS的交互问题:

一、执行简单的JS

上一篇已经讲过:

chrm1.browser.MainFrame.ExecuteJavaScript('alert("abc");','about:blank',0);

chrm1.browser.MainFrame.ExecuteJavaScript('function aaa(){alert("abc");}aaa();','about:blank',0);

二、注入自定义的JS

首先必须在网页加载完成之后,才可以执行,不能为空页面!

str_js := 'var oScript = document.createElement( "script" );oScript.id = "sid";alert(oScript.id);';
chrm1.browser.MainFrame.ExecuteJavaScript(str_js,'about:blank',0);

三、delphi与JS交互(通过ceflib实现)

Cef3的Demo里面,有一个cefclient的例子,可以看下,关键代码:

1TExtension = class(TCefv8HandlerOwn) 2 private 3 FTestParam: ustring; 4 protected 5 function Execute(const name: ustring; const obj: ICefv8Value; 6 const arguments: TCefv8ValueArray; var retval: ICefv8Value; 7 var exception: ustring): Boolean; override; //重写Execute事件 8 end;

//通过不同的name,执行不同的动作

1function TExtension.Execute(const name: ustring; const obj: ICefv8Value; 2 const arguments: TCefv8ValueArray; var retval: ICefv8Value; 3 var exception: ustring): Boolean; 4begin 5 if(name = 'SetTestParam') then 6 begin 7 // Handle the SetTestParam native function by saving the string argument 8 // into the local member. 9 if (Length(arguments) <> 1) or (not arguments[0].IsString) then 10 begin 11 Result := false; 12 Exit; 13 end; 14 FTestParam := arguments[0].GetStringValue; //多个参数arguments[i].GetStringValue 依次类推 15 Result := true; 16 end 17 else if(name = 'GetTestParam') then 18 begin 19 // Handle the GetTestParam native function by returning the local member 20 // value. 21 retval := TCefv8ValueRef.CreateString(Ftestparam); 22 Result := true; 23 end 24 else if (name = 'GetTestObject') then 25 begin 26 // Handle the GetTestObject native function by creating and returning a 27 // new V8 object. 28 retval := TCefv8ValueRef.CreateObject(nil); 29 // Add a string parameter to the new V8 object. 30 retval.SetValueByKey('param', TCefv8ValueRef.CreateString( 31 'Retrieving a parameter on a native object succeeded.')); 32 // Add a function to the new V8 object. 33 retval.SetValueByKey('GetMessage', 34 TCefv8ValueRef.CreateFunction('GetMessage', Self)); 35 Result := true; 36 end 37 else if(name = 'GetMessage') then 38 begin 39 // Handle the GetMessage object function by returning a string. 40 retval := TCefv8ValueRef.CreateString( 41 'Calling a function on a native object succeeded.'); 42 Result := true; 43 end else 44 Result := false; 45end;

{ 注册JS扩展 }

1procedure RegisterExtension; 2var 3 Code: string; 4begin 5 Code := 6 'var cef;if(!cef)cef={};if(!cef.taobao)cef.taobao={};(function(){cef.taobao.test_object=function(){native function GetTestObject();return GetTestObject();};})();'; 7 if Code <> '' then 8 try 9 CefRegisterExtension('example/v8',Code,TExtension.Create as ICefv8Handler); 10 except 11 end; 12end;

在FormCreate中注册类:
RegisterExtension;

调用实例:

chrm1.browser.MainFrame.ExecuteJavaScript('alert("abc");','about:blank',0);

  str_temp := 'function aaa(){var CefObj = new cef.taobao.test_object;'+  //先new一个实例对象
               'CefObj.SetTestParam("abc");var b=CefObj.GetTestParam();alert(b);'+   //设置、获取、输出参数
               '}aaa();';
  chrm1.Browser.MainFrame.ExecuteJavaScript(str_temp,'about:blank',0);

如果要实现多个参数,可以定义个数组替代FTestParam 

  private
    FTestParam : ustring;
    ArrayParam : array of ustring;  //多个参数

关键代码:

1if(name = 'SetTestParam') then 2 begin 3// **************原来一个参数的情况********** 4// if (Length(arguments) <> 1) or (not arguments[0].IsString) then 5// begin 6// Result := false; 7// Exit; 8// end; 9// FTestParam := arguments[0].GetStringValue; 10// **************原来一个参数的情况********** 11 SetLength(ArrayParam,Length(arguments)); 12 for i := 0 to Length(arguments) - 1 do 13 begin 14 ArrayParam[i] := arguments[i].GetStringValue; 15 if s='' then 16 s := ArrayParam[i] 17 else 18 s := s+','+ArrayParam[i]; 19 end; 20 FTestParam := s; 21 end 22 else if(name = 'GetTestParam') then 23 begin 24// **************原来一个参数的情况********** 25// retval := TCefv8ValueRef.CreateString(FTestParam); 26// **************原来一个参数的情况********** 27 for i := 0 to Length(ArrayParam) - 1 do 28 begin 29 if s='' then 30 s := ArrayParam[i] 31 else 32 s := s+','+ArrayParam[i]; 33 end; 34 retval := TCefv8ValueRef.CreateString(FTestParam); 35 end;

这样调用GetTestParam返回的还是一个字符串,多个参数用逗号隔开了而已,可以对其再进行改写即可;

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )