Page 1 of 2

Disable CORS

Posted: Wed May 04, 2022 7:25 am
by pushca
Is it possible to disable the domain name matching check?

"strict-origin-when-cross-origin"

Re: Disable CORS

Posted: Wed May 04, 2022 10:05 am
by pushca
Actually I need to call the js function in the iFrame of another source. I remember doing this in CEF3. In the demo, I could not find an example of how to do this in Edge.

Re: Disable CORS

Posted: Wed May 04, 2022 7:02 pm
by salvadordf
Executing JavaScript in a frame is possible but not as easy as in CEF.

Follow these steps :
  • Use the TWVBrowserBase.OnFrameCreated event.
  • Create a TCoreWebView2FrameCreatedEventArgs instance with the aArgs parameter like this :

    Code: Select all

    TempArgs := TCoreWebView2FrameCreatedEventArgs.Create(aArgs);
  • Create a TCoreWebView2Frame instance like this :

    Code: Select all

    TempFrame := TCoreWebView2Frame.Create(TempArgs.Frame);
  • Call the TCoreWebView2Frame.ExecuteScript procedure like this :

    Code: Select all

    TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1);
  • WVBrowser1.OnExecuteScriptCompleted will be triggered when the execution is complete.

Re: Disable CORS

Posted: Wed May 04, 2022 9:51 pm
by pushca
Thanks! This will come in handy for me. You're a wizard!

Re: Disable CORS

Posted: Mon May 09, 2022 12:39 am
by pushca
salvadordf wrote: Wed May 04, 2022 7:02 pm Executing JavaScript in a frame is possible but not as easy as in CEF.

Follow these steps :
  • Use the TWVBrowserBase.OnFrameCreated event.
  • Create a TCoreWebView2FrameCreatedEventArgs instance with the aArgs parameter like this :

    Code: Select all

    TempArgs := TCoreWebView2FrameCreatedEventArgs.Create(aArgs);
  • Create a TCoreWebView2Frame instance like this :

    Code: Select all

    TempFrame := TCoreWebView2Frame.Create(TempArgs.Frame);
  • Call the TCoreWebView2Frame.ExecuteScript procedure like this :

    Code: Select all

    TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1);
  • WVBrowser1.OnExecuteScriptCompleted will be triggered when the execution is complete.

Hello. I checked the code. It cannot be executed in this line

Code: Select all

TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1);
Because TempFrame does not support the executeScript method


Code: Select all

var
G_TempFrame: ICoreWebView2Frame;

procedure TForm1.WVBrowser1FrameCreated(Sender: TObject; const aWebView: ICoreWebView2; const aArgs: ICoreWebView2FrameCreatedEventArgs);
var
  nameFrame: PWideChar;
begin
  G_TempArgs := ICoreWebView2FrameCreatedEventArgs(aArgs);
  aArgs.Get_Frame(G_TempFrame);
  G_TempFrame.Get_name(nameFrame);
  G_TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1); <=== ERROR
  Form1.Memo1.Lines.Add('irame' + nameFrame);
end;

Re: Disable CORS

Posted: Mon May 09, 2022 8:06 am
by salvadordf
ExecuteScript is declared by ICoreWebView2Frame2.

Create a TCoreWebView2Frame class or use aArgs.QueryInterface(ICoreWebView2Frame2, G_TempFrame) if you decide to declare G_TempFrame as ICoreWebView2Frame2.

Re: Disable CORS

Posted: Mon May 09, 2022 9:41 am
by pushca
salvadordf wrote: Mon May 09, 2022 8:06 am ExecuteScript is declared by ICoreWebView2Frame2.

Create a TCoreWebView2Frame class or use aArgs.QueryInterface(ICoreWebView2Frame2, G_TempFrame) if you decide to declare G_TempFrame as ICoreWebView2Frame2.
Could you give an example in my code? I think I don't have enough experience, I can't call TCoreWebView2Frame - this method is unknown to me in the IDE. Thank you for your help!

Re: Disable CORS

Posted: Tue May 10, 2022 8:47 am
by salvadordf

Code: Select all

procedure TMainForm.WVBrowser1ExecuteScriptCompleted(Sender: TObject;
  aErrorCode: HRESULT; const aResultObjectAsJson: wvstring;
  aExecutionID: Integer);
begin
  if aExecutionID = 42 then
  begin
    // MyJavaScriptCode finished executing
  end;
end;

procedure TMainForm.WVBrowser1FrameCreated(Sender: TObject;
  const aWebView: ICoreWebView2;
  const aArgs: ICoreWebView2FrameCreatedEventArgs);
var
  TempArgs : TCoreWebView2FrameCreatedEventArgs;
  TempFrame : TCoreWebView2Frame;
  MyJavaScriptCode : wvstring;
  MyCustonID : integer;
begin
  TempArgs := TCoreWebView2FrameCreatedEventArgs.Create(aArgs);
  TempFrame := TCoreWebView2Frame.Create(TempArgs.Frame);
  MyJavaScriptCode := 'your JS code goes here';
  MyCustonID := 42; // Use any value. This is only used to keep track of the executed JS code in WVBrowser1ExecuteScriptCompleted
  TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1);
  TempFrame.Free;
  TempArgs.Free;
end;

Re: Disable CORS

Posted: Tue May 10, 2022 11:59 am
by pushca
salvadordf wrote: Tue May 10, 2022 8:47 am

Code: Select all

procedure TMainForm.WVBrowser1ExecuteScriptCompleted(Sender: TObject;
  aErrorCode: HRESULT; const aResultObjectAsJson: wvstring;
  aExecutionID: Integer);
begin
  if aExecutionID = 42 then
  begin
    // MyJavaScriptCode finished executing
  end;
end;

procedure TMainForm.WVBrowser1FrameCreated(Sender: TObject;
  const aWebView: ICoreWebView2;
  const aArgs: ICoreWebView2FrameCreatedEventArgs);
var
  TempArgs : TCoreWebView2FrameCreatedEventArgs;
  TempFrame : TCoreWebView2Frame;
  MyJavaScriptCode : wvstring;
  MyCustonID : integer;
begin
  TempArgs := TCoreWebView2FrameCreatedEventArgs.Create(aArgs);
  TempFrame := TCoreWebView2Frame.Create(TempArgs.Frame);
  MyJavaScriptCode := 'your JS code goes here';
  MyCustonID := 42; // Use any value. This is only used to keep track of the executed JS code in WVBrowser1ExecuteScriptCompleted
  TempFrame.ExecuteScript(MyJavaScriptCode, MyCustonID, WVBrowser1);
  TempFrame.Free;
  TempArgs.Free;
end;

This is amazing! Thanks! There is still a little more and my problem will be solved. It seems to me that I have no dcu files in my project. Because I see it:
Image

Re: Disable CORS

Posted: Tue May 10, 2022 12:40 pm
by pushca

Code: Select all

uses
uWVCoreWebView2Frame, uWVCoreWebView2Args
I added this. Everything is working now. Thank you very much!