ieCOM.ahk - IE Automation Library

Post your working scripts, libraries and tools
ameyrick
Posts: 119
Joined: 20 Apr 2014, 18:12

ieCOM.ahk - IE Automation Library

19 Jun 2016, 15:53

ieCOM.ahk

Code: Select all

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ieCOM.ahk 
; by ameyrick
; https://autohotkey.com/boards/viewtopic.php?f=6&t=19300
; Last updated: 2016 June 29 21:33 UTC
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*

functions:

		nav(), ie() 
			Same as wb()
			
		wb()
			Connect to an existing IE window by title
			Opens a new IE window if title not found
			Navigates to URL
			~options: invisible, silent
				visible and silent by default.
			
			Creates 'wb' pointer to IE window
				wb.document.getElementByID("ID") 
				
			Automatically calls DocWait() on completion
		
		Example:
			wb("about:blank")
			wb("http://www.google.co.uk","title",true,true)
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		wait(), DocWait()
			Pause script until web page has finished loading
			Automatically invoked after wb()
			Automatically calls setvars() on completion
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		setvars()
			Creates shortcut references to IE
			Call this function, to update the script variables after 
			new browser content. Automatically invoked after DocWait()
			
			document := wb.document
			d:= wb.document
			w:= wb.document.parentwindow
			
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		rs()
		resizeIE()
			Resize &/or move IE window
			
		Example:
			resizeIE(x,y,width,height,PointerToIE)

		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		jsapp()
		jsappend()
			Append your own Javascript functions to the current page
		
		Example:
			https://autohotkey.com/boards/viewtopic.php?f=6&t=19300
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		js()
			Execute Javascript, call a function on the current page.
		
		Example:
			https://autohotkey.com/boards/viewtopic.php?f=6&t=19300
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		IEGet2()
			Modified version of original IEGet() by Jethrow
			Retrieves pointer to existing IE window/tab
			with or without a window/tab title specified
			
		Example:
			wb := IEGet2("page title")
			
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		frames(0)
		frames("name")
		        Create an obj reference to a specific frame or iframe
		frames()
			Create an array of obj references for each frame in the document
			
		Example:
			MyFrame := frames(1)
			MyFrame := frames("name")
			Frame := frames()
			Frame[0].document
			Frame[1].document
			
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		createTable()
			Creates a table element on the current page
			'parent_elem' is the element you wish to append to
			'idValue' is your Table id string
			'r' the amount of rows you would like in your table
			'c' the amount of Columns you would like in your table
			
		Example:
			createTable(wb.document.body,"MyTableID",10,10)
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		table2array()
			stores each <td></td> element in a multi-dimensional array/obj
			
		Example:
			myarray := table2array("MyTableID")
			
			myarray[0,0].InnerHTML := "#"
			Loop,
			{
				myarray[0,(A_Index-1)].bgcolor := "#D3D3D3"
				myarray[(A_Index-1),0].bgcolor := "#D3D3D3"
			}	
						
			myarray[0,1].InnerHTML := "A"
			myarray[0,2].InnerHTML := "B"
			myarray[0,3].InnerHTML := "C"
			myarray[0,4].InnerHTML := "D"
			myarray[0,5].InnerHTML := "E"
			myarray[0,6].InnerHTML := "F"
			myarray[0,7].InnerHTML := "G"
			
			myarray[1,0].InnerHTML := "1"
			myarray[2,0].InnerHTML := "2"
			myarray[3,0].InnerHTML := "3"
			myarray[4,0].InnerHTML := "4"
			myarray[5,0].InnerHTML := "5"
			myarray[6,0].InnerHTML := "6"
			myarray[7,0].InnerHTML := "7"
			
			
		
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


*/ 
nav(url:="",window_title:="",v:="",s:=""){
		wb(url,window_title,v,s)
	return
}
 
ie(url:="",window_title:="",v:="",s:=""){
	wb(url,window_title,v,s)
	return
}
 
