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)privateFTestParam: ustring;protectedfunction 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; beginif(name = 'SetTestParam') thenbegin// Handle the SetTestParam native function by saving the string argument// into the local member.if (Length(arguments) <> 1) or (not arguments[0].IsString) thenbeginResult := false;Exit;end;FTestParam := arguments[0].GetStringValue; //多个参数arguments[i].GetStringValue 依次类推Result := true;endelse if(name = 'GetTestParam') thenbegin// Handle the GetTestParam native function by returning the local member// value.retval := TCefv8ValueRef.CreateString(Ftestparam);Result := true;endelse if (name = 'GetTestObject') thenbegin// 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;endelse if(name = 'GetMessage') thenbegin// Handle the GetMessage object function by returning a string.retval := TCefv8ValueRef.CreateString('Calling a function on a native object succeeded.');Result := true;end elseResult := false; end;
{ 注册JS扩展 }
procedure RegisterExtension; varCode: string; beginCode :='var cef;if(!cef)cef={};if(!cef.taobao)cef.taobao={};(function(){cef.taobao.test_object=function(){native function GetTestObject();return GetTestObject();};})();';if Code <> '' thentryCefRegisterExtension('example/v8',Code,TExtension.Create as ICefv8Handler);exceptend; 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') thenbegin // **************原来一个参数的情况********** // 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 := 0 to Length(arguments) - 1 dobeginArrayParam[i] := arguments[i].GetStringValue;if s='' thens := ArrayParam[i]elses := s+','+ArrayParam[i];end;FTestParam := s;endelse if(name = 'GetTestParam') thenbegin // **************原来一个参数的情况********** // retval := TCefv8ValueRef.CreateString(FTestParam); // **************原来一个参数的情况**********for i := 0 to Length(ArrayParam) - 1 dobeginif s='' thens := ArrayParam[i]elses := s+','+ArrayParam[i];end;retval := TCefv8ValueRef.CreateString(FTestParam);end;
转载于:https://www.cnblogs.com/stroll/p/9942715.html
Delphi中Chrome Chromium、Cef3学习笔记(三)相关推荐
- Delphi中Chrome Chromium、Cef3学习笔记(二)
尊重作者原创,转载请注明出处,原文地址: http://blog.csdn.net/xtfnpgy/article/details/46635739 用Tchromium替换webbrowser 用惯 ...
- J2EE学习笔记三:EJB基础概念和知识 收藏
J2EE学习笔记三:EJB基础概念和知识 收藏 EJB正是J2EE的旗舰技术,因此俺直接跳到这一章来了,前面的几章都是讲Servlet和JSP以及JDBC的,俺都懂一些.那么EJB和通常我们所说的Ja ...
- tensorflow学习笔记(三十二):conv2d_transpose (解卷积)
tensorflow学习笔记(三十二):conv2d_transpose ("解卷积") deconv解卷积,实际是叫做conv_transpose, conv_transpose ...
- Ethernet/IP 学习笔记三
Ethernet/IP 学习笔记三 原文为硕士论文: 工业以太网Ethernet/IP扫描器的研发 知网网址: http://kns.cnki.net/KCMS/detail/detail.aspx? ...
- iView学习笔记(三):表格搜索,过滤及隐藏列操作
iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...
- 吴恩达《机器学习》学习笔记三——多变量线性回归
吴恩达<机器学习>学习笔记三--多变量线性回归 一. 多元线性回归问题介绍 1.一些定义 2.假设函数 二. 多元梯度下降法 1. 梯度下降法实用技巧:特征缩放 2. 梯度下降法的学习率 ...
- Python基础学习笔记三
Python基础学习笔记三 print和import print可以用,分割变量来输出 import copy import copy as co from copy import deepcopy ...
- Mr.J-- jQuery学习笔记(三十二)--jQuery属性操作源码封装
扫码看专栏 jQuery的优点 jquery是JavaScript库,能够极大地简化JavaScript编程,能够更方便的处理DOM操作和进行Ajax交互 1.轻量级 JQuery非常轻巧 2.强大的 ...
- MYSQL学习笔记三:日期和时间函数
MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...
最新文章
- LeCun、河北大学校长康乐当选美国科学院院士,另有6位华人学者位列其中
- Boost:bimap双图的structured_pair的测试程序
- centos7配置haproxy
- 筛法求10000以内的质数
- 数据科学包2-pandas快速入门1
- Java算法常见面试题及答案
- 苹果手机双卡双待是哪一款_等等党大获全胜 多款5G+5G双卡双待手机值得推荐
- 未转变者服务器bug,未转变者BUG问题解决 | 手游网游页游攻略大全
- Corosync双心跳冗余方案配置
- SICTF2023 Osint-wp
- Word图片插入后只显示最底下一行,输入文字后后面的文字不见了(解决办法(全程简洁无废话))
- 总结做SEO没效果,这些点你考虑过吗?
- 《React扩展知识一》setState更新状态2种写法/ lazyLoad / Fragment / Context / Hooks
- FASM 1.67 程序者手册
- echarts数据可视化(仪表盘)
- 将base64格式的图片画到canvas上(js和vue两种)
- leetcode刷题总结(主要是各个博主的总结链接)
- 欢迎使用小书匠编辑器
- 阿里妈妈展示广告预估校准技术演进之路
- 什么是QinQ, QinQ基础,VLAN双层TAG