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) <> 1) or (not arguments[0].IsString) then begin Result := false; Exit; end; FTestParam := arguments[0].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;
調用實例:
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);
'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; //多個參數
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 := 0 to Length(arguments) - 1 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 := 0 to Length(ArrayParam) - 1 do begin if s='' then s := ArrayParam[i] else s := s+','+ArrayParam[i]; end; retval := TCefv8ValueRef.CreateString(FTestParam); end;
這樣調用GetTestParam返回的還是一個字符串,多個參數用逗號隔開了而已,可以對其再進行改寫即可;