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

99封情书 提交于 2019-12-08 06:07:19

问题


I have an app with a WebBrowser inside:

when I POST my webpage i have javascript popup alert/messagebox comes on where I need to click OK. Here is my javascript that creates the alert:

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

    }

I was searching for a while but could not yet find any working solution...

Thanks in advance for all your help!


回答1:


Implement IDocHostShowUI::ShowMessage and show your own dialog, or just return S_OK.

Note:The link is broken. Here the code for the solution:

      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;



回答2:


If this is for very restricted, internal, usage, you can do a dirty

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;

where TargetCaption is the known caption of the TWebBrowser popup, confirm, or prompt dialogs. This may vary between OS versions and language versions, so this approach is only acceptable in a very restricted, in-house application, where it is OK to 'update' the application with every new Windows SP...

By the way, "Meddelande från webbsida" is Swedish for "Message from web page".




回答3:


Buttons on alert() confirm() and prompt() boxes are not scriptable. Use a HTML/CSS modal dialog instead.



来源:https://stackoverflow.com/questions/11885700/howto-simulate-click-ok-in-webbrowser-alert-messagebox-that-initiated-by-a-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!