如何:模拟在由 JavaScript 启动的 WebBrowser 警报/消息框中单击“确定”?(德尔福)

HOWTO: Simulate click OK in WebBrowser alert/messagebox that initiated by a JavaScript? (Delphi)

本文关键字:单击 确定 德尔福 消息 模拟 JavaScript 启动 警报 WebBrowser 如何      更新时间:2023-09-26

我有一个内置WebBrowser的应用程序:

当我发布我的网页时,我有javascript弹出警报/消息框出现,我需要单击确定。这是我创建警报的 JavaScript:

    function delete(){
    if (confirm('Are you sure you wish to delete this ?')){
            document.forms.item.action = "edit.asp?action=delete";
            document.forms.item.submit();
        }
    }

我正在寻找一段时间,但尚未找到任何可行的解决方案......

提前感谢您的所有帮助!

实现 IDocHostShowUI::ShowMessage 并显示您自己的对话框,或者只返回S_OK。

注意:链接已断开。以下是解决方案的代码:

      IDocHostShowUI = interface(IUnknown)
        ['{c4d244b0-d43e-11cf-893b-00aa00bdce1a}']
        function ShowMessage(hwnd: THandle; lpstrText: POleStr; lpstrCaption: POleStr;
          dwType: longint; lpstrHelpFile: POleStr; dwHelpContext: longint;
          var plResult: LRESULT): HRESULT; stdcall;
      end;
      TShowMessageEvent = function(Sender: TObject; HWND: THandle;
        lpstrText: POleStr; lpstrCaption: POleStr; dwType: Longint; lpstrHelpFile: POleStr;
        dwHelpContext: Longint; var plResult: LRESULT): HRESULT of object;
      TWebBrowser = class(SHDocVw.TWebBrowser, IDocHostShowUI)
        private
          fOnShowMessage: TShowMessageEvent;
        protected
          function ShowMessage(HWND: THandle; lpstrText: POleStr; lpstrCaption: POleStr;
            dwType: Longint; lpstrHelpFile: POleStr; dwHelpContext: Longint;
            var plResult: LRESULT): HRESULT; stdcall;
        published
          property OnShowMessage: TShowMessageEvent read fOnShowMessage write
            fOnShowMessage;
      end;
function TWebBrowser.ShowMessage(HWND: THandle; lpstrText, lpstrCaption: POleStr;
  dwType: Integer; lpstrHelpFile: POleStr; dwHelpContext: Integer;
  var plResult: LRESULT): HRESULT;
begin
  if Assigned(fOnShowMessage) then
    Result := fOnShowMessage(Self, HWND, lpstrText, lpStrCaption, dwType,
      lpStrHelpFile, dwHelpContext, plResult)
  else
  Result:= S_OK;
end;

如果这是针对非常受限制的内部用法,你可以做一个脏

procedure TForm1.Timer1Timer(Sender: TObject);
const
  TargetCaption = 'Meddelande från webbsida';
var
  S: string;
  len: integer;
begin
  SetLength(S, 127);
  len := GetWindowText(Application.ActiveFormHandle, PChar(S), 127);
  if len = 0 then Exit;
  SetLength(S, len);
  if S = TargetCaption then
    SendMessage(Application.ActiveFormHandle, WM_COMMAND, ID_OK, 0);
end;

其中TargetCaptionTWebBrowser弹出、确认或提示对话框的已知标题。这可能因操作系统版本和语言版本而异,因此此方法仅在非常受限的内部应用程序中可以接受,在应用程序中,可以使用每个新的 Windows SP "更新"应用程序...

顺便说一下,"Meddelande från webbsida"是瑞典语,意为"来自网页的消息"。

alert() confirm()prompt()框上的按钮不可编写脚本。请改用 HTML/CSS 模式对话框。