Esempio n. 1
0
        private void cmdInvokeJavascript_Click(object sender, EventArgs e)
        {
            // 从当前的 WebBrowser 控件创建 IEBrowser 对象, WebBrowser 的 Url 属性已经设置为 "about:blank".
            IEBrowser ie = new IEBrowser(this.webBrowser);

            // 安装定义函数 setName 和 getName 的函数到 WebBrowser.
            ie.InstallScript("var name;function setName(name){this.name = name;}function getName(){return this.name;}");

            // 调用 setName 函数, 参数为 "jack".
            ie.InvokeScript("setName", new object[] { "jack" });

            // 调用 getName 函数, 得到结果并弹出对话框.
            MessageBox.Show(ie.InvokeScript("getName").ToString( ));
        }
        private void cmdSub_Click(object sender, EventArgs e)
        {
            // 创建 IEBrowser 对象, 用来控制窗口的 WebBrowser 控件.
            IEBrowser ie = new IEBrowser(this.webBrowser);

            // 载入已经放在运行目录的页面 ButtonClick.htm.
            ie.Navigate(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "ButtonClick.htm"));

            // 等待 ButtonClick.htm 完全载入.
            ie.IEFlow.Wait(new UrlCondition("wait", "ButtonClick.htm", StringCompareMode.EndWith));

            // 根据按钮显示的文本模拟按钮点击.

            // 方法1: 安装搜索按钮的 javascript 函数 clickButton 来获取按钮并调用其 click 方法.
            // 安装 clickButton 函数.
            ie.InstallScript("function clickButton(text){for(var i=0;i<document.all.length;i++){if(document.all[i].value==text){document.all[i].click();break;}}}");
            // 调用 clickButton 函数, 模拟点击文本为 Sub 的按钮.
            ie.InvokeScript("clickButton", new object[] { "Sub" });

            // 方法2: 安装跟踪和 jQuery 脚本后, 执行 jQuery 来模拟点击按钮.
            // 安装跟踪脚本.
            ie.InstallTrace( );
            // 安装在资源中的 jQuery 脚本.
            ie.InstallScript(Properties.Resources.jquery_1_5_2_min);
            // 执行获取按钮并模拟点击的 jQuery 脚本.
            ie.ExecuteJQuery(JQuery.Create("'[value=Sub]:button'").Click( ));
        }
		private void cmdSub_Click ( object sender, EventArgs e )
		{
			// 创建 IEBrowser 对象, 用来控制窗口的 WebBrowser 控件.
			IEBrowser ie = new IEBrowser ( this.webBrowser );
			// 载入已经放在运行目录的页面 ButtonClick.htm.
			ie.Navigate ( Path.Combine ( AppDomain.CurrentDomain.BaseDirectory + "ButtonClick.htm" ) );

			// 等待 ButtonClick.htm 完全载入.
			ie.IEFlow.Wait ( new UrlCondition ( "wait", "ButtonClick.htm", StringCompareMode.EndWith ) );

			// 根据按钮显示的文本模拟按钮点击.

			// 方法1: 安装搜索按钮的 javascript 函数 clickButton 来获取按钮并调用其 click 方法.
			// 安装 clickButton 函数.
			ie.InstallScript ( "function clickButton(text){for(var i=0;i<document.all.length;i++){if(document.all[i].value==text){document.all[i].click();break;}}}" );
			// 调用 clickButton 函数, 模拟点击文本为 Sub 的按钮.
			ie.InvokeScript ( "clickButton", new object[] { "Sub" } );

			// 方法2: 安装跟踪和 jQuery 脚本后, 执行 jQuery 来模拟点击按钮.
			// 安装跟踪脚本.
			ie.InstallTrace ( );
			// 安装在资源中的 jQuery 脚本.
			ie.InstallScript ( Properties.Resources.jquery_1_5_2_min );
			// 执行获取按钮并模拟点击的 jQuery 脚本.
			ie.ExecuteJQuery ( JQuery.Create ( "'[value=Sub]:button'" ).Click ( ) );
		}
