Page 1 of 1

change de body --> Request Interception

Posted: Sat Apr 20, 2024 3:00 pm
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

Re: change de body --> Request Interception

Posted: Sat Apr 20, 2024 3:52 pm
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.

Re: change de body --> Request Interception

Posted: Sat Apr 20, 2024 5:20 pm
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,
});

Re: change de body --> Request Interception

Posted: Sat Apr 20, 2024 9:33 pm
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;

Re: change de body --> Request Interception

Posted: Sat Apr 20, 2024 9:41 pm
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);

Re: change de body --> Request Interception

Posted: Sun Apr 21, 2024 2:26 pm
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)

Re: change de body --> Request Interception

Posted: Mon Apr 22, 2024 10:49 pm
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,
});
});
}

Re: change de body --> Request Interception

Posted: Wed Apr 24, 2024 9:32 am
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.