I followed this article, explaining how to spice up an Internet Explorer COM-Object with jQuery. While the author used Python, I want to do something similar in Powershell.
Right now I have this code:
function addJQuery ($browser) {
$url="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"
$document = $browser.document
$window = $document.parentWindow
$head = @($document.getElementsByTagName("head"))[0]
$script = $document.createElement("script")
$script.type = "text/javascript"
$script.src = $url
$head.appendChild($script)
while (!$window.jQuery) {
sleep -milliseconds 100
}
return $window.jQuery
}
$ie = New-Object -comobject InternetExplorer.Application
$ie.visible = $true
$ie.Navigate("https://some.site.com")
while ($ie.busy) {start-sleep -milliseconds 500}
$j = addJQuery $ie
With Fiddler and via the document.scripts collection I verified that the file gets downloaded. However, the script sleeps forever and when I try to output $window.jQuery it prints nothing in the Powershell ISE console.
The Script is nevertheless correctly loaded, since jQuery-Functions can be called from the browser's console or via execScript().
It seems the problem is that the DOM-representation available via $ie.document isn't updated when DOM-changes are made via JavaScript. But shouldn't the Internet Explorer COM-Object behave the same way in Powershell as it does in Python?
For some reason I needed to make the browser window visible for mine to even remotely work - without it the Navigate method was unbearably slow and the $ie.Busy property would never seem to to return anything but True. You shouldn't have to do that, but oh well.
Inspecting the $ie.Document.Scripts collection, you can verify the jQuery file has been loaded, but I couldn't seem to make the reference $ie.Document.parentWindow work - which also means I can't seem to get at the jQuery property either. It is a known property, but it doesn't seem to be populated with anything useful as it is passed around in PowerShell.
In your
while
loop, the expression(!$window.jQuery)
always returnstrue
because$window
is aComObject
and COM Objects are not expandos like JavaScript objects, so even ifwindow.jQuery
exists in JavaScript, it won't automatically become visible on the$window
object in PowerShell.I really couldn't find a workaround to make jQuery objects available in powershell and I'm also interested to know if there is a way to do that. See this and this question I created on that.
But I figured this trick to run javascript/jquery on the web page and receive some results back from the page in PowerShell:
You may look into the source code of
Invoke-JQuery
. I've tried it and I was able to use jquery to manipuate the page, though I've not modified the script to add jquery.I wish I read this last year. I was also trying to integrate or "inject" JQuery in a Windows Scripting Host Environment. Also tried Powershell. None worked. I did however succeed in using this object "InternetExplorer.Application" with IE7, IE8 and now IE9.
After the above, JQuery is your friend. Again!!!
I found this nice "wait" function somewhere on the web:
The navigate function has all these optional parameters
Even though I also couldn't figure a way to work directly with JQuery as an object, the below is as close as I could get using Posh (I had to put the end of the first here-string in the same line in order to get the code formatting working):