wb(url:="",window_title:="",v:="",s:=""){
	global	
	if !wb
		wb := IEGet2(window_title)
	if !wb 
		wb := ComObjCreate("InternetExplorer.Application")
	if !v
		wb.Visible := true
	if v = true
		wb.Visible := true
	if v = false
		wb.Visible := false
 
	if !s
		wb.Silent := true
	if s = true
		wb.Silent := true
	if s = false
		wb.Silent := false
 
	if url
		wb.Navigate(url)
 
	DocWait(wb)
	return
}
 
wait(x=""){
	global
	if x
		wb := x
	DocWait(wb)
	return
}
 
DocWait(x:=""){
	global
	if x 
		wb := x
	try while wb.readyState != 4 or wb.busy
           Sleep, 10
	Sleep, 100
	setvars(wb)
	return
}
 
setvars(x=""){
	global
	if x
		wb := x
	document := wb.document
	d:= wb.document
	w:= wb.document.parentwindow
	return
}
 
rs(x:=0,y:=0,width:="",height:="",browser:=""){
	global
	if !browser
		browser := wb
	resizeIE(x,y,width,height,browser)
	return
}
 
resizeIE(x:=0,y:=0,width:="",height:="",browser:=""){
	global
	if !browser
		browser := wb
	if !width
		width:= A_ScreenWidth
	if !height
		height:= A_ScreenHeight
	try browser.document.parentWindow.moveTo(x,y)
	try browser.document.parentWindow.resizeTo(width,height)
	return
}
 
jsapp(code=""){
	jsappend(code)
}
 
jsappend(code=""){
	global
	if !code
		return
	s := d.createElement("script")
	s.type := "text/javascript"
	try {
	  s.appendChild(d.createTextNode(code))
	  d.body.appendChild(s)
	} catch e {
	  s.text := code
	  d.body.appendChild(s)
	}
}
 
js(js:=""){
	global
	if !js
		return
	try w.execScript(js)
	catch {
		DocWait()
		try w.execScript(js)
	}
	return
}

IEGet2(Name="")  ;Retrieve pointer to existing IE window/tab
{
IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
	Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
	: RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
For wb in ComObjCreate( "Shell.Application" ).Windows
	If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
		Return wb
	; Below added for when no tab name/page title provided ~ ameyrick
	If InStr( wb.FullName, "iexplore.exe" ) 
		Return wb
} ;Original IEGet() written by Jethrow
 
table2array(id="") {
	Global
	if !id
		return
	tObj:= Object()
	loop % d.getElementById(id).rows.length
	{
		row:= (A_Index-1) 
		loop % d.getElementById(id).rows[row].cells.length
		{
			col := (A_Index-1)
			tObj[row, col] := d.getElementById(id).rows[row].cells[col]
		}
	}
	return tObj
}

createTable(parent_elem="",idValue="",r=1,c=1){
	Global
	if !parent_elem
		return
	if !idValue
		return
	t := document.createElement("TABLE")
	t.setAttribute("id", idValue)
	parent_elem.appendChild(t)
	loop % r
	{
		row := (A_Index-1)
		tr := d.createElement("TR")
		tr.setAttribute("id", idValue "_tr" row )
		t.appendChild(tr)
		loop % c
		{
			td := document.createElement("TD")
			td.setAttribute("id", idValue "_tr" row "_td" (A_Index-1) )
			td.setAttribute("class", idValue )
			
			cell := document.createTextNode(" ")
			td.appendChild(cell)
			tr.appendChild(td)
		}
	}
	DocWait(wb)
	setvars(wb)
}

frames(name=""){
	global wb
	frames := wb.document.parentwindow.frames
	fObj := Object()
	loop, % frames.length
	{
		If !IsObject(fObj[A_Index-1] := frames[A_Index-1])
			try fObj[A_Index-1] := ComObj(9, ComObjQuery(frames[A_Index-1], "{332C4427-26CB-11D0-B483-00C04FD90119}", "{332C4427-26CB-11D0-B483-00C04FD90119}"), 1)
	}
	if !name
		return fObj
	if name is Integer
		return fObj[name]
	else
		loop, % frames.length
		{
			if ( fObj[A_Index-1].name == name ){
				return fObj[A_Index-1]
			}
		}
}

