Can AppleScript access browser tabs and execute javascript in them?

后端 未结 3 739
逝去的感伤
逝去的感伤 2021-01-31 19:47

I\'m curious if AppleScript can access each specific tab in a browser and execute some javascript in them.

Anyone have ideas?

3条回答
  •  甜味超标
    2021-01-31 20:18

    For Google Chrome, use execute from AppleScript Chromium Suite:

    execute v : Execute a piece of javascript.

    execute specifier : The tab to execute the command in.

    javascript text : The javascript code to execute.

    Example:

    tell application "Google Chrome"
        execute front window's active tab javascript "alert('example');"
    end tell
    

    Can be done in Safari either:

    tell application "Safari" to do JavaScript "alert('example')" in document 1
    

    For other browsers, check the functions at File -> Open Dictionary on AppleScript editor.

提交回复
热议问题