Inserts a Javascript into the Head of the document
#include <IE.au3>
_IEHeadInsertEventScript ( ByRef $oObject, $sHTMLFor, $sEvent, $sScript )
$oObject | Object variable of an InternetExplorer.Application, Window or Frame object |
$sHTMLFor | The HTML element for event monitoring (e.g. "document", "window" or an element ID) |
$sEvent | The event to monitor (e.g. "onclick" or "oncontextmenu") |
$sScript | Javascript string to be executed |
Success: | 1. |
Failure: | 0 and sets the @error flag to non-zero. |
@error: | 2 ($_IEStatus_COMError) - COM Error in Object reference 3 ($_IEStatus_InvalidDataType) - Invalid Data Type |
@extended: | Contains invalid parameter number |
Using ObjEvent(), AutoIt is able to be notified of events via COM, but it manages them asynchronously (rather than synchronously as they are handled in the browser context).
This routine allows you to inject code that is managed inside the browser context.
Note that elements that do not have an ID assigned can still be used by obtaining their "uniqueID" property with _IEPropertyGet()
_IEDocInsertHTML, _IEDocInsertText, _IEPropertyGet
; Open a browser with the basic example page, insert an
; event script into the head of the document that creates
; a JavaScript alert when someone clicks on the document
#include <IE.au3>
Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")
; Open a browser with the basic example page, insert an
; event script into the head of the document that creates
; a JavaScript alert when someone tries to right-click on the
; document and then the event script returns "false" to prevent
; the right-click context menu from appearing
#include <IE.au3>
Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu');return false")
; Open a browser with the basic example page, insert an
; event script into the head of the document that creates a
; JavaScript alert when we are about to navigate away from the
; page and presents the option to cancel the operation.
#include <IE.au3>
Local $oIE = _IE_Example("basic")
_IEHeadInsertEventScript($oIE, "window", "onbeforeunload", _
"alert('Example warning follows...');return 'Pending changes may be lost';")
_IENavigate($oIE, "www.autoitscript.com")
; Open a browser with the basic example page, insert an
; event script into the head of the document that prevents
; selection of text in the document
#include <IE.au3>
Local $oIE = _IE_Example()
_IEHeadInsertEventScript($oIE, "document", "ondrag", "return false;")
_IEHeadInsertEventScript($oIE, "document", "onselectstart", "return false;")
; Open a browser with the AutoIt homepage, insert an
; event script into the head of the document that prevents
; navigation when any link is clicked and log the URL of the
; clicked link to the console
#include <IE.au3>
Local $oIE = _IECreate("http://www.autoitscript.com")
Local $oLinks = _IELinkGetCollection($oIE)
For $oLink In $oLinks
Local $sLinkId = _IEPropertyGet($oLink, "uniqueid")
If @error Then Exit
_IEHeadInsertEventScript($oIE, $sLinkId, "onclick", "return false;")
If @error Then Exit
ObjEvent($oLink, "_Evt_")
Next
; idle as long as the browser window exists
While WinExists(_IEPropertyGet($oIE, "hwnd"))
Sleep(100)
WEnd
Func _Evt_onClick()
Local $o_Link = @COM_EventObj
ConsoleWrite($o_Link.href & @CRLF)
EndFunc ;==>_Evt_onClick