salvadordf / WebView4Delphi

WebView4Delphi is an open source project created by Salvador Díaz Fau to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows.
https://www.briskbard.com/forum/
MIT License
282 stars 59 forks source link

Useful functions #11

Closed amancini closed 2 years ago

amancini commented 2 years ago

Hello,

Thanks for your work. You are amazing.

please add some useful function:

SelectAll

  WVBrowser1.CoreWebView2.BaseIntf.ExecuteScript('this.document.execCommand("SelectAll", true);',ICoreWebView2ExecuteScriptCompletedHandler(
    function (errorCode: HResult; resultObjectAsJson: PWideChar): HResult stdcall
    begin
      Result := S_OK;
    end))

CopySelected

WVBrowser1.CoreWebView2.BaseIntf.ExecuteScript('this.document.execCommand("copy");',ICoreWebView2ExecuteScriptCompletedHandler(
    function (errorCode: HResult; resultObjectAsJson: PWideChar): HResult stdcall
    begin
      Result := S_OK;
    end))

ClearSelection

    WVBrowser1.CoreWebView2.BaseIntf.ExecuteScript('window.getSelection().removeAllRanges();',
              ICoreWebView2ExecuteScriptCompletedHandler(
      function (errorCode: HResult; resultObjectAsJson: PWideChar): HResult stdcall
      begin
        Result := S_OK;
      end));

FindText

Procedure FindText(Const aText:String;aCaseSensitive, aBackwards, aWrapAround,
            aWholeWord, aSearchInFrames:Boolean);
begin
   if not WVBrowser1.Initialized then exit;

   WVBrowser1.CoreWebView2.BaseIntf.ExecuteScript(PWideChar(Format('window.find("%s",%s,%s,%s,%s,%s,false)',[
                    aText,
                    BoolToStr(aCaseSensitive,true).ToLower,
                    BoolToStr(aBackwards,true).ToLower,
                    BoolToStr(aWrapAround,true).ToLower,
                    BoolToStr(aWholeWord,true).ToLower,
                    BoolToStr(aSearchInFrames,true).ToLower
                    ])),
              ICoreWebView2ExecuteScriptCompletedHandler(
      function (errorCode: HResult; resultObjectAsJson: PWideChar): HResult stdcall
      begin
        Result := S_OK;
      end));

this function is call by event onFind of standard TFindDialog

procedure TFormBrowserEDGE.FindDialog1Find(Sender: TObject);
begin
  inherited;
  FindText(FindDialog1.FindText,
    frMatchCase in FindDialog1.Options,
    not (frDown in FindDialog1.Options),
    true,//??
    frWholeWord  in FindDialog1.Options,
    True//??
    )
end;

SaveToFile

Get HTML Source and SaveToFile

end;

Best regards Alessandro Mancini

salvadordf commented 2 years ago

I just added the TWVBrowserBase.SimulateEditingCommand function to simulate more than 130 editing commands.

It uses an experimental parameter of the "Input.dispatchKeyEvent" DevTools method but it seems to work fine with the editing commands that don't require extra parameters.

I also modified the EditorBrowser demo to use TWVBrowserBase.SimulateEditingCommand for most of the editing functions.

The TWVBrowserBase.RetrieveHTML function and the TWVBrowserBase.OnRetrieveHTMLCompleted event can be used to get the HTML source.

Try TWVBrowserBase.KeyboardShortcutSearch to open the search box included in WebView2.

amancini commented 2 years ago

wow, thanks