/*  Fix keyboard shortcuts in WebBrowser control.
 *  References:
 *    http://www.autohotkey.com/community/viewtopic.php?p=186254#p186254
 *    http://msdn.microsoft.com/en-us/library/ms693360
 */

gui_KeyDown(wParam, lParam, nMsg, hWnd) {
    global wb
    if (Chr(wParam) ~= "[A-Z]" || wParam = 0x74) ; Disable Ctrl+O/L/F/N and F5.
        return
    pipa := ComObjQuery(wb, "{00000117-0000-0000-C000-000000000046}")
    VarSetCapacity(kMsg, 48), NumPut(A_GuiY, NumPut(A_GuiX
    , NumPut(A_EventInfo, NumPut(lParam, NumPut(wParam
    , NumPut(nMsg, NumPut(hWnd, kMsg)))), "uint"), "int"), "int")
    Loop 2
    r := DllCall(NumGet(NumGet(1*pipa)+5*A_PtrSize), "ptr", pipa, "ptr", &kMsg)
    ; Loop to work around an odd tabbing issue (it's as if there
    ; is a non-existent element at the end of the tab order).
    until wParam != 9 || wb.Document.activeElement != ""
    ObjRelease(pipa)
    if r = 0 ; S_OK: the message was translated to an accelerator.
        return 0
}

/* 	Fix keyboard shortcuts in WebBrowser control.
*	by coco 
*	https://autohotkey.com/boards/viewtopic.php?f=5&t=5487&p=31581#p31581
*/
WB_OnKeyPress(wParam, lParam, nMsg, hWnd)
{
   WinGetClass WinClass, ahk_id %hWnd%
   if (WinClass == "Internet Explorer_Server")
   {
      static riid_IDispatch
      if !VarSetCapacity(riid_IDispatch)
      {
         VarSetCapacity(riid_IDispatch, 16)
         DllCall("ole32\CLSIDFromString", "WStr", "{00020400-0000-0000-C000-000000000046}", "Ptr", &riid_IDispatch)
      }
      DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", 0xFFFFFFFC, "Ptr", &riid_IDispatch, "Ptr*", pacc) ; OBJID_CLIENT:=0xFFFFFFFC
 
      static IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}"
      pwin := ComObjQuery(pacc, IID_IHTMLWindow2, IID_IHTMLWindow2)
         ObjRelease(pacc)
 
      static IID_IWebBrowserApp := "{0002DF05-0000-0000-C000-000000000046}"
           , SID_SWebBrowserApp := IID_IWebBrowserApp
      pweb := ComObjQuery(pwin, SID_SWebBrowserApp, IID_IWebBrowserApp)
         ObjRelease(pwin)
      wb := ComObject(9, pweb, 1)
 
      static IID_IOleInPlaceActiveObject := "{00000117-0000-0000-C000-000000000046}"
      pIOIPAO := ComObjQuery(wb, IID_IOleInPlaceActiveObject)
 
      VarSetCapacity(MSG, 48, 0)                      ; http://goo.gl/GX6GNm
      , NumPut(A_GuiY                                 ; POINT.y
      , NumPut(A_GuiX                                 ; POINT.x
      , NumPut(A_EventInfo                            ; time
      , NumPut(lParam                                 ; lParam
      , NumPut(wParam                                 ; wParam
      , NumPut(nMsg                                   ; message
      , NumPut(hWnd, MSG)))), "UInt"), "Int"), "Int") ; hwnd
 
      TranslateAccelerator := NumGet(NumGet(pIOIPAO + 0) + 5*A_PtrSize)
      Loop 2
         r := DllCall(TranslateAccelerator, "Ptr", pIOIPAO, "Ptr", &MSG)
      until (wParam != 9 || wb.Document.activeElement != "")
      ObjRelease(pIOIPAO)
      if (r == 0)
         return 0
   }
}

/*  javascript:AHK('Func') --> Func()
 */

JS_AHK(func, prms*) {
    global wb
    ; Stop navigation prior to calling the function, in case it uses Exit.
    wb.Stop(),  %func%(prms*)
}



Usage:

Code: Select all

