65.9K
CodeProject 正在变化。 阅读更多。
Home

IE 高级工具栏,用于收藏网站导航和登录

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.76/5 (11投票s)

2005年1月23日

3分钟阅读

viewsIcon

134933

downloadIcon

3742

高级IE工具栏,使用.NET Band Objects,实现一键导航和登录。

Sample Image - IE_Advanced_Toolbar.jpg

引言

当您想访问您的雅虎邮箱账户时,您会访问mail.yahoo.com,输入您的用户名和密码,然后点击“登录”按钮。您可以选择“雅虎记住我的ID”来节省输入用户名的麻烦,但仍然这需要大量的工作。如果您切换到另一台计算机,您将不得不再次执行此操作。在本文中,我将向您展示如何向Internet Explorer添加一个工具栏,该工具栏将导航到您喜欢的网站(mail.yahoo.com)并为您完成登录。将一些文件复制到其他计算机上也可以在其他计算机上添加工具栏按钮。

背景

本教程的基础框架,用于向Internet Explorer添加工具栏,是从Pavel Zolnikov关于Band Objects的文章中复制的。我建议您阅读这篇文章,以了解BandObjects以及如何将Explorer Bar / Toolbar添加到Internet Explorer。我扩展了他的SampleBar以添加自动登录功能。

使用工具栏

您可以使用setup.exe在您的机器上设置工具栏,或者您可以下载源代码并构建项目。一旦您的IE中有了这个工具栏,您就可以导航到您喜欢的网站,并实现单点登录,例如mail.yahoo.com。

  1. 点击工具栏上的“自定义”按钮,这将弹出一个“自定义”表单,其中包含该网站的所有链接和控件。
  2. 在“按钮标题/工具提示”编辑控件中,为此网站输入按钮标题,例如xyz@yahoo。
  3. 在列表框中,您将看到“登录”条目。选择它,在值编辑控件中输入您的登录名,然后选择“添加”按钮(输入您的用户名的操作)。
  4. 选择“密码”,输入您的密码,然后选择“添加”按钮(输入您的密码的操作)。
  5. 选择“登录”并选择“添加”按钮(点击“登录”按钮的操作)。
  6. 选择“确定”按钮,将此按钮条目添加到IE工具栏。

现在,每当您点击此按钮时,IE将首先导航到mail.yahoo.com并使用上述登录详细信息登录。您输入的用户名和密码保存在C:\IE AutoComplete目录的XML文件中。这是一个简单的文本文件,因此用户名和密码没有受到保护,请不要将此工具栏用于您的银行网站。我没有为其添加任何加密,但如果您想修改源代码,请自行操作。

要从工具栏中删除任何按钮条目,请从C:\IE AutoComplete目录中删除相应的文件。要将相同的条目添加到其他计算机,只需复制此目录内容即可。工具栏会读取此目录中的所有文件以创建配置好的按钮。

