问题描述
我正在寻找一些技术来执行javascript以从WinForms C#应用程序填充已经运行的Firefox中的表单。
I am searching for some technique to execute javascript for filling a form in an already running Firefox from an WinForms C# application.
推荐答案
C#应用程序的表单包含:TextBox name_TB,ComboBox state_CB,DateTimePicker date_DTP,Button revise_data_BUT和Button exit_BUT。有标签标记前三个组件中的每一个。
The C# application's form contains: TextBox name_TB, ComboBox state_CB, DateTimePicker date_DTP, Button revise_data_BUT, and Button exit_BUT. There are labels that label each of the first three components.
假设FormToBeFilled存在于一台服务器。在与FormToBeFilled相同的目录中,还必须存在HTMLFormData的输出,对于此示例,该输出将被称为CToHTML.txt。这是AGREED_FILENAME。 HTMLFormData写入此文件; FormToBeFilled读取此文件。
It is assumed that FormToBeFilled exists on a server. In the same directory as FormToBeFilled must also exist the output of HTMLFormData which, for this example, will be called CToHTML.txt. This is the AGREED_FILENAME. HTMLFormData writes to this file; FormToBeFilled reads from this file.
HTMLFormData没什么特别之处。其来源如下。完整的项目,包括FormToBeFilled,可以在找到。
There is nothing really special about HTMLFormData. Its source follows. The complete project, including FormToBeFilled, can be found at FillHTMLFromCSharp.zip (9.5 KB).
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace FillHTMLFromCSharp
{
// ******************************************** class HTMLFormData
public partial class HTMLFormData : Form
{
const string AGREED_FILENAME = @"C:\Users\Public\CToHTML.txt";
// ********************************************* OnFormClosing
protected override void OnFormClosing (
FormClosingEventArgs e )
{
base.OnFormClosing(e);
// do wrapup things
}
// ********************************************** HTMLFormData
public HTMLFormData ( )
{
InitializeComponent ( );
}
// ****************************************** revise_data_file
void revise_data_file ( )
{
string date = date_DTP.Text;
string message = String.Empty;
string name = name_TB.Text;
object provided_state = state_CB.SelectedItem;
string state = String.Empty;
if ( String.IsNullOrEmpty ( name ) )
{
message = "A name must be provided";
}
if ( provided_state == null )
{
if ( !String.IsNullOrEmpty ( message ) )
{
message += Environment.NewLine;
}
message += "A state must be provided";
}
else
{
state = provided_state.ToString ( );
}
if ( String.IsNullOrEmpty ( message ) )
{
using ( StreamWriter sw = new StreamWriter (
AGREED_FILENAME,
false ) )
{
StringBuilder sb = new StringBuilder ( );
sb.AppendFormat ( "{0},{1},{2}",
name,
date,
state );
sw.Write ( sb.ToString ( ));
}
}
else
{
MessageBox.Show ( message,
"Input Error" );
}
}
// ************************************************* BUT_Click
void BUT_Click ( object sender, EventArgs e )
{
Button button = ( Button ) sender;
string tag = button.Tag.ToString ( ).
ToLower ( ).
Trim ( );
switch ( tag )
{
case "exit":
Application.Exit ( );
break;
case "revise_data":
revise_data_file ( );
break;
default:
throw new ApplicationException (
"Unrecognized button pressed" );
}
}
} // class HTMLFormData
} // namespace FillHTMLFromCSharp
两件感兴趣的话:
Two items of interest:
- CToHTML.txt文件位于C:\ Users \ Public \目录中。这使得程序可以在任何地方由任何人运行而不会出现安全问题。在HTMLFormData完成其执行后,必须将CToHTML.txt文件复制到找到FormToBeFilled的目录中。部署时,应修改常量AGREED_FILENAME以指向同一目录,从而不必复制副本。
- 写入CToHTML.txt文件时,请勿使用StreamWriter WriteLine放置< cr>< lf>在行尾。这将导致解析数据的Javascript中出现问题。而是使用StreamWriter写。
为方便起见,我在FormToBeFilled中包含了Javascript 。 Javascript在窗口加载时执行。 FormToBeFilled显示为:
For expediency, I included the Javascript within FormToBeFilled. The Javascript executes on window load. FormToBeFilled appears as:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<form action="">
<table cellpadding="10" cellspacing="0">
<tr>
<td align="right">
Name
</td>
<td align="left">
<input id="name"
type="text"
style="width: 200px;"
maxlength="100"
title="Enter your name"
value="foo" />
</td>
</tr>
<tr>
<td align="right">
State
</td>
<td align="left">
<select id="states">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DC">DC</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
</td>
</tr>
<tr>
<td align="right">
Date
</td>
<td align="left">
<input id="date"
type="text"
style="width: 200px;"
maxlength="100"
title="Enter the date"
value="bar" />
</td>
</tr>
</table>
</form>
<script type="text/javascript">
window.onload = init;
function init ( )
{
if ( window.XMLHttpRequest )// IE7+, Firefox, Chrome, Opera, Safari
{
xmlhttp=new XMLHttpRequest ( );
}
else // IE6, IE5
{
xmlhttp=new ActiveXObject ( "Microsoft.XMLHTTP" );
}
xmlhttp.open ( "GET", "CToHTML.txt", false );
xmlhttp.send ( null );
response = xmlhttp.responseText;
// name,date,state
var items = response.split ( "," );
document.getElementById ( "name" ).value = items [ 0 ];
document.getElementById ( "date" ).value = items [ 1 ];
var state = items [ 2 ];
var states = document.getElementById ( "states" );
var options = states.options;
for ( var i = 0; ( i < options.length ); i++ )
{
if ( options.item ( i ).innerHTML == state )
{
states.selectedIndex = i;
break;
}
}
}
</script>
</body>
</html>
Hope that helps.
Hope that helps.
这篇关于如何在运行Firefox浏览器时从C#应用程序执行JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!