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.
Disable CORS
Disable CORS
Is it possible to disable the domain name matching check?
"strict-origin-when-cross-origin"
"strict-origin-when-cross-origin"
Re: Disable CORS
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.
- salvadordf
- Posts: 4620
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Disable CORS
Executing JavaScript in a frame is possible but not as easy as in CEF.
Follow these steps :
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
Thanks! This will come in handy for me. You're a wizard!
Re: Disable CORS
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);
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;
- salvadordf
- Posts: 4620
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Disable CORS
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.
Create a TCoreWebView2Frame class or use aArgs.QueryInterface(ICoreWebView2Frame2, G_TempFrame) if you decide to declare G_TempFrame as ICoreWebView2Frame2.
Re: Disable CORS
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!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.
- salvadordf
- Posts: 4620
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Disable CORS
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
salvadordf wrote: Tue May 10, 2022 8:47 amCode: 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:

Re: Disable CORS
Code: Select all
uses
uWVCoreWebView2Frame, uWVCoreWebView2Args