Jump to content

[SOLVED] How to inject some Javascript into a loaded HTML page?


Recommended Posts

Hi,

I'm trying to do something I believe is simple.

1. There is this HTML page, let's call it "page.html". It's on a distant server, I don't own it.

2. Once it's loaded, I want to calculate the pixel position of an element on it

3. To do so, I think the best solution would be to use such Javascript functions like document.getElementById("ElementName").style.left

4. So, I believe I need to insert that function into the page's body after it's loaded.

I don't know how to do this with AutoIt.

I've seen function _IEHeadInsertEventScript, but I'm not sure that's what I need, as it only works with events.

Here, I don't think I have an event to intercept, except maybe "onload", but since it happens before I can call IEHeadInsertEventScript, then it's of no use...

Any suggestions?

Edited by MonsieurOUXX
Link to comment
Share on other sites

Greasemonkey's fine and good, but I don't think there's an IE version.

Edit:

There is not, but there is iMacros (http://en.wikipedia.org/wiki/IMacros) ... Unfortunately, you have to pay for it to get greasemonkey style scraping and injecting. ... oh wait .. I.E. right ... forgot about that.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Using the IE.au3 you could do the same as greasemonkey does. It would require some work on your part.

Do some simple experiments to see it will work for you. Also, do a search in the forum for code that gives examples of what you have in mind

Edited by MPH
Link to comment
Share on other sites

I have something called gm4ie, but it's homepage has disappared. There is als Trixie: http://www.bhelpuri.net/Trixie/

This can also bedone with AutoIT with IE.au3 and there are examples in the forum -- depending how you are wanting to use the functionality.

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

Hey,

you can just access the document directly to get this done, something like this:

#include <IE.au3>
$oIE = _IECreate("http://autoitscript.com", 0, 0)
$oElem = $oIE.document.getElementById('nextlink')
If IsObj($oElem) Then MsgBox(262208, "Get element offset", "Element offset: " & $oElem.offsetTop & ", " & $oElem.offsetLeft & @CRLF)

hope that helps

Edit: Changed element id string to from 'featured-slider' to 'nextlink' to avoid offset being '0, 0'

Edited by Robjong
Link to comment
Share on other sites

Haha, all of you guys didn't seem to realize that Javascript and DOM are jibberish to me. When you write "there are examples all over the place"... I wouldn't even know how to find them (I have searched before posting) :graduated:

However Robjong's post makes it clearer for me. I didn't actually know that one can access to properties of a document directly, just like that.

Thanks also to all of you for your suggestions of external tools, I'll try them out to get better acquainted with the world of Web Pages manipulation.

I'll tag this thread as "SOLVED" as soon as I have time to test.

Link to comment
Share on other sites

UPDATE: I've produced the little function below to display

Func DumpCollection($oIE) $formName = "the_form_Im_Interested_in" $oForm = _IEFormGetObjByName($oIE, $formName) ;get the form from the page $oObjects = _IEFormElementGetCollection($oForm) Log("There are " & @extended & " objects in form '"&$formName&"'") For $oObject In $oObjects Log($oObject.name & ": "&$oObject.offsetTop&","&$oObject.offsetLeft&"") Next EndFunc

The ids of the objects are dumped correctly.

But offsetTop and offsetLeft return completely unlikely coordinates.

For example, on a big form, the so-called "top" coordinates of the objects are all smaller than 10 -- even though they are much farther way from the top of the document than 10 pixels! It should be hundreds of pixels.

I'm not sure if the issue is that I'm misusing offsetTop and offsetLeft, or simply that they don't make any sense in a "modern" form that is then reprocessed by the browser's renderer (e.g. because of font size)

EDIT: It's simply because according to the specs, offsetTop gives a value relative to the parent. I'll sort it out.

EDIT: I made this thread go off-topic since my very first post. I'll create a different thread for the pixel-position of objects. I'll be coming back to the original topic (how to add a javascript function to a page) in my latest post, below.

Edited by MonsieurOUXX
Link to comment
Share on other sites

Thanks John.

=> I'll create a different thread for the question about pixel position. Please do not answer here (please note, though, tat the "x" and "y" properties don't exist. I'll detail my answer in the new thread)

Back to the original topic: "Is it possible to insert a Javascript function into a page after it's loaded?"

What do you AutoIt people think of function _IEDocInsertText ?

See the last example at http://www.autoitscript.com/autoit3/docs/libfunctions/_IEDocInsertText.htm

Wouldn't that do the trick?

Link to comment
Share on other sites

Guess not because as the name says it inserts text, and _IEDocInsertHTML function seems to remove the script tag even if defer is set but I don't know why. (I guess Dale knows).

But it can be done a little different by adapting some of the _IE* functions, this just does not allow positioning of the script tags with bofereend etc.

Edit: Couldn't sleep again so wrote the functions _IEHeadInsertJS and _IEDocInsertJS which also support positioning of the tag with AfterBegin/BeforeBegin/AfterEnd/BeforeEnd.

They (are supposed to) work just like _IEDocInsertHTML with the exception of the $s_where parameter for IEHeadInsertJS which only accepts AfterBegin and BeforeEnd, for obvious reasons.

....

Edited by Robjong
Link to comment
Share on other sites

Couldn't sleep again so wrote the functions _IEHeadInsertJS and _IEDocInsertJS which also support positioning of the tag with AfterBegin/BeforeBegin/AfterEnd/BeforeEnd.

They (are supposed to) work just like _IEDocInsertHTML with the exception of the $s_where parameter for IEHeadInsertJS which only accepts AfterBegin and BeforeEnd, for obvious reasons.

OH. MY. GOD.

thanks! I'm pretty sure I won't be the only one who will use this!!!!

This should be insterted into the _IE UDF!

Edited by MonsieurOUXX
Link to comment
Share on other sites

  • 1 year later...

I am interested in the _IEHeadInsertJS and _IEDocInsertJS functions that you wrote. Is there a udf file that you made somewhere with them in it?

 

Hey grabb,

please try reviewing _IEHeadInsertEventScript in the help file.

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

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...