Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow System.Windows.Forms.WebBrowser to run javascript

Seriously - read the question all the way through before attempting to answer it. "Use a different browser" doesn't answer this question.

The question is this: How do I get a System.Windows.Forms.WebBrowser control to display a web page that has Javascript embedded in it when I have no control over the incoming page? I've already seen people suggest adding registry values for IE emulation (Allowing javascript to run on a windows form web browser) and altering the script settings in Internet Options/Security. Internet security options

I've done that already.

So, here's the test - use a System.Windows.Forms.WebBrowser control to access www.hulu.com.
Still getting this

And we're still getting this.

Any other ideas?

like image 705
rlranft Avatar asked Dec 01 '16 23:12

rlranft


1 Answers

Set the property ScriptErrorsSuppressed of the WebBrowser control to true to suppress the JavaScript error message.


In order to allow the code on hulu.com to execute, you must run the Webbrowser control in a mode such that it runs with newer version features. This can only be done by setting registry entries.

See this question and the answers for details.


To specify: I have a demo application to open the hulu website with the embedded WebBrowser control named WindowsFormsApplication5.exe.

Without registry changes, I see a note by Hulu that JavaScript support is not enabled. When sniffing the network transfer with Fiddler, I see that the following request is sent to the Hulu server:

GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Creative AutoUpdate v1.41.09)
Host: www.hulu.com
Connection: Keep-Alive
Pragma: no-cache

Note the version number "7.0" in the User-Agent string.

I now add a registry key of type REG_DWORD with name "WindowsFormsApplication5.exe" and value 0x00002af9 (11001) in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION.

As a result, the Hulu Website is successfully displayed in my demo application and I see the following request being sent via Fiddler:

GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://www.hulu.com/
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: www.hulu.com
Connection: Keep-Alive

Notice the different User-Agent string after the registry changes.


Registry setting to use IE 11 in WebBrowser Control in Demo Application

Demo Application showing Hulu Website after registry changes

like image 194
NineBerry Avatar answered Nov 09 '22 14:11

NineBerry