Jump to content

Inserting an Event Script into IE


DaleHohm
 Share

Recommended Posts

This will make it into IE.au3 eventually, but several people have had a need for this functionality recently so I wanted to post it now.

This will insert a Javascript into the HEAD section of an existing webpage (the in-memory page -- nothing written back to a file) and allows you to specify an HTML element, an event to monitor and the script to execute when the event is fired.

The impetus for this code is the recent revelation from SvenP that ObjEvent() does not process events synchronously with the event source, but rather queues them up and for processing in AutoIt and allows the source application to continue. This UDF allows you to process events in the IE context and handle them synchroneously [There is no provision yet for communication back to AutoIt from these event scripts, but I hope to work out code to create some sort of mechanism/convention].

Edit: For reference, here is what SvenP wrote about how ObjEvent processes events

It can only work on elements in a document. I have yet to ponder how this might be accomplished on the browser object. Syntax is likely to change when the next release of IE.au3 is made, so be prepared to modify your code is you use this version.

This is quite an in-depth subject, so please be prepared to spend time on MSDN examining the events and elements available in the DOM in order to make full use of this.

Dale

Examples:

#include <IE.au3>

$oIE = _IECreate()
_IENavigate($oIE, "http://www.google.com")

; Create an alert when the document is clicked
_IEInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")

; Prevent the right-click context menu
_IEInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu');return false;")

_IEInsertEventScript()

;===============================================================================
;
; Function Name:    _IEInsertEventScript()
; Description:      Inserts a Javascript 
; Parameter(s):     $o_object   - object variable for a InternetExplorer.Application, Window or Frame
;                   $s_htmlFor  - the HTML element for event monitoring (e.g. "document" or an element ID)
;                   $s_event    - the event to monitor (e.g. "onclick" or "oncontextmenu")
;                   $s_script   - Javascript to be executed
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - 1  and sets @ERROR = 0
;                  On Failure - 0  and sets @ERROR = 1
; Author(s):        Dale Hohm
;
;===============================================================================
;
Func _IEInsertEventScript($o_object, $s_htmlFor, $s_event, $s_script)
    If IsObj($o_object) Then
        SetError(0)
        Local $o_head = $o_object.document.all.tags("HEAD").Item(0)
        Local $o_script = $o_object.document.createElement("script")
        With $o_script
            .defer = True
            .language="jscript"
            .type = "text/javascript"
            .htmlFor = $s_htmlFor
            .event = $s_event
            .text = $s_script
        EndWith
        $o_head.appendChild($o_script)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc

Edit: fix typos

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

nice dale :lmao:

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

This will make it into IE.au3 eventually, but several people have had a need for this functionality recently so I wanted to post it now.

This will insert a Javascript into the HEAD section of an existing webpage (the in-memory page -- nothing written back to a file) and allows you to specify an HTML element, an event to monitor and the script to execute when the event is fired.

The impetus for this code is the recent revelation from SvenP that ObjEvent() does not process events synchronously with the event source, but rather queues them up and for processing in AutoIt and allows the source application to continue. This UDF allows you to process events in the IE context and handle them synchroneously [There is no provision yet for communication back to AutoIt from these event scripts, but I hope to work out code to create some sort of mechanism/convention].

It can only work on elements in a document. I have yet to ponder how this might be accomplished on the browser object. Syntax is likely to change when the next release of IE.au3 is made, so be prepared to modify your code is you use this version.

This is quite an in-depth subject, so please be prepared to spend time on MSDN examining the events and elements available in the DOM in order to make full use of this.

Dale

Examples:

#include <IE.au3>

$oIE = _IECreate()
_IENavigate($oIE, "http://www.google.com")

; Create an alert when the document is clicked
_IEInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")

; Prevent the right-click context menu
_IEInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu');return false;")

_IEInsertEventScript()

;===============================================================================
;
; Function Name:    _IEInsertEventScript()
; Description:      Inserts a Javascript 
; Parameter(s):     $o_object   - object variable for a InternetExplorer.Application, Window or Frame
;                   $s_htmlFor  - the HTML element for event monitoring (e.g. "document" or an element ID)
;                   $s_event    - the event to monitor (e.g. "onclick" or "oncontextmenu")
;                   $s_script   - Javascript to be executed
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - 1  and sets @ERROR = 0
;                  On Failure - 0  and sets @ERROR = 1
; Author(s):        Dale Hohm
;
;===============================================================================
;
Func _IEInsertEventScript($o_object, $s_htmlFor, $s_event, $s_script)
    If IsObj($o_object) Then
        SetError(0)
        Local $o_head = $o_object.document.all.tags("HEAD").Item(0)
        Local $o_script = $o_object.document.createElement("script")
        With $o_script
            .defer = True
            .language="jscript"
            .type = "text/javascript"
            .htmlFor = $s_htmlFor
            .event = $s_event
            .text = $s_script
        EndWith
        $o_head.appendChild($o_script)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc

Edit: fix typos

Thanks Dale, your work is awesome.

I had thought about writing javascript to the local document which solves the context menu problem, but didn't persue it because it did not answer trapping the newwindow event to open a new tab bather than a new browser.

The impetus for this code is the recent revelation from SvenP that ObjEvent() does not process events synchronously with the event source, but rather queues them up and for processing in AutoIt and allows the source application to continue.

Do you know if there are any plans in AutoIT to either change this behavior or allow selectively blocking the event in the source application from firing?

[There is no provision yet for communication back to AutoIt from these event scripts, but I hope to work out code to create some sort of mechanism/convention].

