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.

change de body --> Request Interception

Post Reply
Marcelo386
Posts: 8
Joined: Thu May 12, 2022 2:52 pm

change de body --> Request Interception

Post by Marcelo386 »

Hi, existe some way to change de body in BeforeResourceLoad event?

https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage#markdown-header-request-interception


if SameText(request.Url, 'https://test.com') then
begin
// Load the body HTML
htmlContent := TStringList.Create;
try
// Load HTML
htmlContent.LoadFromFile(ExtractFilePath(Application.ExeName) + 'testNew.html');
htmlString := htmlContent.Text;

// Create responde custom
vCefResponse := TCefResponseRef.New;
vCefResponse.Status := 200;
vCefResponse.StatusText := 'OK';
vCefResponse.MimeType := 'text/html';

Thanks
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: change de body --> Request Interception

Post by salvadordf »

Hi / Hola,

Yes but you have to use a filter. See the demos/Delphi_VCL/ResponseFilterBrowser demo.

Please download CEF4Delphi again from GitHub and read the documentation for TCustomResponseFilter.
Marcelo386
Posts: 8
Joined: Thu May 12, 2022 2:52 pm

Re: change de body --> Request Interception

Post by Marcelo386 »

I'm on the latest version, I looked at this example, which you gave me, but I didn't find how to do the same in this example with NodeJs

the body would be the content in html text, which I loaded into the stringlist, it would be exactly what I needed, I started doing it, but I couldn't find how to pass the html text

example nodejs
req.respond({
status: 200,
contentType: 'text/html',
body: body,
});
Marcelo386
Posts: 8
Joined: Thu May 12, 2022 2:52 pm

Re: change de body --> Request Interception

Post by Marcelo386 »

salvadordf wrote: Sat Apr 20, 2024 3:52 pm Hi / Hola,

Yes but you have to use a filter. See the demos/Delphi_VCL/ResponseFilterBrowser demo.

Please download CEF4Delphi again from GitHub and read the documentation for TCustomResponseFilter.
the body would be the content in html text, which I loaded into the stringlist, it would be exactly what I needed, I started doing it, but I couldn't find how to pass the html text
procedure TFrmConsole.Chromium1BeforeResourceLoad(Sender: TObject;
const browser: ICefBrowser; const frame: ICefFrame;
const request: ICefRequest; const callback: ICefCallback;
out Result: TCefReturnValue);
var
htmlContent: TStringList;
htmlString: string;
requestContext: ICefRequestContext;
vCefResponse: ICefResponse;
vCefRequest: ICefRequest;
begin
// Check if the request URL is the one you want to intercept
if SameText(request.Url, 'https://web.teste.com/') then
begin
// Cancel the original request
Result := RV_CANCEL;

vCefRequest := TCefRequestRef.New;

{vCefRequest.Status := 200;
vCefRequest.StatusText := 'OK';
vCefRequest.MimeType := 'text/html';}

// Load your modified HTML content
//LoadModifiedHtml(frame);
//Result := RV_CONTINUE;
end
else
begin
// Allow other requests to proceed normally
Result := RV_CONTINUE;
end;
end;
Last edited by Marcelo386 on Sat Apr 20, 2024 10:06 pm, edited 1 time in total.
Marcelo386
Posts: 8
Joined: Thu May 12, 2022 2:52 pm

Re: change de body --> Request Interception

Post by Marcelo386 »

Added property Inteface

ICefRequest = interface(ICefBaseRefCounted)
.....................
property Status : Integer read GetStatus write SetStatus;
property StatusText : ustring read GetStatusText write SetStatusText;
property MimeType : ustring read GetMimeType write SetMimeType;

procedure SetMimeType(const mimetype: ustring);
function GetMimeType: ustring;
function GetStatus: Integer;
procedure SetStatus(status: Integer);
function GetStatusText: ustring;
procedure SetStatusText(const StatusText: ustring);
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: change de body --> Request Interception

Post by salvadordf »

Perhaps I misunderstood what you needed.

Do you want to replace part of the text of a web page that an remote web server is sending to the browser?
Or maybe you just want to load custom web contents from the hard drive.

If you need to replace part of the text sent by a web server then use the ResponseFilterBrowser demo.

However, if you need to load web contents from the hard drive then use the SchemeRegistrationBrowser demo. (see the ICefResourceHandler documentation)
Marcelo386
Posts: 8
Joined: Thu May 12, 2022 2:52 pm

Re: change de body --> Request Interception

Post by Marcelo386 »

Example puppeteer, I need the same as the code below, the examples mentioned, it doesn't have what I need, I want to do the same as the code in Bitbucket, but I didn't find how to apply it

export async function setTestVersion(
page: Page,
version: string,
log?: (level: LogLevel, message: string, meta?: object) => any
) {
let body: string | null = null;
try {
body = Version.getPageContent(version);
} catch (error) {}

if (!body) {
log?.(
'error',
`Version not available for ${version}, using latest as fallback`
);
return;
}

await page.setRequestInterception(true);

page.on('request', (req) => {
if (req.url().startsWith('https://web.test.com/check-update')) {
req.abort();
return;
}
if (req.url() !== 'https://web.test.com/') {
req.continue();
return;
}

req.respond({
status: 200,
contentType: 'text/html',
body: body,
});
});
}
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: change de body --> Request Interception

Post by salvadordf »

Implement the TChromiumCore.OnLoadingStateChange event and check the isLoading to know when the navigation has finished.

Then call TChromiumCore.RetrieveHTML and you will receive the HTML contents in the TChromiumCore.OnTextResultAvailable event.

You can use TChromiumCore.OnBeforeResourceLoad event to read the URL with request.url.
Post Reply