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
The code works as expected in Safari Console. I have also tried initializing an list prior to running the do Javascript command with set theVar to {} to no avail. Any help would be appreciated.
This seems to return one of the values
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
If I try to take the array selector [0] off of b at the end of the javascript code then tell AppleScript to log theVar it throws an error that that theVar is not defined.