#NoEnv
#SingleInstance force
#persistent
#Include ieCOM.ahk ;https://autohotkey.com/boards/viewtopic.php?f=6&t=19300

script= 
(
	function MoveLogo() {
		var elem = document.getElementById("ahk_logo"); 
		var pos = 0;
		var id = setInterval(moveright, 5);
		
		function moveright() {
			if (pos == 350) {
				clearInterval(id);
				id = setInterval(moveleft, 5);
			} else {
				pos++; 
				elem.style.left = pos + 'px'; 
			}
		}
		function moveleft() {
			if (pos == -350) {
				clearInterval(id);
				id = setInterval(moveright, 5);
			} else {
				pos--; 
				elem.style.left = pos + 'px'; 
			}
		}
	}
)

nav("http://autohotkey.com/boards/")
rs()
logo := d.getElementById("logo").getelementsbytagname("img")[0]
logo.id := "ahk_logo"
logo.style.position := "relative"
jsappend(script)
js("MoveLogo();")

DocWait()

;Spreadsheet Table settings
MyTableID:="MyTableID"
rows := 31
cols := 27
Height := 300 

gosub, css

;create <div id='ahk'>
div := d.createElement("div")
div.setAttribute("id", "ahk")
div.setAttribute("style","height:" Height "px;overflow:scroll;")
d.queryselectorall(".navbar.nav-breadcrumbs")[0].appendChild(div)

;Create a html table element child of <div id='ahk'>
;set the id as "MyTableID"
createTable(d.getElementByID("ahk"),MyTableID,rows,cols)


;store each <td> element in an array
myarray := table2array(MyTableID)

;change the InnerHTML of our first <td> element
myarray[0,0].InnerHTML := "#"
myarray[0,0].SetAttribute("class", "head")

;array of column headers
headers := ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

;loop rows
loop, % rows
{
	;A_Index starts from a value of 1
	;write our row numbers
	myarray[A_Index,0].InnerHTML := A_Index
	myarray[A_Index,0].SetAttribute("class", "head")
	
	;store our current row
	row := A_Index
	
	;loop  columns
	loop, % cols
	{
		if (row == 1) {
			;write our column headers
			myarray[0,A_Index].InnerHTML := headers[A_Index]
			myarray[0,A_Index].SetAttribute("class", "head")
		}
		
		;store the current cell reference 
		cell := headers[A_Index] . row
				
		;write an input box with an id of the cell reference
		myarray[row, A_Index].InnerHTML:= "<input id='" cell "' value='' size='5' >"
		
		;Create script cell refrence variables
		Cell_%cell% := myarray[row, A_Index].FirstChild
		
	}
}

;check the browser is ready & update the script variables
DocWait()

;++++++++++++++++++++++++++++++++

;write new values to cell ref
Cell_A1.Value := 5
Cell_B1.Value := 6

;math
Cell_C1.Value := Cell_A1.Value * Cell_B1.Value

;++++++++++++++++++++++++++++++++

;Create cell references by html id's 
B2 := d.getElementByID("B2")
C2 := d.getElementByID("C2")
D2 := d.getElementByID("D2")

;write new values to cell ref
B2.Value := 5
C2.Value := 6

;math
D2.Value := B2.Value * C2.Value

;++++++++++++++++++++++++++++++++

;Create cell references by array
B4 := myarray[4,2].FirstChild
C4 := myarray[4,3].FirstChild
D4 := myarray[4,4].FirstChild

;write new values to cell ref
B4.Value := 4
C4.Value := 9

;math
D4.Value := B4.Value * C4.Value

return



css:
;css table style
css=
(
	
	#%MyTableID% {border: 1px solid black; border-collapse: collapse;}
	
	#%MyTableID% tr:nth-child(even) {background-color: #f2f2f2}
	
	#%MyTableID% td.head {border: 1px solid black; background-color: #f1f1f1;}
	#%MyTableID% td.head:hover {border: 1px solid black; background-color: #f1f1f1;}
	
	#%MyTableID% td {border: 1px solid black; background-color: #ffffff;}
	#%MyTableID% td:hover {border: 1px solid red; background-color: lightblue;}
	
	#%MyTableID% input {background: transparent;border: none;}
)
;apply css rules
s := d.createElement("style")
s.appendChild(d.createTextNode(css))
d.head.appendChild(s)
return


