I'm trying to change the User Agent of a Webview in a Windows 8 app. At the moment, I'm currently doing this:
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption,
string pBuffer, int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;
Then if I needed to change the UA to the iPhone for example:
string ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)" +
"AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0);
This works perfectly. But unfortunately, if I'm going to submit this app to the Windows Store, I won't be able to use this method since UrlMkSetSessionOption is not officially supported in Windows 8 apps.
Upon searching for alternatives I came across this. However, although that works, the page renders horribly, with barely any images or other elements being shown.
So I hoped that another way to do this would be by using the WebView.InvokeScript method and injecting some JavaScript to change the UA. The problem being, to be quite honest, is that I'm a complete beginner when it comes to JavaScript.
I did a bit more searching and apparently something like this (found here) would work:
navigator.__defineGetter__('userAgent', function(){
return 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25' // customized user agent
});
navigator.userAgent; // 'foo'
But like I said, I'm a complete beginner with Javascript. So when exactly should I inject the code? Do I need to listen for an event in JavaScript that runs this code when it fires?