通过 (JXA / AppleScript) 从 Chrome 当前网页的内部函数获取值

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

我无法从 Chrome 中当前网页的内部函数获取值。为什么会这样?

JXA 中的示例:

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

Chrome returns 引用错误:

 Uncaught ReferenceError: jwplayer is not defined

在 AppleScript 中相同:

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

已检查 Chrome 中的设置:"Allow Javascript from Apple Events"

使用 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