Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Disable CORS

pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Disable CORS

Post by pushca »

Is it possible to disable the domain name matching check?

"strict-origin-when-cross-origin"
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post 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.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Disable CORS

Post 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.
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post by pushca »

Thanks! This will come in handy for me. You're a wizard!
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post 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;
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Disable CORS

Post 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.
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post 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!
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Disable CORS

Post 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;
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post 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
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

Re: Disable CORS

Post by pushca »

Code: Select all

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