GuiClose:
quit:
ESC::
try wb.quit()
ExitApp
return

`::Reload
Last edited by ameyrick on 07 Jul 2016, 20:41, edited 17 times in total.
User avatar
Xeo786
Posts: 193
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: ieCOM.ahk - IE Automation Library

20 Jun 2016, 00:44

Great thank you so much... can you please include some shortcuts to IE tables. :salute:
"When there is no gravity, there is absolute vacuum" -Game changer theory
ameyrick
Posts: 119
Joined: 20 Apr 2014, 18:12

Re: ieCOM.ahk - IE Automation Library

22 Jun 2016, 17:07

Hi Xeo786, No problem! I have added two table fuctions:

createTable(parent_elem,idValue,row,col)
table2array(idValue)

First post updated with code and example :)

another example:

Code: Select all

#NoEnv
#SingleInstance Force
#persistent
#Include ieCOM.ahk ;https://autohotkey.com/boards/viewtopic.php?f=6&t=19300
 
nav("about:Blank")
 
parent := d.body
createTable(parent,"myTableId",8,8)
 
myTableArray := table2array("myTableId")
 
d.getelementbyid("myTableId").border := "3"
 
myTableArray[0,0].InnerHTML := "Hi"
myTableArray[1,1].InnerHTML := "Hi Again!"
myTableArray[2,2].InnerHTML := "This is"
myTableArray[3,3].InnerHTML := "a"
myTableArray[4,4].InnerHTML := "table"
myTableArray[4,6].InnerHTML := "in"
myTableArray[5,6].InnerHTML := "an"
myTableArray[6,7].InnerHTML := "array!"
myTableArray[7,0].InnerHTML := "<center>6</center>"
myTableArray[7,1].InnerHTML := "<center>+</center>"
myTableArray[7,2].InnerHTML := "<center>14</center>"
myTableArray[7,3].InnerHTML := "<center>=</center>"


 
msgbox, % myTableArray[0,0].InnerText 
msgbox, % myTableArray[1,1].InnerText "`n" myTableArray[2,2].InnerText " " myTableArray[3,3].InnerText " " myTableArray[4,4].InnerText " " myTableArray[4,6].InnerText " " myTableArray[5,6].InnerText " " myTableArray[6,7].InnerText
 

myTableArray[7,4].InnerHTML := "<center>" ( myTableArray[7,0].InnerText + myTableArray[7,2].InnerText ) "</center>"

myTableArray[7,4].bgcolor := "LightGreen"

 
return
 
ESC::
quit:
try wb.quit()
ExitApp
return
 
