Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a bit of code to test JSCore stuff

Signed-off-by: Peter Nelson <peter@peterdn.com>
  • Loading branch information...
commit f2bbd5260f15756e368c21108f42934bf1d7a4ae 1 parent b0a96e1
@peterdn peterdn authored
View
40 WebKitBrowserTest/MainForm.Designer.cs
@@ -76,9 +76,11 @@ private void InitializeComponent()
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator();
this.tToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.newWindowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.tabControl = new System.Windows.Forms.TabControl();
+ this.jSTestPageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+ this.test3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.navigationBar = new WebKitBrowserTest.NavigationBar();
- this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
@@ -213,7 +215,7 @@ private void InitializeComponent()
// aboutToolStripMenuItem
//
this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
- this.aboutToolStripMenuItem.Size = new System.Drawing.Size(107, 22);
+ this.aboutToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.aboutToolStripMenuItem.Text = "&About";
this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
//
@@ -224,7 +226,9 @@ private void InitializeComponent()
this.toolStripMenuItem1,
this.tToolStripMenuItem,
this.newWindowToolStripMenuItem,
- this.test2ToolStripMenuItem});
+ this.test2ToolStripMenuItem,
+ this.jSTestPageToolStripMenuItem,
+ this.test3ToolStripMenuItem});
this.testToolStripMenuItem.Name = "testToolStripMenuItem";
this.testToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
this.testToolStripMenuItem.Text = "Test";
@@ -255,6 +259,13 @@ private void InitializeComponent()
this.newWindowToolStripMenuItem.Text = "New &Window";
this.newWindowToolStripMenuItem.Click += new System.EventHandler(this.newWindowToolStripMenuItem_Click);
//
+ // test2ToolStripMenuItem
+ //
+ this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem";
+ this.test2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
+ this.test2ToolStripMenuItem.Text = "Test 2";
+ this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click);
+ //
// tabControl
//
this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
@@ -266,6 +277,20 @@ private void InitializeComponent()
this.tabControl.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
this.tabControl.TabIndex = 2;
//
+ // jSTestPageToolStripMenuItem
+ //
+ this.jSTestPageToolStripMenuItem.Name = "jSTestPageToolStripMenuItem";
+ this.jSTestPageToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
+ this.jSTestPageToolStripMenuItem.Text = "JS Test Page";
+ this.jSTestPageToolStripMenuItem.Click += new System.EventHandler(this.jSTestPageToolStripMenuItem_Click);
+ //
+ // test3ToolStripMenuItem
+ //
+ this.test3ToolStripMenuItem.Name = "test3ToolStripMenuItem";
+ this.test3ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
+ this.test3ToolStripMenuItem.Text = "Test 3";
+ this.test3ToolStripMenuItem.Click += new System.EventHandler(this.test3ToolStripMenuItem_Click);
+ //
// navigationBar
//
this.navigationBar.Dock = System.Windows.Forms.DockStyle.Top;
@@ -275,13 +300,6 @@ private void InitializeComponent()
this.navigationBar.TabIndex = 1;
this.navigationBar.UrlText = "";
//
- // test2ToolStripMenuItem
- //
- this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem";
- this.test2ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
- this.test2ToolStripMenuItem.Text = "Test 2";
- this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click);
- //
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -328,6 +346,8 @@ private void InitializeComponent()
private System.Windows.Forms.ToolStripMenuItem printPreviewToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem printImmediatelyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test2ToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem jSTestPageToolStripMenuItem;
+ private System.Windows.Forms.ToolStripMenuItem test3ToolStripMenuItem;
}
}
View
63 WebKitBrowserTest/MainForm.cs
@@ -122,12 +122,14 @@ void browser_DocumentTitleChanged(object sender, EventArgs e)
void browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
- navigationBar.UrlText = currentPage.browser.Url.ToString();
+ if (currentPage.browser.Url != null)
+ navigationBar.UrlText = currentPage.browser.Url.ToString();
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
- navigationBar.UrlText = currentPage.browser.Url.ToString();
+ if (currentPage.browser.Url != null)
+ navigationBar.UrlText = currentPage.browser.Url.ToString();
navigationBar.CanGoBack = currentPage.browser.CanGoBack;
navigationBar.CanGoForward = currentPage.browser.CanGoForward;
@@ -254,5 +256,62 @@ private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
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() {
+
+}
+function dog(age, breed) {
+ this.age = age;
+ this.breed = breed;
+}
+dog.prototype.woof = function(wat) {
+ document.getElementById(""dog"").innerHTML = ""woof! "" + wat;
+}
+function someDog(age, breed) {
+ myDog = new dog(age, breed);
+ return myDog;
+}
+function printDog(dog) {
+ var txt = """";
+ for (var p in dog)
+ txt += p + "": "" + dog[p] + ""<br />"";
+ alert(txt);
+ document.getElementById(""dog"").innerHTML = txt;
+}
+</script>
+</head>
+<body>
+<p id=""dog"">Hi!</p>
+</body>
+</html>
+";
+ }
+
+ private void test3ToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ JSContext ctx = (JSContext)currentPage.browser.GetGlobalScriptContext();
+ JSObject dog = ctx.EvaluateScript("someDog(12, \"Golden Retriever\");").ToObject();
+ if (dog != null)
+ {
+ if (dog.HasProperty("breed"))
+ {
+ MessageBox.Show("breed = " + dog.GetProperty("breed").ToString());
+ dog.SetProperty("breed", "Border Collie");
+ MessageBox.Show("breed = " + dog.GetProperty("breed").ToString());
+ dog.SetProperty("name", "Holly");
+ MessageBox.Show("name = " + dog.GetProperty("name").ToString());
+ ctx.EvaluateScript("printDog(myDog)");
+ //ctx.GarbageCollect();
+ }
+ }
+ }
}
}
View
10 WebKitBrowserTest/WebBrowserTabPage.cs
@@ -29,6 +29,14 @@
namespace WebKitBrowserTest
{
+ public class TestScriptObject
+ {
+ public void f()
+ {
+ MessageBox.Show("Hey!");
+ }
+ }
+
public partial class WebBrowserTabPage : TabPage
{
public WebKitBrowser browser;
@@ -80,6 +88,8 @@ public WebBrowserTabPage(WebKitBrowser browserControl, bool goHome)
//browser.IsScriptingEnabled = false;
container.ContentPanel.Controls.Add(browser);
+ browser.ObjectForScripting = new TestScriptObject();
+
// context menu
this.Controls.Add(container);
Please sign in to comment.
Something went wrong with that request. Please try again.