Esempio n. 4
0
 private void DebugConsole(string info)
 {
     Invoke(new MethodInvoker(delegate
     {
         try
         {
             ie.IEFlow.Wait(new UrlCondition("wait", @"", StringCompareMode.Contain), 1);
             ie.InstallScript("function _HasReceiver(){ if(window.__receive) return true; return false; }", "runner");
             if ("True".Equals(ie.InvokeScript("_HasReceiver").ToString()))
             {
                 ie.InvokeScript("__receive", new object[] { info });
             }
         }
         catch (Exception e)
         {
             Log(e.Message);
         }
     }));
 }
Esempio n. 5
0
		private void cmdInvokeJavascript_Click ( object sender, EventArgs e )
		{
			// 从当前的 WebBrowser 控件创建 IEBrowser 对象, WebBrowser 的 Url 属性已经设置为 "about:blank".
			IEBrowser ie = new IEBrowser ( this.webBrowser );

			// 安装定义函数 setName 和 getName 的函数到 WebBrowser.
			ie.InstallScript ( "var name;function setName(name){this.name = name;}function getName(){return this.name;}" );

			// 调用 setName 函数, 参数为 "jack".
			ie.InvokeScript ( "setName", new object[] { "jack" } );

			// 调用 getName 函数, 得到结果并弹出对话框.
			MessageBox.Show ( ie.InvokeScript ( "getName" ).ToString ( ) );
		}
Esempio n. 6
0
		public static void Discuz ( IEBrowser ie, string userName, string password, string url, string title, string content )
		{

			// 导航到页面 dz 论坛的页面.
			ie.Navigate ( url );

			// 等待 10 秒钟, 以便页面载入完毕.
			ie.IEFlow.Wait ( 10 );

			// 安装资源中的 jquery 脚本.
			ie.InstallJQuery ( Properties.Resources.jquery_1_5_2_min );

			// 解除 jquery 的 $ 定义, 但仍然可以只用 jQuery 定义.
			ie.ExecuteJQuery ( JQuery.Create ( false, true ).NoConflict ( ) );

			// 安装 javascript 函数 clickLink, 根据超链接的文本点击超链接.
			ie.InstallScript (
				"function clickLink(text) {" +
				"	links = document.getElementsByTagName('a');" +
				"	for(var index = 0; index < links.length; index++)" +
				"	{" +
				"		if(links[index].innerText == text)" +
				"		{" +
				"			links[index].click();" +
				"			break;" +
				"		}" +
				"	}" +
				"}"
				);

			// 是否存在已经登录后显示的短消息.
			if ( ie.ExecuteJQuery<int> ( JQuery.Create ( "'#pm_ntc'", false ).Length ( ) ) == 1 )
			{
				// 调用 javascript 函数 clickLink, 模拟点击退出链接.
				ie.InvokeScript ( "clickLink", new object[] { "退出" } );

				// 等待 3 秒钟, 以便退出完毕.
				ie.IEFlow.Wait ( 3 );

				// 重新调用自身.
				Discuz ( ie, userName, password, url, title, content );
				return;
			}

			// 设置用户名.
			ie.ExecuteJQuery ( JQuery.Create ( "'#ls_username'", false ).Val ( "'"+userName+"'" ) );

			// 设置密码.
			ie.ExecuteJQuery ( JQuery.Create ( "'#ls_password'", false ).Val ( "'"+password+"'" ) );

			// 密码框获得焦点.
			ie.ExecuteJQuery ( JQuery.Create ( "'#ls_password'", false ).Focus ( ) );

			// 等待 5 秒钟以显示验证码.
			ie.IEFlow.Wait ( 5 );

			// 获取验证码并显示给用户输入.
			FormVCode vCodeWindow = new FormVCode (  );
			vCodeWindow.Image = ie.CopyImage ( "'vcodeimg1'" );

			// 用户是否确认.
			if ( vCodeWindow.ShowDialog ( ) == DialogResult.OK )
			{
				// 验证码框获得焦点.
				ie.ExecuteJQuery ( JQuery.Create ( "'#vcodetext_header1'", false ).Focus ( ) );
				
				// 填写验证码并多加 1.
				ie.ExecuteJQuery ( JQuery.Create ( "'#vcodetext_header1'", false ).Val ( "'" + vCodeWindow.VCode + "1'" ) );

				// 模拟一个退格键, 删除掉 1.
				SendKeys.Send ( "{Backspace}" );

				// 等待 2 秒.
				ie.IEFlow.Wait ( 2 );

				// 登录框提交.
				ie.ExecuteJQuery ( JQuery.Create ( "'#lsform'", false ).Submit() );

				// 等待 5 秒, 以便登录完成.
				ie.IEFlow.Wait ( 5 );

				// 是否是验证码错误.
				if ( ie.ExecuteJQuery<int> ( JQuery.Create ( "'p:contains(验证码错误)'", false ).Length ( ) ) == 1 )
				{
					// 验证码错误重新登录.
					Discuz ( ie, userName, password, url, title, content );
					return;
				}

			TOPIC:
				// 随机导航至某一话题.
				ie.Navigate ( "http://nt.discuz.net/showtopic-" + new Random ( ).Next ( 11000, 18000 ).ToString() + ".html" );

				// 等待 5 秒, 以便页面完成.
				ie.IEFlow.Wait ( 5 );

				// 安装 jquery 脚本的一系列操作.
				ie.InstallJQuery ( Properties.Resources.jquery_1_5_2_min );
				ie.ExecuteJQuery ( JQuery.Create ( false, true ).NoConflict ( ) );

				// 话题不存在则重新选择.
				if ( ie.ExecuteJQuery<int> ( JQuery.Create ( "'p:contains(该主题不存在)'", false ).Length ( ) ) == 1 )
					goto TOPIC;

				// 切换源码编辑方式.
				ie.InvokeScript ( "clickLink", new object[] { "Code" } );

				// 填写内容.
				ie.ExecuteJQuery ( JQuery.Create ( "'#quickpostmessage'", false ).Val ( "'" + content + "'" ) );

				// 验证码框获得焦点.
				ie.ExecuteJQuery ( JQuery.Create ( "'#vcodetext1'", false ).Focus ( ) );

				// 等待 5 秒钟以显示验证码.
				ie.IEFlow.Wait ( 3 );

				// 获取验证码并显示给用户输入.
				vCodeWindow = new FormVCode ( );
				vCodeWindow.Image = ie.CopyImage ( "'vcodeimg1'" );

				// 用户是否确认.
				if ( vCodeWindow.DialogResult == DialogResult.OK )
				{
					// 填写验证码并多加1.
					ie.ExecuteJQuery ( JQuery.Create ( "'#vcodetext1'", false ).Val ( "'" + vCodeWindow.VCode + "1'" ) );

					// 模拟一个退格键, 删除掉 1.
					SendKeys.Send ( "{Backspace}" );

					// 等待 2 秒.
					ie.IEFlow.Wait ( 3 );

					// 提交.
					ie.ExecuteJQuery ( JQuery.Create ( "'#quickpostsubmit'", false ).Click ( ) );
				}
				else
				{
					Discuz ( ie, userName, password, url, title, content );
					return;
				}

			}
			else
			{
				Discuz ( ie, userName, password, url, title, content );
				return;
			}

		}
