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.

Emulate a mobile browser

Post Reply
thefunkyjoint
Posts: 460
Joined: Thu Aug 10, 2017 12:40 pm

Emulate a mobile browser

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

Re: Emulate a mobile browser

Post 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.
thefunkyjoint
Posts: 460
Joined: Thu Aug 10, 2017 12:40 pm

Re: Emulate a mobile browser

Post by thefunkyjoint »

Thank you !
thefunkyjoint
Posts: 460
Joined: Thu Aug 10, 2017 12:40 pm

Re: Emulate a mobile browser

Post by thefunkyjoint »

Thank you !
Post Reply