帮助大家学习和分析网页上的遇到格式混乱的js工具代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace FormatCode
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
string[] strI;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.LinkLabel linkLabel2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.linkLabel2 = new System.Windows.Forms.LinkLabel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 30);
this.button1.TabIndex = 0;
this.button1.Text = "打开文件";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Enabled = false;
this.textBox1.Location = new System.Drawing.Point(40, 120);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(376, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(40, 232);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 32);
this.button2.TabIndex = 2;
this.button2.Text = "整理保存代码";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(40, 200);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(376, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panel1.Controls.Add(this.linkLabel2);
this.panel1.Controls.Add(this.linkLabel1);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(520, 88);
this.panel1.TabIndex = 4;
//
// label1
//
this.label1.Font = new System.Drawing.Font("黑体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(336, 23);
this.label1.TabIndex = 0;
this.label1.Text = "JavaScript代码整理程序.net V1.0";
//
// panel2
//
this.panel2.BackColor = System.Drawing.SystemColors.Desktop;
this.panel2.Location = new System.Drawing.Point(0, 88);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(504, 2);
this.panel2.TabIndex = 5;
//
// linkLabel1
//
this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(5, 4);
this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel1.Location = new System.Drawing.Point(360, 48);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(120, 16);
this.linkLabel1.TabIndex = 1;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "版权所有:任丘热线";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// linkLabel2
//
this.linkLabel2.LinkArea = new System.Windows.Forms.LinkArea(5, 3);
this.linkLabel2.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel2.Location = new System.Drawing.Point(360, 64);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.Size = new System.Drawing.Size(120, 16);
this.linkLabel2.TabIndex = 3;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "程序设计:CLX";
this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
//
// Form1
//
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(498, 303);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Javascript代码整理程序";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1 =new OpenFileDialog();
this.openFileDialog1.ShowDialog();
this.textBox1.Text=this.openFileDialog1 .FileName;
if(this.textBox1.Text != "")
{
strI=this.textBox1.Text.Split('//');
strI[strI.Length-1]="Temp_"+strI[strI.Length-1];
this.textBox2.Text=strI[strI.Length-1];
}
else
{
this.textBox2.Text="";
}
}
private void textBox2_TextChanged(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
if(this.textBox1.Text!="" && this.textBox2.Text!="" )
{
this.strI[this.strI.Length-1]=this.textBox2.Text;
System.IO .StreamReader sr=new System.IO.StreamReader(this.textBox1.Text);
int i;
string line;
string xg="";
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
bool j=true;
bool a=true;
bool b=true;
int t=0;
for(i=0;i<line.Length ;i++)
{
if(line[i]=='/'')
j=!j;
if(line[i]=='"')
b=!b;
if(line[i]=='(' || line[i]==')')
a=!a;
if(line[i]!=';')
{
if(line[i]=='{' )
{
xg=xg+'/n';
for(int s=0;s<t;s++)
{xg=xg+" ";}
t+=2;
xg=xg + line[i]+'/n';
for(int s=0;s<t;s++)
{xg=xg+" ";}
}
else
{
if( line[i]=='}')
{
t-=2;
xg=xg+'/n';
for(int s=0;s<t;s++)
{xg=xg+" ";}
xg=xg + line[i]+'/n';
for(int s=0;s<t;s++)
{xg=xg+" ";}
}
else
{
xg=xg+line[i];
}
}
}
else
{
if(j && a && b)
{
xg=xg+line[i]+'/n';
for(int s=0;s<t;s++)
{xg=xg+" ";}
}
else
xg=xg+line[i];
}
}
}
string FILE_NAME=this.strI[0];
for(i=1;i<this.strI.Length;i++)
FILE_NAME=FILE_NAME+'//'+this.strI[i];
StreamWriter sr1 = File.CreateText(FILE_NAME);
sr1.WriteLine (xg);
sr1.Close();
}
else
{
this.textBox2.Text="请先选择一个代码文件!";
}
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("www.rqrx.cn");
}
private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("mailto:tx_clx@sina.com");
}
}
}