handle IE's win32ole(ruby,存根)

jeremy.chen 发表于 2008-09-16 12:06:41

//**********************************************HTMLClass.rb*****************************************

if (#CONTENT# != __FILE__)
  curpath = File.expand_path(File.dirname(__FILE__))
  if ($LOAD_PATH.include?(curpath) == false)
    $LOAD_PATH << curpath
  end
end
require 'win32ole'

class DOMObject
  def name
    @name
  end
 
  def id
    @id
  end
 
  def initialize(_name, _id)
    @name = _name
    @id = _id
  end
end

class IE
  READYSTATE_COMPLETE = 4
  FAST_SLEEP_TIME = 0.01
  LOW_SLEEP_TIME = 0.1
 
  attr_reader :m_down_load_time
  attr_reader :m_ie
 
  def initialize()
    @m_ie = WIN32OLE.new('InternetExplorer.Application')
    @m_url_list = []
    @m_defaultSleepTime = FAST_SLEEP_TIME
  end
 
  def goto( _url )
    @m_ie.navigate( _url )
    wait()
    sleep 0.2
    return @m_down_load_time
  end
 
  def setHtml( _htmlfile )
    #~ _htmlfile = Func.getFullpath(_htmlfile)
    goto( _htmlfile )
  end
 
  def close
    @m_ie.quit
  end
 
  def document
    return @m_ie.document
  end
 
  def activeElement
    return @m_ie.document.activeElement
  end
 
  def focus
    @m_ie.document.activeElement.blur
    @m_ie.document.focus
  end
 
  def rect( _width, _height, _fullscreen=false )
    @m_ie.menubar = false
    @m_ie.addressbar = false
    @m_ie.toolbar = false
    @m_ie.statusbar = false
    @m_ie.resizable = true
    @m_ie.fullscreen = _fullscreen
    @m_ie.width = _width
    @m_ie.height = _height
    @m_ie.left = (@m_ie.document.parentwindow.screen.availwidth-@m_ie.width)/2
    @m_ie.top = (@m_ie.document.parentwindow.screen.availheight-@m_ie.height)/2
  end
 
  def url
    return @m_ie.LocationURL
  end
 
  def html
    return @m_ie.document.body.outerHTML
  end
 
  def text
    return @m_ie.document.body.innerText.strip
  end
 
  def wait( _noSleep=false )
    begin
      @m_down_load_time = 0
      pageLoadStart = Time.now
      @m_pageHasReloaded = false
     
      until @m_ie.readyState == READYSTATE_COMPLETE
        @m_pageHasReloaded = true
        sleep 0.02
      end
      sleep 0.02
     
      until @m_ie.document.readyState == "complete"
        sleep 0.02
      end
     
      if @m_ie.document.frames.length > 1
        begin
          0.upto @m_ie.document.frames.length-1 do |i|
            until @m_ie.document.frames[i.to_s].document.readyState == 'complete'
                sleep 0.02
            end
            @m_url_list << @m_ie.document.frames[i.to_s].document.url unless @m_url_list.include?(@m_ie.document.frames[i.to_s].document.url)
          end
        rescue=>e
            puts 'frame error in wait'   + e.to_s + "\n" + e.backtrace.join("\n")
        end
      else
          @m_url_list << @m_ie.document.url unless @m_url_list.include?(@m_ie.document.url)
      end
      @m_down_load_time = Time.now - pageLoadStart
    rescue WIN32OLERuntimeError => e
      puts "runtime error in wait: #{e}\n#{e.backtrace.join("\\n")}"
    end
    sleep 0.01
    sleep @m_defaultSleepTime unless _noSleep == true
  end

  def stopMsgLoop
    puts 'Now Stop IE...'
    @m_ie_loop = false
  end

  def show
    @m_ie.visible = true
   
    ev = WIN32OLE_EVENT.new( @m_ie, 'DWebBrowserEvents' )
    ev.on_event('Quit') {|*args| stopMsgLoop}
    @m_ie_loop = true
    while (@m_ie_loop)
      sleep 0.005
      WIN32OLE_EVENT.message_loop
    end
  end
 
  def msgBox( _message )
    @m_ie.document.parentwindow.alert( _message )
    #bConfirmed = @m_ie.document.parentwindow.confirm( _message )
    #vTextData = @m_ie.document.parentwindow.prompt( _message, _defaultValue )
  end
 
  def getElementById( _id )
    return @m_ie.document.getElementById( _id )
  end
 
  #~ def getAllIDObjects()
    #~ @m_ie.document.all.each do |n|
      #~ id = n.invoke( 'id' )
      #~ if ( id.to_s!='' )
        #~ tag = n.invoke( 'tagname' )
        #~ if ( tag=='INPUT' )
          #~ t = n.invoke( 'type' ).upcase
          #~ tag = tag + ' ' + t
        #~ end
        #~ s = tag + ': id='+ id
        #~ @m_DOMlist.add( NElement.new(tag, id) ) #( DOMObject.new(tag, id) )
        #~ puts s
      #~ end
    #~ end
  #~ end

  def newObject( _type, _id, _value, _parent=nil )
    case _type
      when 'Button'
        type_text = "<INPUT TYPE='button'>"
      when 'Text'
        type_text = "<INPUT TYPE='text'>"
      when 'A'
        type_text = "A"
      else
        type_text = _type
    end
    obj = @m_ie.document.createElement( "#{type_text}" )
    obj.setAttribute( "id", "#{_id}" )
    obj.setAttribute( "value", "#{_value}" )
    if _parent == nil
      @m_ie.document.body.insertBefore( obj )
    else
      @m_ie.document.getElementById("#{_parent}").appendChild( obj )
    end
  end
 
  def deleteTr(_table_id, _tr_id)
    tr = @m_ie.document.getElementById(_table_id).childNodes(0)
    tr.removeChild(tr.childNodes(_tr_id))
  end

  def deleteTbody(_tbody)
    if (_tbody.hasChildNodes())
      begin
        (_tbody.childNodes.length-1).downto 0 do |i|
          _tbody.removeChild(_tbody.childNodes(i))
        end
      rescue
        p 'You have already removed the body element.'
      end
    end
  end

end

 //**********************************************test.html*****************************************

<body>
<div>input URL:<input type="text" id="url_location" style='width:80%' value='http://cache.tianya.cn/publicforum/ArticlesList/0/poem.shtml'>
<!--<br><INPUT type=radio name="type" id='type_forum' CHECKED>Forum
<INPUT type=radio name="type" id='type_thread'>Thread
<input  id='url_button'   style="width:50;visibility:hidden" type="button" value="ok" >
</br>-->
<input  id='filter_button'   style="width:50" type="button" value="filter">
</div>

<!--<IFRAME id='frame' FRAMEBORDER=1 style='width:99%;height:70%' ></IFRAME>-->

<div id='main'>
</div>

<div id='result'></div>

</body>

//***********************************************test.rb***************************************

#~ require 'net/http'
#~ require 'open-uri'
#~ require 'hpricot'
#require 'simpleTianyaParser.rb'
require 'HTMLClass.rb'


def handle_filter
   
    url = $window.document.getElementById('url_location').value
    p url
    tp = SimpleTianyaParser.new
   
   
    p tp.checkType?(url)
    begin
      type,result = tp.parseHTML(url)
    rescue Exception => e  
      p e
      result = e
      type = 'exception'
    end
    if type=='forum'
      s = '<p>'
      s += result
      s += '</p>'
       $window.document.getElementById('result').innerHTML += "#{s}<br>----------------<br>"  
    elsif type=='thread'
      s ='<p>'
      s += result
      s +='</p>'
      $window.document.getElementById('result').innerHTML += "<br>#{s}<br>----------------<br>"           
    elsif type=='exception'
      $window.document.getElementById('result').innerHTML += "Exception Occur:<br>#{result}<br>"                         
    else #not supported
      $window.document.getElementById('result').innerHTML += "The following URL is not supported:<br>#{url}<br>"                   
    end
  
end

htmlroot = File.expand_path(File.dirname(__FILE__))
htmlfile = 'test.html'

$window = IE.new
$window.setHtml(htmlroot+'/'+htmlfile  )
$window.rect( 800, 600 )

$window.document.getElementById('filter_button').onclick = method(:handle_filter)

$window.show()

 

 
关键词(Tag): dom

最新评论

发表评论

*昵称

已经注册过? 请登录

Email
网址
*评论