4

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
  • When toggling JavaScript in Safari's settings, two defaults keys do get written (WebKitJavaScriptEnabled and WebKitPreferences.javaScriptEnabled). Changing these from the command line almost works, but you seem to need to restart Safari for the change to be taken into account. Close, but no cigar.
    – Cykelero
    Commented Jul 23 at 14:50

2 Answers 2

11

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
0

Building off of @JMY1000 answer, you can create a toggle that also displays the state as a notification:

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
                set currentState to (its value as boolean)
                if currentState then
                    click javascriptBox
                    set stateMessage to "JavaScript disabled"
                else
                    click javascriptBox
                    set stateMessage to "JavaScript enabled"
                end if
            end tell
            keystroke "w" using command down
        end tell
    end tell
end tell

display notification stateMessage with title "Safari JavaScript Toggle"

Take this and use Automator to create a quick action, then link it in the system settings to a shortcut key. I used Cmd+Shft+J and now it's as it used to be and all is right with the world. This is just another way to skin the cat for those that don't want to jump on the shortcut train.

You must log in to answer this question.

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