【问题标题】:How can I get textarea text value with autohotkey?如何使用 autohotkey 获取 textarea 文本值?
【发布时间】:2018-10-09 09:25:51
【问题描述】:

我正在尝试使用 https://www.tutorialspoint.com/online_java_formatter.htm 格式化我的 java 代码。但我在从 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>

自动热键代码:

;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

【问题讨论】:

  • 你到底面临什么问题?
  • 此代码无法从 textarea 获取文本值。当我尝试使用 MessageBox 显示剪贴板变量时,messagebox 显示空白文本

标签: html automation autohotkey


【解决方案1】:

问题是您用来格式化代码的网站没有使用普通的&lt;textarea&gt;,而是使用this code editor。如果您查看站点源代码底部附近的 JavaScript,您会发现使用了其中两个编辑器(editoroutputeditor)。它们的setValuegetValue 方法可用于处理它们的内容。

要在 AHK 中执行此操作,您可以在页面中创建一个元素,其内容将保存剪贴板数据以及稍后的格式化结果。 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

请注意,美化按钮的点击处理程序在 outputeditor 更新之前返回,因为它只是发送带有未格式化代码的异步 POST 请求,所以我添加了一个回调到 outputeditor 的更改事件,当请求完成并更新编辑器。此回调中的marker 用于区分由于最初包含在网站上的代码而发生的outputeditor 更改事件与格式化我们的代码后发生的更改事件。我假设您不想看到 IE 窗口并删除了它的可见性,但如果您确实希望它可见,您需要使用 WinWaitActive 或类似的东西来等待 IE 窗口关闭和上一个(复制- from) 窗口在发送 Ctrl+v 之前再次变为活动状态。

由于格式化实际上是通过 POST 请求完成的,因此您可以改用 WinHttp.WinHttpRequest.5.1 COM 对象来复制请求。但是,IMO 比较麻烦(至少在 AHK 中),因为您必须在剪贴板中手动编码 Java 代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 2011-03-23
    • 2015-06-13
    • 2018-11-16
    • 2011-08-15
    • 2011-12-15
    • 2018-01-12
    • 2010-09-13
    相关资源
    最近更新 更多