zoukankan      html  css  js  c++  java
  • 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) <> 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);
     
     
    如果要实现多个参数,可以定义个数组替代FTestParam 
      private
        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返回的还是一个字符串,多个参数用逗号隔开了而已,可以对其再进行改写即可;
  • 相关阅读:
    usb3.0 bMaxBurst最大支持多少个 这个描述符什么时候被读取
    盒式图|加置信椭圆的散点图|分组盒式图|分组散点图|马赛克图|
    协方差分析|随机区组设计|样本单位|样本容量|变异系数|片面误差|抽样误差|真实性|精密度|重复性|精确程度|计数数据|区间变量|离散型变量|数值变量
    试验指标|试验单位|均方|随机模型|固定模型|字母标记法|LSR|q检验|LSD|重复值|弥补缺失数据|可加性|平方根转换|对数转换|反正弦转化
    2×c列联表|多组比例简式|卡方检验|χ2检验与连续型资料假设检验
    显著水平|区间估计|假设检验|显著性|第一类错误|Ⅱ类错误|β错误|t检验|连续性矫正|二项分布的假设检验|样本百分率|
    估计量|估计值|矩估计|最大似然估计|无偏性|无偏化|有效性|置信区间|枢轴量|似然函数|伯努利大数定理|t分布|单侧置信区间|抽样函数|
    单因素方差分析
    左偏|有偏|中心极限定理|卡方分布|
    正交试验
  • 原文地址:https://www.cnblogs.com/stroll/p/9942715.html
Copyright © 2011-2022 走看看