Created
May 13, 2011 10:25
-
-
Save tsupo/970310 to your computer and use it in GitHub Desktop.
an idea of expansion of WebKit.NET's WebKitBrowser control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2011, Hiroshi Tsujimura | |
// All rights reserved. | |
// | |
// Redistribution and use in source and binary forms, with or without | |
// modification, are permitted provided that the following conditions are met: | |
// | |
// * Redistributions of source code must retain the above copyright notice, | |
// this list of conditions and the following disclaimer. | |
// * Redistributions in binary form must reproduce the above copyright | |
// notice, this list of conditions and the following disclaimer in the | |
// documentation and/or other materials provided with the distribution. | |
// * Neither the name of watcher.moe-nifty.com. nor the names of its | |
// contributors may be used to endorse or promote products derived from | |
// this software without specific prior written permission. | |
// | |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
// POSSIBILITY OF SUCH DAMAGE. | |
using System; | |
using System.Windows.Forms; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Text; | |
using System.Net; | |
using System.IO; | |
using WebKit; | |
namespace webkitTest4 | |
{ | |
public class WebKitBrowserEx : WebKitBrowser | |
{ | |
/// <summary> | |
/// 「URL欄」表示用の URL の履歴 | |
/// </summary> | |
public Collection<string> history; | |
/// <summary> | |
/// 「履歴メニュー」表示用の履歴情報(URL と title) | |
/// </summary> | |
public List<ToolStripMenuItem> histories; | |
public WebKitBrowserEx() | |
{ | |
ShowJavaScriptAlertPanel += (s, e) => | |
MessageBox.Show(e.Message, "[JavaScript Alert]"); | |
ShowJavaScriptConfirmPanel += (s, e) => | |
{ | |
e.ReturnValue = | |
MessageBox.Show( | |
e.Message, | |
"[JavaScript Confirm]", | |
MessageBoxButtons.YesNo) == DialogResult.Yes; | |
}; | |
ShowJavaScriptPromptPanel += (s, e) => | |
{ | |
var dlg = new JavaScriptPrompt(e.Message, e.DefaultValue); | |
if (dlg.ShowDialog() == DialogResult.OK) | |
e.ReturnValue = dlg.PromptText; | |
}; | |
// history (初期値は「スピードダイアル」の代用 => 「優先URL」) | |
history = new Collection<string>(); | |
history.Clear(); | |
history.Add("http://www.tumblr.com/dashboard"); | |
history.Add("http://b.hatena.ne.jp/"); | |
history.Add("http://twitter.com/"); | |
history.Add("http://www.google.co.jp/"); | |
history.Add("http://www.google.com/notebook/?hl=ja"); | |
history.Add("http://www.asahi.com/"); | |
histories = new List<ToolStripMenuItem>(); | |
} | |
public new void Navigate(string url) | |
{ | |
if (url.StartsWith("javascript:")) | |
{ | |
#if false | |
JSContext ctx = (JSContext)GetGlobalScriptContext(); | |
JSValue val = ctx.EvaluateScript(url); | |
if (val == null) | |
StringByEvaluatingJavaScriptFromString(url); | |
#else | |
StringByEvaluatingJavaScriptFromString(url); | |
#endif | |
} | |
else | |
{ | |
// なぜか https:// ではじまる Web ページに関しては base.Navigate(url) が | |
// うまく機能しないので、WebRequest を使って何とかする | |
// (あくまでも base.Navigate(url) がちゃんと機能するようになるまでの暫定対処) | |
if (url.StartsWith("https://")) | |
{ | |
CookieContainer cc = new CookieContainer(); | |
string text = GetWebPage(url, cc); | |
if (text != null && text.Length > 0) | |
{ | |
DocumentText = text; // なぜか text の値が反映されない場合がある (例: https://github.com/) | |
if (DocumentText == string.Empty) | |
DocumentText = | |
@"<html> | |
<head><title>https test</title></head> | |
<body><p>https test page</p></body> | |
</html>"; | |
} | |
} | |
else | |
base.Navigate(url); | |
} | |
} | |
public string GetWebPage(string url, CookieContainer cc) | |
{ | |
WebProxy proxy = null; | |
WebRequest.DefaultWebProxy = null; | |
bool useProxy = Properties.Settings.Default.useProxy; | |
if (useProxy) | |
{ | |
string proxyServerURL = Properties.Settings.Default.proxyServer; | |
int proxyServerPortNumber = Properties.Settings.Default.proxyPort; | |
string proxyUserName = Properties.Settings.Default.proxyUsername; | |
string proxyPassword = Properties.Settings.Default.proxyPassword; | |
if ((proxyServerPortNumber > UInt16.MinValue) && | |
(proxyServerPortNumber <= UInt16.MaxValue)) | |
{ | |
proxy = new WebProxy( | |
proxyServerURL + ":" + proxyServerPortNumber.ToString()); | |
if ((proxyUserName.Length > 0) && (proxyPassword.Length > 0)) | |
proxy.Credentials = | |
new NetworkCredential(proxyUserName, proxyPassword); | |
WebRequest.DefaultWebProxy = proxy; | |
} | |
} | |
string result = ""; | |
Encoding encode = Encoding.GetEncoding("utf-8"); | |
// 指定Webページを取得 | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | |
request.Method = "GET"; | |
request.UserAgent = UserAgent; | |
if (cc != null) | |
request.CookieContainer = cc; | |
// はてブ対策 (http://d.hatena.ne.jp/tomity/20080213/1202890384) | |
ServicePoint currentServicePoint = request.ServicePoint; | |
if (currentServicePoint != null) | |
currentServicePoint.Expect100Continue = false; | |
request.AllowAutoRedirect = true; | |
HttpWebResponse response = SendRequest(request); | |
if (response == null) | |
return result; | |
Stream receiveStream = response.GetResponseStream(); | |
StreamReader readStream = new StreamReader(receiveStream, encode); | |
while (!readStream.EndOfStream) | |
{ | |
result += readStream.ReadLine(); | |
} | |
readStream.Close(); | |
response.Close(); | |
return result; | |
} | |
public static HttpWebResponse SendRequest(HttpWebRequest request) | |
{ | |
HttpWebResponse response = null; | |
try | |
{ | |
response = (HttpWebResponse)request.GetResponse(); | |
} | |
catch (Exception ee) | |
{ | |
string url = request.Address.ToString(); | |
if (ee.GetType() == typeof(WebException)) | |
{ | |
WebException we = (WebException)ee; | |
string statusString = we.Status.ToString(); | |
string messegeString = we.ToString(); | |
int pos = messegeString.IndexOf("\r\n"); | |
if (pos > 0) | |
messegeString = messegeString.Substring(0, pos - 1); | |
if (statusString.CompareTo(messegeString) != 0) | |
{ | |
statusString += " :: " + messegeString; | |
} | |
if (we.Status == WebExceptionStatus.ProtocolError && we.Response != null) | |
MessageBox.Show( | |
url + ": Cannot access this web page" + "\r\n" + | |
" Details: " + | |
((HttpWebResponse)we.Response).StatusCode + "\r\n" + | |
"[" + ((HttpWebResponse)we.Response).StatusDescription + "]", | |
"Server Error"); | |
else | |
MessageBox.Show( | |
url + ": Cannot access this web page" + "(" + statusString + ")", | |
"Server Error"); | |
} | |
else | |
MessageBox.Show( | |
url + ": Some error occurred" + " \r\n" + | |
"(" + ee.Message + ")", | |
"Error"); | |
response = null; | |
} | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment