Search code examples
macosgoogle-chromeapplescriptjavascript-automation

Getting values from inner functions of current webpage in Chrome via (JXA / AppleScript)


I can't get values from inner functions of current webpage in Chrome. Why it's happened?

for examples in JXA:

function getTime() {
    const chrome = Application('Google Chrome')
    const currentTab = chrome.windows[0].activeTab()
    return currentTab.execute({javascript: 'jwplayer().getPosition()'})
}
getTime()

Chrome returns reference error:

 Uncaught ReferenceError: jwplayer is not defined

in AppleScript the same:

tell application "Google Chrome"
    return execute front window's active tab javascript "jwplayer().getPosition()"
end tell

Settings in Chrome is checked: "Allow Javascript from Apple Events"


Solution

  • Founded a workaround with AppleScript:

    tell front window of application "Google Chrome"
        set URL of active tab to "javascript:
        sessionStorage.setItem('scrapy', JSON.stringify({
          currentTime: jwplayer().getPosition()
        }, 2, '  ')
      "
        return {execute active tab javascript "sessionStorage.getItem('scrapy')"}
    end tell