Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_USERSOFTWAREMicrosoftInternet ExplorerMainFeatureControlFEATURE_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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...