tell application "Safari"
set theVar to (do JavaScript "
var a =
document.getElementById('unitsdiv').getElementsByClassName('even');
var b = []
for (var i = 0; i < a.length; i++) {
b[i] = a[i].textContent;
};
console.log(b.length); \\ Returns 2
console.log(b[1]); \\ Returns item 2 of Array
console.log(b[0]); \\ Returns item 1 of Array
b; \\ Shows both items in Safari Console
") in document 1
end tell
log theVar -- Missing Value
代码在Safari控制台中按预期工作。我还尝试在运行带有set theVar to {}
的do Javascript命令之前初始化列表,但无济于事。任何帮助,将不胜感激。
这似乎返回了一个值
tell application "Safari"
tell document 1
set theVar to (do JavaScript "
var a = document.getElementById('unitsdiv').getElementsByClassName('even');
var b = [];
for (var i = 0; i < a.length; i++)
{ b[i] = a[i].textContent; };
console.log(b.length);
console.log(b[1]);
console.log(b[0]);
b[0];")
end tell
end tell
log theVar
如果我尝试从javascript代码末尾的[0]
处取出数组选择器b
,请告诉AppleScript log theVar
它会抛出错误theVar
没有定义。