本文介绍了Powershell:通过InternetExplorer.Application使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我按照这篇文章说明了使用jQuery来调用Internet Explorer COM对象。虽然作者使用Python,我想在Powershell中做类似的操作。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.现在我有这个代码: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通过Fiddler和通过document.scripts集合,我验证了文件被下载。然而,脚本睡着了,当我尝试输出$ window.jQuery它在Powershell ISE控制台中什么都不打印。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.脚本仍然正确加载,因为jQuery-函数可以从浏览器的控制台或通过execScript()调用。The Script is nevertheless correctly loaded, since jQuery-Functions can be called from the browser's console or via execScript().似乎问题是,通过$ ie.document可用的DOM表示不更新DOM更改是通过JavaScript进行的。但是,在Powershell中,Internet Explorer COM对象不应该像在Python中一样工作。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?推荐答案 code> while 循环,表达式(!$ window.jQuery)总是返回 true 因为 $ window 是一个 ComObject ,COM对象不像JavaScript对象一样是expandos,所以即使 window.jQuery 存在于JavaScript中,它不会自动在PowerShell中的 $ window 对象上可见。In your while loop, the expression (!$window.jQuery) always returns true because $window is a ComObject and COM Objects are not expandos like JavaScript objects, so even if window.jQuery exists in JavaScript, it won't automatically become visible on the $window object in PowerShell.我真的找不到一个解决方法,使jQuery 对象在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.但我想出了这个技巧,在网页上运行javascript / jquery,并从PowerShell中的页面接收一些结果:But I figured this trick to run javascript/jquery on the web page and receive some results back from the page in PowerShell:# 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 $ieecho "jQuery exists? $jQueryExists"# make a jQuery callExecJavaScript $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-nullRemove-Variable ie 这篇关于Powershell:通过InternetExplorer.Application使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 18:00
查看更多