IE COM 自动化:如何在 PowerShell 中获取 `window.execScript` 的返回值 [英] IE COM automation: How to get the return value of `window.execScript` in PowerShell

查看:28
本文介绍了IE COM 自动化:如何在 PowerShell 中获取 `window.execScript` 的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一篇文章 看来可以得到 IHTMLWindow2::execScript 的返回值 来自 Internet Explorer API,但是链接的示例是用 C++ 编写的,我不太明白.我正在尝试在 PowerShell 中做同样的事情,所以这是显示问题的代码:

From this other post it looks like it is possible to get the return value of IHTMLWindow2::execScript from Internet Explorer API, however the linked example is in C++ and I don't really understand it. I'm trying to do the same thing in PowerShell, so here's my code that shows the problem:

$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

我真正想做的是在我的 PowerShell 脚本中提供 jQuery 选择器/对象,以便我可以执行以下操作:

What I'm really trying to do is to make jQuery selector/objects available in my PowerShell script, so that I can do things like:

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

还有更多.

更新:看看这个Gist PowerShell 功能运行 JavaScript/JQuery 并将结果返回给 PS,超时.它是从下面的答案扩展而来的.

Update: Look at this Gist for PowerShell function to run JavaScript/JQuery and return results back to PS, with timeout. It was extended from the answer below.

推荐答案

首选方法可能是@Matt 建议使用 eval 方法而不是 execScript 已在 IE11 中弃用.但是,我仍然无法找到如何从 IE API 访问该 eval.我创建了另一个问题来跟进.

The preferred method is probably what @Matt suggested to use the eval method instead of execScript which has been deprecated in IE11. However, I still couldn't find how to access that eval from IE API. I created this other question to follow up with that.

然而,我可以想出一种在网页上执行 JavaScript/jQuery 并将结果返回给 PowerShell 的方法,即我们使用 setAttribute 将 JavaScript 返回值存储在 DOM 中,并且然后使用 getAttribute 在 PowerShell 中检索它.

I could, however, figure a way to execute JavaScript/jQuery on a web page and return the results back to PowerShell with this trick that we store the JavaScript return value in the DOM using setAttribute and then retrieving it in PowerShell using 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

这篇关于IE COM 自动化:如何在 PowerShell 中获取 `window.execScript` 的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