webview simulate desktop version of a page

4,180 views
Skip to first unread message

Lumparn

unread,
Jan 25, 2017, 9:42:35 AM1/25/17
to DroidScript
Hello all,

I'm trying to use a webview to load a webpage, my problem is the webview automatic shows the mobile version of the page, probably some user-agent string in the header makes this. I wonder if there is a way to force the web view to show desktop version of websites? For instance a way to alter the header?

It works fine when using a webrequest and manually setting the user-agent, but would love to be able to use the webview directly with desktop version. Any ideas on this?

Steve Garman

unread,
Jan 25, 2017, 9:50:53 AM1/25/17
to DroidScript
If the mobile version is shown because of the user agent, this might work for you.

web.SetUserAgent(navigator.userAgent.replace(/android/gi,"secret"));

bill t

unread,
Nov 22, 2017, 7:45:23 PM11/22/17
to DroidScript
Hi Steave,
This is not working for me, Android 7.

Steve Garman

unread,
Nov 23, 2017, 3:23:44 AM11/23/17
to DroidScript
bill,
are you sure the user agent is the problem?

You can test whether the user agent is being changed by running the test code below.
If the page displays a string including the word "Android" please let us know.


function OnStart()
{
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

web = app.CreateWebView( 0.9,0.9 )
web.SetUserAgent(navigator.userAgent.replace(/android/gi,"secret"));
lay.AddChild( web );

web.LoadUrl( "http://sgarman.net/ua.php" );
app.AddLayout( lay );
}

bill t

unread,
Dec 9, 2017, 10:17:09 PM12/9/17
to DroidScript
Hi Steave,
Run your code, the result is:
Mozilla/5.0 (Linux; secret 7.1.2; Mi Max Build/NJH47D; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/59.0.3071.92 Mobile Safari/537.36

But when set UserAgent as you recommend, remove "android". It's not working.
I need webview loading site as desktop mode very much. So, please give me the full solution for that in DS soon.
Thank you very much.

Steve Garman

unread,
Dec 10, 2017, 3:03:53 AM12/10/17
to DroidScript
There is no universal way of asking sites to provide the desktop version.

If the particular website you are having trouble with does not respond to that change in useragent, you will need to find out how it decides which version to serve.

The way to find this may be by examining the html if you are lucky.

If not, you may need to ask the owner of the website or search for information from other users of the site.

Some website designers are very determined to control which view you see and it may be virtually impossible to override.


Steve Garman

unread,
Dec 10, 2017, 3:43:29 AM12/10/17
to DroidScript
Looking at this thread again, I see the original regex is a bit out of date.

Try removing "Mobile" as well as "Android"

web.SetUserAgent(navigator.userAgent.replace(/android|mobile/gi,"secret"));

JustAnotherDude

unread,
Dec 11, 2017, 2:58:25 AM12/11/17
to DroidScript
and websites with responsive and mobile-first css cannot be force to show the 'desktop' version becazse it haven't any

the only way is to change the viewport size

bill t

unread,
Dec 12, 2017, 2:36:39 AM12/12/17
to DroidScript
Thank Steve,
It don't work for all sites.
But that site still work with "view as desktop" option of chrome mobile. How did they do that?
And i want to browse desktop version of site, not only view. Bacause, in mobile version of some site, some site function don't work.
Any other solution is more advanced?
Thank you!

bill t

unread,
Dec 12, 2017, 2:58:46 AM12/12/17
to DroidScript
Hi,
I find out this code in java:
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true); webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

I think not only set userAgent but also set some other settings to show desktop site for sure. Ex: wide viewport or zoom.
How to set that settings in DS ?

Steve Garman

unread,
Dec 12, 2017, 9:57:05 AM12/12/17
to androi...@googlegroups.com
When you create the WebView, try creating it with the options AllowZoom, AutoZoom, Wide and NoScrollBars.

web = app.CreateWebView( 1,1,"AllowZoom,AutoZoom,Wide,NoScrollBars" );

That should set most of the settings above.

jhon vega

unread,
Aug 12, 2018, 3:31:15 PM8/12/18
to DroidScript
Hola!, encontraste alguna solución ?, tengo el mismo problema 

Jared

unread,
Aug 12, 2018, 8:28:48 PM8/12/18
to DroidScript
What Steve recommends is generally the best way to get desktop mode, however here's a alternative option to try as well. 

try adding:
?nomobile=true
or 
?mobile=0
at the end or somewhere within the url that looks like it would make sense. Its essentially just guesswork, however searching Google for the page url followed by:
inurl:"nomobile
or any of the others listed above in quotes, may find a url for desktop mode as it searches only for text in page urls(not content of page,headers,meta,etc)


Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages