This repository has been archived by the owner on May 6, 2019. It is now read-only.
/
MainForm.cs
377 lines (320 loc) · 14.9 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2009, Peter Nelson (charn.opcode@gmail.com)
* 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.
*
* 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 HOLDER 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.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.Threading;
using WebKit;
using WebKit.JSCore;
namespace WebKitBrowserTest
{
public partial class MainForm : Form
{
WebBrowserTabPage currentPage;
public MainForm()
{
InitializeComponent();
// an an initial tab
WebBrowserTabPage page = new WebBrowserTabPage();
tabControl.TabPages.Add(page);
currentPage = page;
RegisterBrowserEvents();
// tabcontrol events
tabControl.SelectedIndexChanged += (s, e) =>
{
if (currentPage != null)
UnregisterBrowserEvents();
currentPage = (WebBrowserTabPage)tabControl.SelectedTab;
if (currentPage != null)
{
RegisterBrowserEvents();
if (currentPage.browser.Url != null)
navigationBar.UrlText = currentPage.browser.Url.ToString();
else
navigationBar.UrlText = "";
SetCookieMenuItemText();
this.Text = "WebKit Browser Example - " + currentPage.browser.DocumentTitle;
currentPage.browser.Focus();
}
};
// navigation bar events
navigationBar.Back += () => { currentPage.browser.GoBack(); ActivateBrowser(); };
navigationBar.Forward += () => { currentPage.browser.GoForward(); ActivateBrowser(); };
navigationBar.Go += () => { currentPage.browser.Navigate(navigationBar.UrlText); ActivateBrowser(); };
navigationBar.Refresh += () => { currentPage.browser.Reload(); ActivateBrowser(); };
navigationBar.Stop += () => { currentPage.Stop(); ActivateBrowser(); };
SetCookieMenuItemText();
}
private void SetCookieMenuItemText()
{
cookiesToolStripMenuItem.Text = currentPage.browser.CookieAcceptPolicy == CookieAcceptPolicy.Always ?
"Disable cookies" : "Enable cookies";
}
private void RegisterBrowserEvents()
{
currentPage.browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
currentPage.browser.Navigated += new WebBrowserNavigatedEventHandler(browser_Navigated);
currentPage.browser.DocumentTitleChanged += new EventHandler(browser_DocumentTitleChanged);
currentPage.browser.Error += new WebKit.WebKitBrowserErrorEventHandler(browser_Error);
currentPage.browser.DownloadBegin += new FileDownloadBeginEventHandler(browser_DownloadBegin);
currentPage.browser.NewWindowRequest += new NewWindowRequestEventHandler(browser_NewWindowRequest);
currentPage.browser.NewWindowCreated += new NewWindowCreatedEventHandler(browser_NewWindowCreated);
}
void browser_NewWindowCreated(object sender, NewWindowCreatedEventArgs args)
{
tabControl.TabPages.Add(new WebBrowserTabPage(new WebKitBrowser((WebKitBrowserCore)args.WebKitBrowser), false));
}
void browser_NewWindowRequest(object sender, NewWindowRequestEventArgs args)
{
args.Cancel = (MessageBox.Show(args.Url, "Open new window?", MessageBoxButtons.YesNo) == DialogResult.No);
}
void browser_DownloadBegin(object sender, FileDownloadBeginEventArgs args)
{
DownloadForm frm = new DownloadForm(args.Download);
}
void browser_Error(object sender, WebKitBrowserErrorEventArgs args)
{
if (currentPage != null)
currentPage.browser.DocumentText = "<html><head><title>Error</title></head><center><p>" + args.Description + "</p></center></html>";
}
void browser_DocumentTitleChanged(object sender, EventArgs e)
{
this.Text = "WebKit Browser Example - " + currentPage.browser.DocumentTitle;
}
void browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (currentPage.browser.Url != null)
navigationBar.UrlText = currentPage.browser.Url.ToString();
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (currentPage.browser.Url != null)
navigationBar.UrlText = currentPage.browser.Url.ToString();
navigationBar.CanGoBack = currentPage.browser.CanGoBack;
navigationBar.CanGoForward = currentPage.browser.CanGoForward;
}
private void UnregisterBrowserEvents()
{
currentPage.browser.DocumentCompleted -= browser_DocumentCompleted;
currentPage.browser.Navigated -= browser_Navigated;
currentPage.browser.DocumentTitleChanged -= browser_DocumentTitleChanged;
currentPage.browser.NewWindowCreated -= browser_NewWindowCreated;
currentPage.browser.NewWindowRequest -= browser_NewWindowRequest;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
{
WebBrowserTabPage page = new WebBrowserTabPage();
tabControl.TabPages.Add(page);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("www.sourceforge.net/projects/webkitdotnet\n\nWebKitBrowser version " + currentPage.browser.Version, "About WebKit.NET");
}
private void pageSourceToolStripMenuItem_Click(object sender, EventArgs e)
{
(new SourceViewForm(currentPage.browser.DocumentText, currentPage.browser)).Show();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (currentPage.browser.SelectedText != null)
Clipboard.SetText(currentPage.browser.SelectedText);
}
catch (Exception)
{
}
}
private void MainForm_Shown(object sender, EventArgs e)
{
ActivateBrowser();
}
private void ActivateBrowser()
{
if (currentPage.browser.CanFocus)
currentPage.browser.Focus();
}
private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage page = currentPage;
tabControl.Controls.Remove(page);
page.Dispose();
if (tabControl.Controls.Count == 0)
Application.Exit();
}
private void testPageToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.DocumentText =
"<html><head><title>Test Page</title></head><body>" +
"<p id=\"testelement\" style=\"color: red\">Hello, World!</p>" +
"<div><p>A</p><p>B</p><p>C</p></div>" +
"<script type=\"text/javascript\">" +
"function f() { return 'Hello, C#!'; }</script>" +
"</body></html>";
}
private void tToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.StringByEvaluatingJavaScriptFromString("f()");
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.ShowPrintDialog();
}
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.ShowPageSetupDialog();
}
private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("This is likely to cause a crash. Continue?",
"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
var thread = new Thread(new ThreadStart(MyThread));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
}
private void MyThread()
{
Application.Run(new MainForm());
}
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.ShowPrintPreviewDialog();
}
private void printImmediatelyToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.Print();
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
JSValue val = ctx.EvaluateScript("f()");
MessageBox.Show(val.ToString());
}
private void jSTestPageToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.DocumentText = @"<!DOCTYPE html>
<html lang=""eng"">
<head>
<script>
var myDog;
window.onload = function() {
setTimeout(function() {
window.external.callback(function(x) {
alert('hello ' + x);
});
}, 100);
}
</script>
</head>
<body>
<p id=""dog"">Testing callbacks...</p>
</body>
</html>
";
JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
TestClass myTest = new TestClass();
currentPage.browser.ObjectForScripting = myTest;
}
private void test3ToolStripMenuItem_Click(object sender, EventArgs e)
{
JSContext ctx = (JSContext) currentPage.browser.GetGlobalScriptContext();
var window = ctx.GetGlobalObject();
dynamic doStuff = window.GetProperty("doStuff");
doStuff("hello!");
/*JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
TestClass myTest = new TestClass()
{
ctx = ctx
};
currentPage.browser.ObjectForScripting = myTest;*/
//ctx.EvaluateScript("test()");
//ctx.GarbageCollect();
}
private void inspectorToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.ShowInspector();
}
private void testTextAreaToolStripMenuItem_Click(object sender, EventArgs e)
{
/*JSContext ctx = (JSContext) currentPage.browser.GetGlobalScriptContext();
JSObject doc = ctx.GetGlobalObject().GetProperty("document").ToObject();
JSObject txt = ctx.EvaluateScript("getNewTextArea()").ToObject();//ctx.GetGlobalObject().CallFunction("getNewTextArea").ToObject();//doc.CallFunction("createElement", "textarea").ToObject();
txt.SetProperty("id", "textAreaId");
txt.SetProperty("rows", "5");
txt.SetProperty("onclick", (args) => { if (txt.GetProperty("textContent").ToString() == "Enter your comments here...") txt.SetProperty("textContent", ""); });
txt.SetProperty("onblur", (args) => { if (txt.GetProperty("textContent").ToString() == "") txt.SetProperty("textContent", "Enter your comments here..."); });
txt.SetProperty("textContent", "Enter your comments here...");
JSObject body = doc.CallFunction("getElementById", "container").ToObject();
body.CallFunction("appendChild", txt);*/
// Above, but using DLR
dynamic ctx = currentPage.browser.GetGlobalScriptContext();
dynamic document = ctx.GetGlobalObject().document;
dynamic txt = document.createElement("textarea");
txt.id = "textAreaId";
txt.rows = 5;
txt.textContent = "Enter your comments here...";
txt.onclick = new ActionDelegate((args) => { if (txt.textContent == "Enter your comments here...") txt.textContent = ""; });
txt.onblur = new ActionDelegate((args) => { if (txt.textContent == "") txt.textContent = "Enter your comments here..."; });
document.getElementById("container").appendChild(txt);
dynamic btn = document.createElement("button");
btn.textContent = "What's in my textarea?";
btn.onclick = new ActionDelegate((args) => { MessageBox.Show("Your textarea says \n\n'" + txt.textContent + "'"); });
document.getElementById("container").appendChild(btn);
}
private void setLocalStorageDatabaseDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
var dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
currentPage.browser.LocalStorageDatabaseDirectory = dlg.SelectedPath;
}
}
private void cookiesToolStripMenuItem_Click(object sender, EventArgs e)
{
currentPage.browser.CookieAcceptPolicy = currentPage.browser.CookieAcceptPolicy == CookieAcceptPolicy.Always ? CookieAcceptPolicy.Never : CookieAcceptPolicy.Always;
SetCookieMenuItemText();
}
private void setCertificateToolStripMenuItem_Click(object sender, EventArgs e)
{
var f = new OpenFileDialog();
f.CheckFileExists = true;
f.Multiselect = false;
if (f.ShowDialog() == DialogResult.OK)
{
var filename = f.FileName;
var certificate = new X509Certificate(f.FileName);
MessageBox.Show(certificate.Issuer);
currentPage.browser.ClientCertificate = certificate;
}
}
}
}