`::Reload
Last edited by ameyrick on 26 Jun 2016, 19:36, edited 5 times in total.
ameyrick
Posts: 119
Joined: 20 Apr 2014, 18:12

Re: ieCOM.ahk - IE Automation Library

23 Jun 2016, 01:31

Mock spreadsheet table:

Code: Select all

#NoEnv
#SingleInstance force
#persistent
#Include ieCOM.ahk  ;https://autohotkey.com/boards/viewtopic.php?f=6&t=19300

;Spreadsheet Table settings
MyTableID:="MyTableID"
rows := 51
cols := 51

;GUI settings
Width := 800
Height := 600 

Gui Add, ActiveX, x0 y0 w%Width% h%Height% vwb, about:<!DOCTYPE HTML><meta http-equiv="X-UA-Compatible" content="IE=Edge">
Gui Show, x-5 y-5 w%Width% h%Height% Center, ieCOM.ahk Mock Spreadsheet - by ameyrick

nav("about:blank")

;css table style
css=
(
	
	#%MyTableID% {border: 1px solid black; border-collapse: collapse;}
	
	#%MyTableID% tr:nth-child(even) {background-color: #f2f2f2}
	
	#%MyTableID% td.head {border: 1px solid black; background-color: #f1f1f1;}
	#%MyTableID% td.head:hover {border: 1px solid black; background-color: #f1f1f1;}
	
	#%MyTableID% td {border: 1px solid black; background-color: #ffffff;}
	#%MyTableID% td:hover {border: 1px solid red; background-color: lightblue;}
	
	#%MyTableID% input {background: transparent;border: none;}
)


;apply css rules
s := d.createElement("style")
s.appendChild(d.createTextNode(css))
d.head.appendChild(s)

;create <div id='ahk'>
div := d.createElement("div")
div.setAttribute("id", "ahk")
div.setAttribute("style","height:" (Height-10) "px;overflow:scroll;")
d.body.appendChild(div)

;Create a html table element child of <div id='ahk'>
;set the id as "MyTableID"
createTable(d.getElementByID("ahk"),MyTableID,rows,cols)


;store each <td> element in an array
myarray := table2array(MyTableID)

;change the InnerHTML of our first <td> element
myarray[0,0].InnerHTML := "#"
myarray[0,0].SetAttribute("class", "head")

;array of column headers
headers := ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ","BA","BB","BC","BD","BE","BF","BG","BH","BI","BJ","BK","BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX","BY","BZ","CA","CB","CC","CD","CE","CF","CG","CH","CI","CJ","CK","CL","CM","CN","CO","CP","CQ","CR","CS","CT","CU","CV","CW","CX","CY","CZ"]

;loop rows
loop, % rows
{
	;A_Index starts from a value of 1
	;write our row numbers
	myarray[A_Index,0].InnerHTML := A_Index
	myarray[A_Index,0].SetAttribute("class", "head")
	
	;store our current row
	row := A_Index
	
	;loop  columns
	loop, % cols
	{
		if (row == 1) {
			;write our column headers
			myarray[0,A_Index].InnerHTML := headers[A_Index]
			myarray[0,A_Index].SetAttribute("class", "head")
		}
		
		;store the current cell reference 
		cell := headers[A_Index] . row
				
		;write an input box with an id of the cell reference
		myarray[row, A_Index].InnerHTML:= "<input id='" cell "' value='' size='5' >"
		
		;Create script cell refrence variables
		Cell_%cell% := myarray[row, A_Index].FirstChild
		
	}
}

;check the browser is ready & update the script variables
DocWait()

;++++++++++++++++++++++++++++++++

;write new values to cell ref
Cell_A1.Value := 5
Cell_B1.Value := 6

;math
Cell_C1.Value := Cell_A1.Value * Cell_B1.Value

;++++++++++++++++++++++++++++++++

;Create cell references by html id's 
B2 := d.getElementByID("B2")
C2 := d.getElementByID("C2")
D2 := d.getElementByID("D2")

;write new values to cell ref
B2.Value := 5
C2.Value := 6

;math
D2.Value := B2.Value * C2.Value

;++++++++++++++++++++++++++++++++

;Create cell references by array
B4 := myarray[4,2].FirstChild
C4 := myarray[4,3].FirstChild
D4 := myarray[4,4].FirstChild

;write new values to cell ref
B4.Value := 4
C4.Value := 9

;math
D4.Value := B4.Value * C4.Value

return

GuiClose:
quit:
ESC::
try wb.quit()
ExitApp
return

`::Reload
Last edited by ameyrick on 26 Jun 2016, 19:36, edited 3 times in total.
ameyrick
Posts: 119
Joined: 20 Apr 2014, 18:12

Re: ieCOM.ahk - IE Automation Library

24 Jun 2016, 18:23

Javascript Game:

Code: Select all

#NoEnv
#SingleInstance force
#persistent
#Include ieCOM.ahk ;https://autohotkey.com/boards/viewtopic.php?f=6&t=19300
OnExit, quit

;GUI settings
;game_gui := 0 ;play game in ie browser window.
game_gui := 1 ;play game in a gui window with embeded activex control
Width := 500
Height := 290

gosub, css
gosub, Javascript

