Use the Run JavaScript on Web Page action in Shortcuts
Use the Run JavaScript on Web Page action to retrieve specific data from a website, or to modify the contents of a webpage.
The Run JavaScript on Web Page 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 Web Page action in SFSafariViewController.
Create a JavaScript shortcut
In the Shortcuts app Library , 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 Web Page action to add it to the shortcut editor.
Note: If the Run JavaScript on Web Page action’s input isn’t a Safari webpage, an error message will appear when you run the shortcut. Additionally, if the Run JavaScript on Web Page action isn’t the first action in the shortcut, you need to use a Get Variable action and set its parameter to the Safari webpage (or the shortcut’s input).
Write your script in the text field in the Run JavaScript on Web Page action.
Important: In your script, make sure you call the completion handler. For more information, see Run JavaScript on Web Page action output.
Add any other actions you want to include in the shortcut.
Tip: To use multiple Run JavaScript on Web Page actions in the same shortcut, make sure each instance of the action has the Safari webpage as its input (using the Get Variable action).
Tap to open Settings, then turn on Show in Share Sheet.
A new row labeled “Accepted Types” appears. So that the shortcut only appears in Safari, deselect all input types except “Safari web pages.”
For more information, see About input types in an Action Extension shortcut.
Tap Done to return to the shortcut editor, then tap Done again to save the shortcut to your Library.
For more information about working with JavaScript in Shortcuts, see About the Run JavaScript on Web Page action in Shortcuts.
Run the JavaScript shortcut on a webpage
In Safari, make sure the Shortcuts button is enabled in the share sheet.
In Safari, tap .
In the bottom row (the monochrome buttons), tap Shortcuts.
If the Shortcuts button is not immediately visible, swipe left.
In the Run Shortcut screen, tap a shortcut containing the Run JavaScript on Web Page action.
Note: You can also run the shortcut in SFSafariViewController.