[英]How can I get textarea text value with autohotkey?
I am trying to format my java code with https://www.tutorialspoint.com/online_java_formatter.htm . 我正在尝试使用https://www.tutorialspoint.com/online_java_formatter.htm格式化Java代码。 But I have an issue with getting text from textarea.I am trying to get text from following textarea. 但是我从textarea获取文本有问题,我正在尝试从以下textarea获取文本。
<textarea class="ace_text-input" style="width: 6.59px; height: 14.05px; right: 428.4px; bottom: 511.79px; opacity: 0;" spellcheck="false" wrap="off"></textarea>
Autohotkey code: 自动热键代码:
;code beautifier java
^+b::
Send ^c
formatter := "https://www.tutorialspoint.com/online_java_formatter.htm"
(pwb2 := ComObjCreate("InternetExplorer.Application")).Visible:=True
pwb2.navigate(formatter)
while pwb2.busy
sleep 15
pwb2.document.getElementsByTagName("textarea")[0].value=Clipboard
pwb2.document.getElementById("beautify").Click()
sleep 5000
Clipboard := pwb2.document.getElementsByTagName("textarea")[1].innerHTML
Send, ^v
pwb2.quit()
Return
The problem is that the website you're using to format the code doesn't use a plain <textarea>
, but rather this code editor . 问题在于,您用来格式化代码的网站没有使用普通的<textarea>
,而是使用了此代码编辑器 。 If you look at the JavaScript near the bottom of the site's source you can see that there's two of these editors being used ( editor
and outputeditor
). 如果您查看站点源代码底部附近的JavaScript,您会发现使用了其中两个编辑器( editor
和outputeditor
)。 Their setValue
and getValue
methods can be used to work with their contents. 它们的setValue
和getValue
方法可用于处理其内容。
To do this in AHK, you can make an element in the page whose content will hold the clipboard data and later the result of the formatting. 要在AHK中执行此操作,您可以在页面中创建一个元素,其内容将保存剪贴板数据,然后保存格式设置的结果。 pwb2.document.parentWindow.execScript
can be used to execute the JavaScript for interacting with the editors using the data from the clipboard element. pwb2.document.parentWindow.execScript
可用于执行JavaScript,以使用剪贴板元素中的数据与编辑器进行交互。
^+b::
Clipboard := ""
Send {Ctrl Down}c{Ctrl Up}
ClipWait 1
if ErrorLevel {
MsgBox Java formatter error: Clipboard is empty
return
}
java := Clipboard
formatter := "https://www.tutorialspoint.com/online_java_formatter.htm"
pwb2 := ComObjCreate("InternetExplorer.Application")
pwb2.navigate(formatter)
while pwb2.busy
sleep 15
d := pwb2.document
ahkData := d.createElement("div")
ahkData.id := "ahkData"
ahkData.hidden := true
ahkData.innerText := java
d.body.appendChild(ahkData)
js =
(
var ahkData = document.getElementById('ahkData');
var marker = "//AHKDATA\n";
editor.setValue(marker + ahkData.innerText);
outputeditor.on("change", function(e) {
var value = outputeditor.getValue();
if (value.indexOf(marker) !== -1) {
ahkData.innerText = value.substring(marker.length);
ahkData.hidden = false;
}
});
document.getElementById("beautify").click();
)
d.parentWindow.execScript(js)
while ahkData.hidden
sleep 150
Clipboard := ahkData.innerText
Send ^v
pwb2.quit()
Return
Note that the click handler for the beautify button returns before outputeditor
is updated because it just sends an asynchronous POST request with the unformatted code, so I added a callback to outputeditor
's change event which will run when the request finishes and updates the editor. 请注意,美化按钮的单击处理程序在outputeditor
更新之前会返回,因为它只是发送带有未格式化代码的异步POST请求,因此我向outputeditor
的change事件添加了回调,该回调将在请求完成并更新编辑器时运行。 marker
in this callback serves to differentiate the outputeditor
change events that occur because of the code initially included on the website from those that occur after formatting our code. 此回调中的marker
用于区分由于最初包含在网站上的代码而导致的outputeditor
更改事件与格式化我们的代码后发生的事件之间的区别。 I assumed you don't want to see the IE window and removed its visibility, but if you did want it visible you'll need to use WinWaitActive
or something similar to wait for the IE window to close and the previous (copied-from) window to become active again before sending Ctrl + v . 我以为您不想看到IE窗口并删除了它的可见性,但是如果您希望它可见,则需要使用WinWaitActive
或类似的方法来等待IE窗口关闭并返回上一个(复制自)发送Ctrl + v之前,窗口再次变为活动状态。
Since the formatting is really done through a POST request, you could instead use the WinHttp.WinHttpRequest.5.1
COM object to replicate the request. 由于格式化实际上是通过POST请求完成的,因此您可以改用WinHttp.WinHttpRequest.5.1
COM对象复制请求。 However, IMO it's more cumbersome (in AHK at least) since you'll have to manually encode the Java code in the clipboard. 但是,IMO(至少在AHK中)比较麻烦,因为您必须在剪贴板中手动编码Java代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.