Esempio n. 7
0
        public static void Discuz(IEBrowser ie, string userName, string password, string url, string title, string content)
        {
            // 导航到页面 dz 论坛的页面.
            ie.Navigate(url);

            // 等待 10 秒钟, 以便页面载入完毕.
            ie.IEFlow.Wait(10);

            // 安装资源中的 jquery 脚本.
            ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min);

            // 解除 jquery 的 $ 定义, 但仍然可以只用 jQuery 定义.
            ie.ExecuteJQuery(JQuery.Create(false, true).NoConflict( ));

            // 安装 javascript 函数 clickLink, 根据超链接的文本点击超链接.
            ie.InstallScript(
                "function clickLink(text) {" +
                "	links = document.getElementsByTagName('a');"+
                "	for(var index = 0; index < links.length; index++)"+
                "	{"+
                "		if(links[index].innerText == text)"+
                "		{"+
                "			links[index].click();"+
                "			break;"+
                "		}"+
                "	}"+
                "}"
                );

            // 是否存在已经登录后显示的短消息.
            if (ie.ExecuteJQuery <int> (JQuery.Create("'#pm_ntc'", false).Length( )) == 1)
            {
                // 调用 javascript 函数 clickLink, 模拟点击退出链接.
                ie.InvokeScript("clickLink", new object[] { "退出" });

                // 等待 3 秒钟, 以便退出完毕.
                ie.IEFlow.Wait(3);

                // 重新调用自身.
                Discuz(ie, userName, password, url, title, content);
                return;
            }

            // 设置用户名.
            ie.ExecuteJQuery(JQuery.Create("'#ls_username'", false).Val("'" + userName + "'"));

            // 设置密码.
            ie.ExecuteJQuery(JQuery.Create("'#ls_password'", false).Val("'" + password + "'"));

            // 密码框获得焦点.
            ie.ExecuteJQuery(JQuery.Create("'#ls_password'", false).Focus( ));

            // 等待 5 秒钟以显示验证码.
            ie.IEFlow.Wait(5);

            // 获取验证码并显示给用户输入.
            FormVCode vCodeWindow = new FormVCode(  );

            vCodeWindow.Image = ie.CopyImage("'vcodeimg1'");

            // 用户是否确认.
            if (vCodeWindow.ShowDialog( ) == DialogResult.OK)
            {
                // 验证码框获得焦点.
                ie.ExecuteJQuery(JQuery.Create("'#vcodetext_header1'", false).Focus( ));

                // 填写验证码并多加 1.
                ie.ExecuteJQuery(JQuery.Create("'#vcodetext_header1'", false).Val("'" + vCodeWindow.VCode + "1'"));

                // 模拟一个退格键, 删除掉 1.
                SendKeys.Send("{Backspace}");

                // 等待 2 秒.
                ie.IEFlow.Wait(2);

                // 登录框提交.
                ie.ExecuteJQuery(JQuery.Create("'#lsform'", false).Submit());

                // 等待 5 秒, 以便登录完成.
                ie.IEFlow.Wait(5);

                // 是否是验证码错误.
                if (ie.ExecuteJQuery <int> (JQuery.Create("'p:contains(验证码错误)'", false).Length( )) == 1)
                {
                    // 验证码错误重新登录.
                    Discuz(ie, userName, password, url, title, content);
                    return;
                }

TOPIC:
                // 随机导航至某一话题.
                ie.Navigate("http://nt.discuz.net/showtopic-" + new Random( ).Next(11000, 18000).ToString() + ".html");

                // 等待 5 秒, 以便页面完成.
                ie.IEFlow.Wait(5);

                // 安装 jquery 脚本的一系列操作.
                ie.InstallJQuery(Properties.Resources.jquery_1_5_2_min);
                ie.ExecuteJQuery(JQuery.Create(false, true).NoConflict( ));

                // 话题不存在则重新选择.
                if (ie.ExecuteJQuery <int> (JQuery.Create("'p:contains(该主题不存在)'", false).Length( )) == 1)
                {
                    goto TOPIC;
                }

                // 切换源码编辑方式.
                ie.InvokeScript("clickLink", new object[] { "Code" });

                // 填写内容.
                ie.ExecuteJQuery(JQuery.Create("'#quickpostmessage'", false).Val("'" + content + "'"));

                // 验证码框获得焦点.
                ie.ExecuteJQuery(JQuery.Create("'#vcodetext1'", false).Focus( ));

                // 等待 5 秒钟以显示验证码.
                ie.IEFlow.Wait(3);

                // 获取验证码并显示给用户输入.
                vCodeWindow       = new FormVCode( );
                vCodeWindow.Image = ie.CopyImage("'vcodeimg1'");

                // 用户是否确认.
                if (vCodeWindow.DialogResult == DialogResult.OK)
                {
                    // 填写验证码并多加1.
                    ie.ExecuteJQuery(JQuery.Create("'#vcodetext1'", false).Val("'" + vCodeWindow.VCode + "1'"));

                    // 模拟一个退格键, 删除掉 1.
                    SendKeys.Send("{Backspace}");

                    // 等待 2 秒.
                    ie.IEFlow.Wait(3);

                    // 提交.
                    ie.ExecuteJQuery(JQuery.Create("'#quickpostsubmit'", false).Click( ));
                }
                else
                {
                    Discuz(ie, userName, password, url, title, content);
                    return;
                }
            }
            else
            {
                Discuz(ie, userName, password, url, title, content);
                return;
            }
        }