Question

Looking to get my delphi app to log into a website, navigate to a page, and automatically download certain files, the solution at How do I keep an embedded browser from prompting where to save a downloaded file?, helped a great deal with the file download.

The final problem is the last step on navigating opens in a popup window, there are plenty of solutions out there to capture popup windows by implementing TWebBrowser.NewWindow2 but none of these events seem to work with the above code, something to do with how twebbrowser.invokeevent in the above code works maybe?

If I use invokeveent and the dispID of 273(newwindow3) to call a function I can twebbwowser.navigate() a second webbrowser to the url of the popupwindow.

My problem is the popup window has basicly one line of javascript "document.print(parent.parent.opener.thefunction())" the second twebbrowser has no reference to its parent so this fails.

I can see two possible solutions, get the TWebBrowser.NewWindow2 or 3 to trigger, fix the code sample bellow, LVarArray[0] {const IDispatch}, is null for some reason.

 procedure TWebBrowser.InvokeEvent(ADispID: TDispID; var AParams: TDispParams);
             // DispID 250 is the BeforeNavigate2 dispinterface and to the FFileSource here
          // is stored the URL parameter (for cases, when the IDownloadManager::Download
          // won't redirect the URL and pass empty string to the pszRedir)
          //showmessage('test');
          var
        ArgCount : Integer;
        LVarArray : Array of OleVariant;
        LIndex : Integer;
        begin
        inherited;
        ArgCount := AParams.cArgs;
        SetLength(LVarArray, ArgCount);
        for LIndex := Low(LVarArray) to High(LVarArray) do
        LVarArray[High(LVarArray)-LIndex] := OleVariant(TDispParams(AParams).rgvarg^[LIndex]);

        case ADispID of
          250: FFileSource := OleVariant(AParams.rgvarg^[5]);

        273: DoNewWindow3(Self,
         LVarArray[0] {const IDispatch},
          WordBool((TVarData(LVarArray[1]).VPointer)^) {var WordBool},
          LVarArray[2] {const OleVariant},
        LVarArray[3] {const OleVariant},
        LVarArray[4] {const OleVariant});
            end;
         end;
Was it helpful?

Solution

I'm not going to answer your question directly because I think you've asked the wrong question. You are trying to download files over the internet without any GUI being shown to the user. As such, an embedded browser is simply the wrong solution.

Rather than trying to suppress popup dialogs, use a tool that never shows popup dialogs. What I believe you should be doing is downloading the files using direct HTTP download. There are many different ways to achieve that. For example, an extremely convenient method, available out of the box with Delphi, is to use Indy. I believe that the component you need is TIdHttp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top