3

Safari 16.xx had an option Disable JavaScript in menu bar>Developers. I had a shortcut to simply enable/disable JavaScript with an easy shortcut.

With Safari 17 Apple redesigned the developers section in Safari. The option to enable/disable JavaScript from the menu bar doesn't exist any more. Therefore, the shortcut doesn't work any more. Now, you can only enable/disable JavaScript within Safari preferences>Security>Enable JavaScript.

Does anyone has an idea, how to receive the old behavior: enable/disable JavaScript with a simple shortcut?

1 Answer 1

10

There used to be the setting com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptEnabled, but this seems to have disappeared sometime prior to Safari 17. As a substitute, we can use AppleScript to do some GUI scripting. It's far from ideal, but it works.

Here's a link to the Shortcut; you can change the keyboard binding as desired by clicking on "Quick Actions" and changing the "Run with" binding:

enter image description here

Here's also a copyable version of the AppleScript:

tell application "System Events"
    tell application process "Safari"
        set frontmost to true
        keystroke "," using command down
        tell window 1
            click button "Security" of toolbar 1 of it
            click checkbox 1 of group 1 of group 1 of it
            keystroke "w" using command down
        end tell
    end tell
end tell

You can also use separate the Shortcuts for enabling and disabling Javascript if you want. Here's the corresponding AppleScript; remove the not as appropriate:

tell application "System Events"
    tell application process "Safari"
        set frontmost to true
        keystroke "," using command down
        tell window 1
            click button "Security" of toolbar 1 of it
            set javascriptBox to checkbox 1 of group 1 of group 1 of it
            tell javascriptBox
                if not (its value as boolean) then click javascriptBox
            end tell
            keystroke "w" using command down
        end tell
    end tell
end tell

Note that you may have to grant siriactionsd and Script Editor permission to control your computer in System Settings → Privacy and Security → Accessibility:

enter image description here

2
  • Cool, thank you, and much needed. I’m afraid my AppleScript is too rusty to figure out how to split it into two scripts, one to disable JavaScript and another to reenable it. Any tips? Commented Nov 1, 2023 at 17:59
  • 2
    Updated the body of the answer with separate shortcuts for enabling and disabling JavaScript.
    – JMY1000
    Commented Nov 2, 2023 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .