在dcef3当中执行js代码并获得返回值

时间:2022-01-21 05:23:09

1、如何在dcef3当中执行js代码

procedure TForm1.btnWriteZMClick(Sender: TObject);
var
  js: string;
begin
  js := 'document.getElementById(''login_button'').click();';    //获得ID为"login_button"并点击
  crm.Browser.MainFrame.ExecuteJavaScript(js, 'about:blank', 0);   //执行js
end;

2、如何给输入框赋值

procedure TForm1.btnWriteZMClick(Sender: TObject);
var
  js: string;
begin
  js := 'document.getElementById(''u'').value=''123123''';    //获得ID为"u"的输入框并赋值
  crm.Browser.MainFrame.ExecuteJavaScript(js, 'about:blank', 0);   //执行js
end;

3、如何获得js执行的返回值

procedure TForm1.btn1Click(Sender: TObject);
var
js: string;
begin
js := 'var uPage=document.getElementById(''errorMessage_lol'').innerText; alert(uPage);'; //获取errorMessage_lol文本值,并通过alert提交给crmJsdialog事件
crm.Browser.MainFrame.ExecuteJavaScript(js, 'about:blank', 0);
end;

//获取内容

procedure TForm1.crmJsdialog(Sender: TObject; const browser: ICefBrowser; const originUrl, acceptLang: ustring; dialogType: TCefJsDialogType; const messageText, defaultPromptText: ustring; callback: ICefJsDialogCallback; out suppressMessage, Result: Boolean);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add(messageText);   //这里获得alert弹框发送的内容
Result := False; //这里返回true标识不弹出对话框
end;