Delphi中Chrome Chromium、Cef3学习笔记(三)
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的例子,可以看下,关键代码:
TExtension = class(TCefv8HandlerOwn)
private
FTestParam: ustring;
protected
function Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean; override; //重写Execute事件
end;
//通过不同的name,执行不同的动作
function TExtension.Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean;
begin
if(name = 'SetTestParam') then
begin
// Handle the SetTestParam native function by saving the string argument
// into the local member.
if (Length(arguments) <> ) or (not arguments[].IsString) then
begin
Result := false;
Exit;
end;
FTestParam := arguments[].GetStringValue; //多个参数arguments[i].GetStringValue 依次类推
Result := true;
end
else if(name = 'GetTestParam') then
begin
// Handle the GetTestParam native function by returning the local member
// value.
retval := TCefv8ValueRef.CreateString(Ftestparam);
Result := true;
end
else if (name = 'GetTestObject') then
begin
// Handle the GetTestObject native function by creating and returning a
// new V8 object.
retval := TCefv8ValueRef.CreateObject(nil);
// Add a string parameter to the new V8 object.
retval.SetValueByKey('param', TCefv8ValueRef.CreateString(
'Retrieving a parameter on a native object succeeded.'));
// Add a function to the new V8 object.
retval.SetValueByKey('GetMessage',
TCefv8ValueRef.CreateFunction('GetMessage', Self));
Result := true;
end
else if(name = 'GetMessage') then
begin
// Handle the GetMessage object function by returning a string.
retval := TCefv8ValueRef.CreateString(
'Calling a function on a native object succeeded.');
Result := true;
end else
Result := false;
end;
{ 注册JS扩展 }
procedure RegisterExtension;
var
Code: string;
begin
Code :=
'var cef;if(!cef)cef={};if(!cef.taobao)cef.taobao={};(function(){cef.taobao.test_object=function(){native function GetTestObject();return GetTestObject();};})();';
if Code <> '' then
try
CefRegisterExtension('example/v8',Code,TExtension.Create as ICefv8Handler);
except
end;
end;
在FormCreate中注册类:
RegisterExtension;
调用实例:
'CefObj.SetTestParam("abc");var b=CefObj.GetTestParam();alert(b);'+ //设置、获取、输出参数
'}aaa();';
chrm1.Browser.MainFrame.ExecuteJavaScript(str_temp,'about:blank',0);
FTestParam : ustring;
ArrayParam : array of ustring; //多个参数
if(name = 'SetTestParam') then
begin
// **************原来一个参数的情况**********
// if (Length(arguments) <> 1) or (not arguments[0].IsString) then
// begin
// Result := false;
// Exit;
// end;
// FTestParam := arguments[0].GetStringValue;
// **************原来一个参数的情况**********
SetLength(ArrayParam,Length(arguments));
for i := to Length(arguments) - do
begin
ArrayParam[i] := arguments[i].GetStringValue;
if s='' then
s := ArrayParam[i]
else
s := s+','+ArrayParam[i];
end;
FTestParam := s;
end
else if(name = 'GetTestParam') then
begin
// **************原来一个参数的情况**********
// retval := TCefv8ValueRef.CreateString(FTestParam);
// **************原来一个参数的情况**********
for i := to Length(ArrayParam) - do
begin
if s='' then
s := ArrayParam[i]
else
s := s+','+ArrayParam[i];
end;
retval := TCefv8ValueRef.CreateString(FTestParam);
end;
Delphi中Chrome Chromium、Cef3学习笔记(三)的更多相关文章
- Delphi中Chrome Chromium、Cef3学习笔记(四)
原文 http://blog.csdn.net/xtfnpgy/article/details/48155323 一.遍历网页元素并点击JS: 下面代码为找到淘宝宝贝页面,成交记录元素的代码: ...
- Delphi中Chrome Chromium、Cef3学习笔记(一)
原文 http://blog.csdn.net/xtfnpgy/article/details/46635225 官方下载地址:https://cefbuilds.com/ CEF简介: 嵌入 ...
- Delphi中Chrome Chromium、Cef3学习笔记(二)
原文 http://blog.csdn.net/xtfnpgy/article/details/46635739 用Tchromium替换webbrowser 用惯了EmbeddedWB,不想 ...
- Delphi中Chrome Chromium、Cef3学习笔记(五)
原文 http://blog.csdn.net/xtfnpgy/article/details/48489489 一.模拟移动鼠标 // SetCursorPos(StrToInt(Edit ...
- Delphi中Chrome Chromium、Cef3学习笔记(六)
原文 http://blog.csdn.net/xtfnpgy/article/details/71703317 一.CEF加载网页时空白 chrm1.Load(‘你的网址’); 出现空白,跟 ...
- Chrome development tools学习笔记(5)
调试JavaScript 随着如今JavaScript应用的越来越广泛,在面对前端工作的时候,开发人员须要强大的调试工具来高速有效地解决这个问题.我们文章的主角,Chrome DevTools就提供了 ...
- angular学习笔记(三十一)-$location(2)
之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍 ...
- amazeui学习笔记三(你来我往1)--常见问题FAQs
amazeui学习笔记三(你来我往1)--常见问题FAQs 一.总结 1.DOM事件失败:记得加上初始化代码,例如 图片轮播 $('#my-slider').flexslider(); 2.jquer ...
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
随机推荐
- 0913_Python初识及变量
1.Python3不需加utf-8,Python必须加utf-82.单行注释用#,多行注释用""" """3.变量:只能由字母.数字.下划线 ...
- C# 网络通信功能 同步数据交互开发
前言 本文将使用一个Nuget公开的组件技术来实现一对多的数据通信功能,提供了一些简单的API,来方便的向服务器进行数据请求. 在visual studio 中的Nuget管理器中可以下载安装,也可以 ...
- SpringMVC + MyBatis分库分表方案
mybatis作为流行的ORM框架,项目实际使用过程中可能会遇到分库分表的场景.mybatis在分表,甚至是同主机下的分库都可以说是完美支持的,只需要将表名或者库名作为动态参数组装sql就能够完成.但 ...
- JAVA高级篇(一、JVM基本概念)
一.什么是JVM VM的中文名称叫Java虚拟机,它是由软件技术模拟出计算机运行的一个虚拟的计算机. JVM也充当着一个翻译官的角色,我们编写出的Java程序,是不能够被操作系统所直接识别的,这时候J ...
- Combining Lexical and Grammatical Features to Improve Readability Measures for First and Second Language Texts.-paper
http://www.aclweb.org/anthology/N07-1058 Volume:Human Language Technologies 2007: The Conference of ...
- windows下使用kafka的常用命令
参考文档: https://blog.csdn.net/evankaka/article/details/52421314 http://orchome.com/6 1 启动zookeeper cmd ...
- python socket 函数介绍
socket 函数原型:socket.socket([family[,type[,proto]]]) family参数取值(协议族): socket.AF_INET -->ipv4 ...
- cannot find package "context"
导入 github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...
- linux kernel driver debug
1. print printk(): never pr_debug(): always good dev_dbg(): prefered when you have a struct device o ...
- WPF项目中解决ConfigurationManager不能用(转)
https://blog.csdn.net/MOESECSDN/article/details/78107888 在WPF项目中遇到这样的问题,做一下笔记.希望对自己和读者都有帮助. 在aap.con ...