Use the Run JavaScript on Webpage action in Shortcuts
Use the Run JavaScript on Webpage action to retrieve specific data from a website or to modify the contents of a webpage.
The Run JavaScript on Webpage action contains a text field where you write your own script. You run the shortcut from the Safari app.
Note: You can also use the Run JavaScript on Webpage action in SFSafariViewController. For more information, see https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller.
Create a JavaScript shortcut
In My Shortcuts , tap Create Shortcut.
In the shortcut editor, tap at the top of the action list, begin typing “Run JavaScript…” in the search field, then tap the Run JavaScript on Webpage action to add it to the shortcut editor.
Write your script in the text field in the Run JavaScript on Webpage action.
Important: In your script, make sure you call the completion handler. For more information, see Run JavaScript on Webpage action output.
Add any other actions you want to include in the shortcut.
Tip: To use multiple Run JavaScript on Webpage actions in the same shortcut, make sure each instance of the action has the Safari webpage as its input.
Tap to open Details, then turn on Show in Share Sheet.
Tip: To access the Shortcuts User Guide, tap Shortcuts Help.
A new row labelled “Accepted Types” appears. To ensure that the shortcut appears only in Safari, deselect all input types except “Safari webpages”.
For more information, see Understanding share sheet types in Shortcuts.
Tap Done to return to the shortcut editor, then tap Done again to save the shortcut to My Shortcuts.
For more information about working with JavaScript in Shortcuts, see Intro to the Run JavaScript on Webpage action in Shortcuts.
Run the JavaScript shortcut on a webpage
In Safari, tap .
Tap a shortcut containing the Run JavaScript on Webpage action.
Note: You can also run the shortcut in SFSafariViewController.