Page 1 of 1

Emulate a mobile browser

Posted: Mon Aug 21, 2023 6:37 pm
by thefunkyjoint
I'm using the code below to emulate an Android web browser, it works well !

But after i run the code below, what if i want to go back to the 'default' browser ?

var
TempParams, TempDict: ICefDictionaryValue;
TempFormatSettings: TFormatSettings;
TempOrientation: string;
begin
try
TempParams := TCefDictionaryValueRef.New;
TempParams.SetInt('width', 480);
TempParams.SetInt('height', 800);
TempFormatSettings := TFormatSettings.Create;
TempFormatSettings.DecimalSeparator := '.';
TempParams.SetDouble('deviceScaleFactor', StrToFloat('1', TempFormatSettings));
TempParams.SetBool('mobile', true);
TempDict := TCefDictionaryValueRef.New;
TempDict.SetString('type', 'portraitPrimary');
TempDict.SetInt('angle', 0);
TempParams.SetDictionary('screenOrientation', TempDict);
ExecuteDevToolsMethod(5, 'Emulation.setDeviceMetricsOverride', TempParams);
TempParams := TCefDictionaryValueRef.New;
TempParams.SetString('userAgent', uaAndroid);
ExecuteDevToolsMethod(1, 'Emulation.setUserAgentOverride', TempParams);
finally
TempDict := nil;
TempParams := nil;
end;
end;

Re: Emulate a mobile browser

Posted: Tue Aug 22, 2023 5:56 pm
by salvadordf
Hi,

Emulation.clearDeviceMetricsOverride clears the overridden device metrics but the user agent has to be updated again with the original value.

The GetDefaultCEFUserAgent function in uCEFMiscFunctions calculates the user agent but you can also read the HTTP headers in the first navigation in order to get the original user agent from the "User-Agent" header.

Re: Emulate a mobile browser

Posted: Tue Aug 29, 2023 1:20 pm
by thefunkyjoint
Thank you !

Re: Emulate a mobile browser

Posted: Tue Aug 29, 2023 1:20 pm
by thefunkyjoint
Thank you !