2014-09-24 15 views
0

this other post看起來可以從Internet Explorer API獲取IHTMLWindow2::execScript的返回值,但是鏈接的示例是使用C++和I不太瞭解它。我試圖做同樣的事情在PowerShell中,所以這裏是我的代碼,顯示問題:IE COM自動化:如何在PowerShell中獲取`window.execScript`的返回值

$ie = New-Object -COM InternetExplorer.Application 
$ie.visible = $true 
$ie.navigate("http://google.com") 
while ($ie.busy) { start-sleep -m 100 } 
$document = $ie.document 
$window = $document.parentWindow 

Function eval($jsCommand) { 
    # Of course it doesn't return anything here. How can I get the return value? 
    return $window.execScript($jsCommand, 'javascript') 
} 

eval 'parseInt("12")' # returns nothing 

我真正要做的是讓jQuery選擇/對象可在我的PowerShell腳本,所以我可以做這樣的事情:

eval '$("input#selector")' | where name -eq 'username' 

和更多。

更新:查看此Gist for PowerShell函數以運行JavaScript/JQuery並將結果返回給PS,並且超時。它從下面的答案延伸出來。

+1

[This](http://msdn.microsoft.com/en-us/library/ie/ms536420(v = vs.85).aspx)可能是相關的。顯然,execScript總是返回null。這在IE11中也被棄用 – Matt 2014-09-24 16:40:15

+0

好的發現@Matt現在我試圖計算如何訪問[Global Object](http://msdn.microsoft.com/en-us/library/ie/52f50e9t(v = vs.85).aspx),其中包含[eval](http://msdn.microsoft.com/en-us/library/ie/12k71sw7(v = vs.85).aspx)函數。 – orad 2014-09-24 17:59:16

+0

創建了一個相關的問題[here](http://stackoverflow.com/q/26023915/450913)。 – orad 2014-09-24 21:21:18

回答

0

首選的方法可能是@Matt建議使用eval方法,而不是execScript,它已在IE11中棄用。但是,我仍然無法找到如何從IE API訪問eval。我創建了這個其他question跟進。

但是,我可以想出一個方法來在網頁上執行JavaScript/jQuery並將結果返回給PowerShell,這個技巧是我們使用setAttribute將JavaScript返回值存儲在DOM中,然後使用PowerShell檢索它getAttribute

# some web page with jQuery in it 
$url = "http://jquery.com/" 

# Use this function to run JavaScript on a web page. Your $jsCommand can 
# return a value which will be returned by this function unless $global 
# switch is specified in which case $jsCommand will be executed in global 
# scope and cannot return a value. If you received error 80020101 it means 
# you need to fix your JavaScript code. 
Function ExecJavaScript($ie, $jsCommand, [switch]$global) 
{ 
    if (!$global) { 
     $jsCommand = "document.body.setAttribute('PSResult', (function(){$jsCommand})());" 
    } 
    $document = $ie.document 
    $window = $document.parentWindow 
    $window.execScript($jsCommand, 'javascript') | Out-Null 
    if (!$global) { 
     return $document.body.getAttribute('PSResult') 
    } 
} 

Function CheckJQueryExists 
{ 
    $result = ExecJavaScript $ie 'return window.hasOwnProperty("$");' 
    return ($result -eq $true) 
} 

$ie = New-Object -COM InternetExplorer.Application -Property @{ 
    Navigate = $url 
    Visible = $false 
} 
do { Start-Sleep -m 100 } while ($ie.ReadyState -ne 4) 

$jQueryExists = CheckJQueryExists $ie 
echo "jQuery exists? $jQueryExists" 

# make a jQuery call 
ExecJavaScript $ie @' 
    // this is JS code, remember to use semicolons 
    var content = $('#home-content'); 
    return content.text(); 
'@ 

# Quit and dispose IE COM 
$ie.Quit() 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) | out-null 
Remove-Variable ie 
+0

看看這個[Gist](https://gist.github.com/omidkrad/fbb4bfb3b1f5b83c9898)PowerShell函數運行JavaScript/JQuery並返回結果回PS,超時。它從這個答案延伸出來。 – orad 2014-10-01 00:04:34