使用代码

  1. 我使用了Extending Explorer with Band Objects using .NET and Windows Forms来创建一个托管IE工具栏的用户控件。我修改了Pavel ZolnikovSampleBars.sln。在HelloWorld类中,button1被一个名为myToolBar的ToolBar控件替换了。
  2. 需要引用Microsoft.mshtml.dll才能访问HTMLDocumentClass, HTMLAnchorElementClass, IHTMLInputElement。同时在HelloWorldBar.cs的开头添加以下几行:
    using mshtml;
  3. 此工具栏添加了一个“自定义”按钮。此工具栏的点击事件将遍历活动文档以收集页面上的所有链接、按钮和输入框。Explorer.Document是当前加载的HTMLDocumentClass的实例。
    //Explorer.IWebBrowser_Document. HTMLDocumentClass doc = 
          (HTMLDocumentClass)Explorer.Document;

    两个类ItemTypeButtonEntry定义在Interface.cs文件中。ItemType类实例用于网页上的项目详细信息。ButtonEntry类用于将自定义的XML按钮条目数据读/写到文件系统。所有这些HTMLAnchorElementClassHTMLInputElementClass对象都被添加到自定义的ItemsItemType类型的ArrayList中。然后显示自定义表单供用户输入。

    private void onCustomize(object sender, System.EventArgs e)
    {
      //Explorer.IWebBrowser_Document. HTMLDocumentClass doc = 
      //           (HTMLDocumentClass)Explorer.Document;
       
      Customize frm = new Customize(); 
      frm.URL = doc.url; int count = -1; 
      foreach(object o in doc.all)
      {
        try
        {
          count++;
          if( !(o is IHTMLElement))
          {
            continue;
          } 
          IHTMLElement elem = (IHTMLElement)o; 
          if(elem.id == null)
          { 
            if(o is HTMLAnchorElementClass)
              {
                try
                {
                  HTMLAnchorElementClass aElem = (HTMLAnchorElementClass)o;
                  string id = aElem.outerText;
                  string itemInfo = "outterText"; 
                  if(id == null)
                  {
                    id = aElem.href; itemInfo = "href";
                  } 
                  frm.Items.Add(new ItemType("link",id,itemInfo));
                           
                } 
                catch(Exception /*ex_in*/)
                {
                  sp;        
                  {
                    //MessageBox.Show(ex_in.ToString(),"Exception");
                  }
                       
                  continue;
                }
              } 
              if(o is IHTMLInputElement)
              {
                HTMLInputElementClass btn = (HTMLInputElementClass)o;
                try
                {
                  if(btn.type != null)
                  {
                    string type = btn.type.ToLower(); 
                    if(type=="submit" || type == "button" || type == "radio" 
                                 || type == "checkbox" || type == "text" 
                                 || type == "password" )
                    {
                      string id = btn.id;
                      string itemInfo = "id";
                      if(id == null)
                      {
                        id = btn.id; itemInfo = "id";
                      } 
                      if(id == null)
                      {
                        id = btn.name; itemInfo = "name";
                      }
                      string defaultVal = btn.defaultValue; 
                      if(defaultVal != null)
                      { 
                        defaultVal = defaultVal.Trim(); 
                        if(defaultVal == "")
                        { 
                          defaultVal = id;
                        }
                      } 
                      frm.Items.Add(new ItemType(type,id,itemInfo,defaultVal));
                      continue;          
                      continue;
                    }
                  }
                } 
                catch(Exception /*ex_in*/)
                {
                  //MessageBox.Show(ex_in.ToString(),"Exception");
                };       
              }
            }
          } 
          catch(Exception /*eee*/)
          {
            //MessageBox.Show(eee.ToString());
          }
        } 
        if(frm.ShowTheDialog() == DialogResult.OK)
        { 
          addBtn(frm.myButtonEntry);
        };   
      }
       
      return;
    }
  4. 当点击任何其他按钮(xyz@yahoo)时,如果活动页面不是mail.yahoo.com,则会加载mail.yahoo.com。为所有控件设置值,并调用任何提交按钮操作以登录到特定网站。
    private void myToolBar_ButtonClick(object sender, 
                System.Windows.Forms.ToolBarButtonClickEventArgs e)
    {
      if(myToolBar.Buttons.IndexOf(e.Button) == 0)
      {
        onCustomize(null,null);
        return;
      }
    
      ButtonEntry btnEntry = (ButtonEntry)e.Button.Tag;
    
      onButtonClick(btnEntry);
    }
    
    protected void onButtonClick(ButtonEntry frm_in)
    {
      //Explorer.IWebBrowser_Document.
      HTMLDocumentClass doc = (HTMLDocumentClass)Explorer.Document;
    
      //check for correct site.
      if(frm_in.myURL != doc.url)
      {
        try
        {
          doc.url = frm_in.myURL;
        }
        catch(Exception /*e_in*/)
        {
          //MessageBox.Show(e_in.ToString(),"Exception Caught");
          return;
        }
        Explorer.DocumentComplete+= 
           new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(
           Explorer_DocumentComplete);
        myPendingReqest = frm_in;
        return;
      }
    
      //for each item in UserList fill the values on the active page.
      foreach(ItemType it in frm_in.myArrayLst)
      {
        if(it.Type == "text" || it.Type == "password" )
        {
          try
          {
            //get the item by id.
            HTMLInputElementClass ele = (
                HTMLInputElementClass)doc.getElementById(it.Name);
    
            ele.value = it.Val;
          }
          catch(Exception /*e_e*/)
          {
            //MessageBox.Show("getElementById failed." + e_e.ToString());
          }
        }
        else if(it.Type == "radio" || it.Type == "checkbox" )
        {
          try
          {
            //get the item by id.
            HTMLInputElementClass ele = 
                (HTMLInputElementClass)doc.getElementById(it.Name);
    
            ele.@checked = Convert.ToBoolean(it.Val);
          }
          catch(Exception /*e_e*/)
          {
            //MessageBox.Show("getElementById failed." + e_e.ToString());
          }
        }
        else if(it.Type=="submit" || it.Type == "button")
        {
          try
          {
            //get the item by id.
            HTMLInputElementClass ele = 
               (HTMLInputElementClass)doc.getElementById(it.Name);
            ele.click();
          }
          catch(Exception /*e_e*/)
          {
            //MessageBox.Show("getElementById failed." + e_e.ToString());
          }
        }
      }//end of foreach(string userReq in frm_in.UserListBox)
    }
    

Pavel Zolnikov文章展示了添加IE工具栏的重要部分。这篇文章增加了修改IE功能的功能。正如您所看到的,从客户端访问/修改HTML页面内容非常简单易行。

© . All rights reserved.