if game_gui {
    ;WB_OnKeyPress - by coco https://autohotkey.com/boards/viewtopic.php?f=5&t=5487&p=31581#p31581
    OnMessage(0x100, "WB_OnKeyPress", 2) ; WM_KEYDOWN
    OnMessage(0x101, "WB_OnKeyPress", 2) ; WM_KEYUP
    
    Gui Add, ActiveX, x0 y0 w%Width% h%Height% vwb, about:<!DOCTYPE HTML><meta http-equiv="X-UA-Compatible" content="IE=Edge">
    Gui Show, x-5 y-5 w%Width% h%Height% Center, ieCOM.ahk Javascript Game Example - by ameyrick

}

nav("about:blank")
d.body.style.overflow := "hidden"
s := d.createElement("style")
s.appendChild(d.createTextNode(css))
d.head.appendChild(s)

jsappend(script)

p := d.createElement("p")
p.appendChild(d.createTextNode("Make sure the gamearea has focus, and use the arrow keys to move the red square around."))
d.body.appendChild(p)

js("startGame();")

if !game_gui
    d.body.FirstChild.click()
else {
        winwait:
        WinWaitActive, ieCOM.ahk
        ControlClick, Internet Explorer_Server1, ieCOM.ahk
        WinWaitNotActive, ieCOM.ahk
        goto, winwait
}
return

Javascript:
;Javascript Source:
;	http://www.w3schools.com/games/default.asp
script =
(
var myGamePiece;

function startGame() {
    myGamePiece = new component(30, 30, "red", 225, 225);
    myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.frameNo = 0;
        this.interval = setInterval(updateGameArea, 5);
        window.addEventListener('keydown', function (e) {
            e.preventDefault();
            myGameArea.keys = (myGameArea.keys || []);
            myGameArea.keys[e.keyCode] = (e.type == "keydown");
        })
        window.addEventListener('keyup', function (e) {
            myGameArea.keys[e.keyCode] = (e.type == "keydown");
        })
    },
    stop : function() {
        clearInterval(this.interval);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y, type) {

    this.type = type;
    this.width = width;
    this.height = height;
    this.speed = 0;
    this.angle = 0;
    this.moveAngle = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
        ctx = myGameArea.context;
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.angle);
        ctx.fillStyle = color;
        ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
        ctx.restore();
    }
    this.newPos = function() {
        this.angle += this.moveAngle * Math.PI / 180;
        this.x += this.speed * Math.sin(this.angle);
        this.y -= this.speed * Math.cos(this.angle);
    }
}

function updateGameArea() {
    //alert("updateGameArea() called")
    myGameArea.clear();
    myGamePiece.moveAngle = 0;
    myGamePiece.speed = 0;
    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.moveAngle = -5; }
    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.moveAngle = 5; }
    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speed= 5; }
    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speed= -5; }
    myGamePiece.newPos();
    myGamePiece.update();
}
)
return

css:
css=
(
	canvas {
		border:1px solid #d3d3d3;
		background-color: #f1f1f1;
	}
)
return


ESC::
quit:
try wb.quit()
ExitApp
return

`::Reload
Last edited by ameyrick on 28 Jun 2016, 15:32, edited 1 time in total.
User avatar
Xeo786
Posts: 193
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: ieCOM.ahk - IE Automation Library

28 Jun 2016, 02:54

:thumbup: Thank you for tables :dance:
"When there is no gravity, there is absolute vacuum" -Game changer theory
User avatar
adegard
Posts: 77
Joined: 24 Nov 2017, 05:58
GitHub: adegard

Re: ieCOM.ahk - IE Automation Library

21 Nov 2018, 06:48

This topic is old... I create also functions to simplify IE automantion: https://autohotkey.com/boards/viewtopic ... 27#p249027
olfen
Posts: 1
Joined: 05 Oct 2013, 04:49
Location: Stuttgart, Germany
Contact:

Re: ieCOM.ahk - IE Automation Library

21 Nov 2018, 08:11

Great work! Really useful!
burque505
Posts: 1005
Joined: 22 Jan 2017, 19:37

Re: ieCOM.ahk - IE Automation Library

21 Nov 2018, 09:44

Thank you, adegard, really nice.
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: MGGowtham, Odlanir, Rafio, robodesign and 25 guests