使用applescript / javascript将文件内容添加到webform

时间:2014-08-25 22:22:43

标签: javascript webforms web-scraping applescript

我试图将文本文件(在本例中为json)的内容添加到webform中。 数据是一个json列表/数组,如: [{'id':1},{'id':2},{'id':3}] 哪个保存在' myfile.json'中。

我尝试打开需要身份验证的网页,但没有一个友好的身份验证API。例如,我将使用stackoverflow。

这是我到目前为止所得到的:

set fileContents to (read POSIX file "myfile.json" as «class utf8»)

tell application "Safari"
    if not (exists document 1) then reopen
    activate
    set URL of document 1 to "http://stackoverflow.com/questions/ask"
    delay 2
    do JavaScript "document.getElementById('wmd-input').value=" & fileContents in document 1
end tell

这似乎工作得很好,除了最后粘贴到文本域的所有内容都是

  

[object Object],[object Object],[object Object],[object Object],[object Object],[object> Object],[object Object],[object Object],[object Object], [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object> Object],[object Object],[object Object ],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object ],[对象]

如何将文件的实际内容放入文本框?

2 个答案:

答案 0 :(得分:1)

do JavaScript "document.getElementById('wmd-input').value=" & fileContents in document 1将被评估为document.getElementById('wmd-input').value=[{...},{...}](对象列表)。

选项是使用JSON.stringify()将fileContents转换为实际字符串,或者将"添加到fileContents

'document.getElementById("wmd-input").value=JSON.stringify(' & fileContents & ')' 'document.getElementById("wmd-input").value=\"' & fileContents & '\"'

答案 1 :(得分:0)

如果您做了很多这样的事情,您可能需要查看Fake,它是Mac App Store中提供的AppleScript兼容的Web浏览器。自动填写表单和其他用户操作,进行测试或其他任何目的都非常容易。