Chromimu与JS交互的测试

来源:互联网 发布:ipad手写板软件 编辑:程序博客网 时间:2024/04/29 04:27

测试一

为解决ExecuteJavascript无法得到返回值的问题,尝试采用JS Dialog和OnJsDialog的方法。

测试结果:可以采用此法传递Js的结果值。

JS

alter('abc');

confirm('abc');

DELPHI

procedure TForm1.ChromiumFMXJsdialog(Sender: TObject;
  const browser: ICefBrowser; const originUrl, acceptLang: ustring;
  dialogType: TCefJsDialogType; const messageText, defaultPromptText: ustring;
  callback: ICefJsDialogCallback; out suppressMessage, Result: Boolean);
begin
  case dialogType of
  TCefJsDialogType.JSDIALOGTYPE_ALERT: self.Caption:='ALTERT:' + messageText;
  TCefJsDialogType.JSDIALOGTYPE_CONFIRM: self.Caption:='CONFIRM:' + messageText;
  end;
  Result:=true;
end;


测试二

JS调用Delphi自定义的方法。

测试结果:

1. 可以成功调用;

2. JS可以接收到Delphi方法的返回值;

3. JS的输入参数长度,65214*9是没问题的,更长的长度没有做测试。

TTestExtension = class
    class function addTask(songnumber: string): boolean;
end;

TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
  procedure OnWebKitInitialized; override;
end;

class function TTestExtension.addTask(songnumber: string): boolean;

begin
   showmessage('addTask:' + songnumber);
//    result:= (songnumber.Substring(0,1)='1') or    //JS可接收该返回值
//             (songnumber.Substring(0,1)='2') ;
end;

procedure TCustomRenderProcessHandler.OnWebKitInitialized;

begin
  TCefRTTIExtension.Register('app', TTestExtension);
end;

initialization
  CefRemoteDebuggingPort := 9000;
  CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
  CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;


测试三

用默认浏览器打开Chromium页面中的"_blank"超链。

测试结果:

1. 可以在delphi中捕获超链的点击事件,用自定义的方法来替代Chromium原生的浏览器小窗口。

2. 在delphi中返回False给Js,或者在Js中直接return false,都可以阻止Chromium原生小窗口的打开。

JS

$(document).ready(function(){
$("a[target=_blank]").click(function(event){
var href = $(this).attr("href");
return app.doClickHyperlink(href);
//return false;
});

DELPHI

TTestExtension = class
  class function doClickHyperlink(href: string): boolean;
end;

class function TTestExtension.doClickHyperlink(href: string): boolean;
begin
   result:=ShellExecute(0, 'Open', PChar(href), nil,nil,1)<0;    //返回False给JS,以阻止Chromium原生的浏览器小窗口。
end;

0 0
原创粉丝点击