简体   繁体   English

如何使用自动快捷键获取textarea文本值?

[英]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,您会发现使用了其中两个编辑器( editoroutputeditor )。 Their setValue and getValue methods can be used to work with their contents. 它们的setValuegetValue方法可用于处理其内容。

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.

相关问题 我怎样才能得到一个文本<textarea>&lt;i&gt;in a webpage?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在网页中?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> Html:</p><pre> &lt;body&gt; &lt;textarea readonly id="txtImagename1" name="txtImagename1" rows="1000" cols="200"&gt; testing second line &lt;/textarea&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 如何通过 c# 代码获取 textarea 中的文本?</p><blockquote><p> (测试\n第二行)</p></blockquote></div> - How can I get the text of a <textarea> in a webpage? 如何获取textarea中第一行和第二行的值? - How can I get the value of the first and second line in a textarea? 我怎样才能像这种格式一样将此文本区域值(内容)转换为 DIV - How can i get This textarea value (content) into DIV like this format 我如何从textarea获取价值并垂直打印? - How can i get value from textarea and print it vertically? 为什么我无法获取此 textarea 的值? - Why I can't get the value of this textarea? 我如何从 textarea 获取价值并检查它? - How I get value from textarea and check it? 如何获得文本区域的价值? - How do I get the value of a textarea? 我如何获得此textarea jQuery的价值 - How do I get the value of this textarea jquery 我如何检索textarea的当前值? - how can i retrieve a current value of textarea? 我怎样才能专注于textarea价值的终结? - How can I focus on the end of textarea value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM