原文 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返回的还是一个字符串,多个参数用逗号隔开了而已,可以对其再进行改写即可;