;) It would seem to me that in the new window case, javascript might not be the answer as it would have to trap the newwindow event, and communicate the link that was clicked on back to AutoIT where it could be written to a browser object in a new tab.

Also, when a link is clicked in the document, the browser loads a new page without the inserted javascript header, so all behavior mods are lost.

:lmao: Again, a very elegant solution, and a valiant effort, but I believe the answer really lies in the com event handling. I am going to post a link to this in the idea forum also.

Link to comment
Share on other sites

Thanks Dale, your work is awesome.

I had thought about writing javascript to the local document which solves the context menu problem, but didn't persue it because it did not answer trapping the newwindow event to open a new tab bather than a new browser.

Do you know if there are any plans in AutoIT to either change this behavior or allow selectively blocking the event in the source application from firing?

;) It would seem to me that in the new window case, javascript might not be the answer as it would have to trap the newwindow event, and communicate the link that was clicked on back to AutoIT where it could be written to a browser object in a new tab.

Also, when a link is clicked in the document, the browser loads a new page without the inserted javascript header, so all behavior mods are lost.

:lmao: Again, a very elegant solution, and a valiant effort, but I believe the answer really lies in the com event handling. I am going to post a link to this in the idea forum also.

Ah yes, referring back to your question here I see that I only addressed half of your problem.

Thinking about this a little more, I think the function above should work on the "window" as well as the document and its elements. This would give you access to events like Newwindow.

Edit: And I do want to ack that you are right about the custom scripting getting lost after changing pages or reloading... I thought that was obvious, but it bears getting pointed out clearly.

Regarding communication back to AutoIt, I'm thinking about using attributes on a <div> element to store values for AutoIt to retrieve and then using a custom event that AutoIt would register to watch and would know that there was new information in the <div> for it to retrieve... this information could contain information about the new URL from the aborted NewWindow2 that could be used to populate a new tab.

Regarding the change in the core AutoIt event handling -- Sven understood the desire and the need and implied he had started thinking about it, but that it was a pretty major design issue and is not likely to be seen anything really soon.

Dale

Edited by DaleHohm

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 2 months later...

why doesnt works when u copy the www.google.com page to your hard disk and run the script in the saved htm l?

thanks, and sorry x my stupid questions .

I love this Game :p----------------------Freeware Multilange support or Translate your scripts----------------------aNyBoDy KnOwS WhY A LiGhT iN My KeYbOaRd iS aLlWaIs BlInKiNg !?Who is "General Failure" and what is he doing in my hard disk !!!!!?

Link to comment
Share on other sites

why doesnt works when u copy the www.google.com page to your hard disk and run the script in the saved htm l?

thanks, and sorry x my stupid questions .

Can't think of any reason for it not to work. You'll need to provide more detail on what you tried.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 4 years later...

This will make it into IE.au3 eventually, but several people have had a need for this functionality recently so I wanted to post it now.

This will insert a Javascript into the HEAD section of an existing webpage (the in-memory page -- nothing written back to a file) and allows you to specify an HTML element, an event to monitor and the script to execute when the event is fired.

The impetus for this code is the recent revelation from SvenP that ObjEvent() does not process events synchronously with the event source, but rather queues them up and for processing in AutoIt and allows the source application to continue. This UDF allows you to process events in the IE context and handle them synchroneously [There is no provision yet for communication back to AutoIt from these event scripts, but I hope to work out code to create some sort of mechanism/convention].

Edit: For reference, here is what SvenP wrote about how ObjEvent processes events

It can only work on elements in a document. I have yet to ponder how this might be accomplished on the browser object. Syntax is likely to change when the next release of IE.au3 is made, so be prepared to modify your code is you use this version.

This is quite an in-depth subject, so please be prepared to spend time on MSDN examining the events and elements available in the DOM in order to make full use of this.

Dale

Examples:

#include <IE.au3>

$oIE = _IECreate()
_IENavigate($oIE, "http://www.google.com")

; Create an alert when the document is clicked
_IEInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")

; Prevent the right-click context menu
_IEInsertEventScript($oIE, "document", "oncontextmenu", "alert('No Context Menu');return false;")

_IEInsertEventScript()

;===============================================================================
;
; Function Name:    _IEInsertEventScript()
; Description:      Inserts a Javascript 
; Parameter(s):     $o_object   - object variable for a InternetExplorer.Application, Window or Frame
;                   $s_htmlFor  - the HTML element for event monitoring (e.g. "document" or an element ID)
;                   $s_event    - the event to monitor (e.g. "onclick" or "oncontextmenu")
;                   $s_script   - Javascript to be executed
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - 1  and sets @ERROR = 0
;                  On Failure - 0  and sets @ERROR = 1
; Author(s):        Dale Hohm
;
;===============================================================================
;
Func _IEInsertEventScript($o_object, $s_htmlFor, $s_event, $s_script)
    If IsObj($o_object) Then
        SetError(0)
        Local $o_head = $o_object.document.all.tags("HEAD").Item(0)
        Local $o_script = $o_object.document.createElement("script")
        With $o_script
            .defer = True
            .language="jscript"
            .type = "text/javascript"
            .htmlFor = $s_htmlFor
            .event = $s_event
            .text = $s_script
        EndWith
        $o_head.appendChild($o_script)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc

Edit: fix typos

Would this kind of thing work to handle JS alerts?

I'm having a real problem handling them in AutoIT, nothing I do seems to work in both identifying them and responding to them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...