AppleScript : Execute JavaScript on every Safari Tab/Window
Using the Ask Question button that's in the upper right hand corner of the page, like in this question, and having multiple windows each with multiple tabs to a Ask Different web page, as well as some non-related pages...
The following example AppleScript code clicks the Ask Question button on every page having the button. It doesn't matter what window or tab has focus, the button on all that have it gets clicked.
Example AppleScript code:
tell application &quot;Safari&quot; to tell tabs of windows to ¬
    do JavaScript &quot;document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();&quot;