Another way to execute JavaScript code natively in Shortcuts, besides using data: URLs
The code is also executed in a secure context unlike the data: URL method — letting you take advantage of several features that otherwise aren't available.
1. Inside a Text action, create an HTML document containing script code that writes its output to a visible element
2. Either convert the document with Make Rich Text from HTML or use Set Name and give it a name with html as extension e.g.
3. Convert this to PDF with the Make PDF action — this is the action that executes the JavaScript code
4. Extract the text from the PDF — for short output you can use Get Text from Input, but if the length of the output causes multiple PDF pages being made, you'll be asked to choose which page — If so, extract the text with Combine Text instead.
Empty lines aren't preserved with these actions though (probably a bug), but you can use Make Markdown from Rich Text as a workaround, which both preserves empty lines (not repeated ones) and combines the PDF pages.
To keep the output completely unmodified, you can URL encode or base64 encode it from the JavaScript side first. Base64 encoding also makes it possible to transfer non-text output like images. For larger output, you should split the encoded string by newlines every e.g. 64 or 76 characters, or the output from Make PDF will likely get corrupt. Then afterwards, combine and decode the output using the Combine Text, URL Encode and Base64 Encode actions.
Example shortcut — updated 2025-05-09 17:54 UTC
1. Inside a Text action, create an HTML document containing script code that writes its output to a visible element
2. Either convert the document with Make Rich Text from HTML or use Set Name and give it a name with html as extension e.g.
index.html
(same result with these actions)
3. Convert this to PDF with the Make PDF action — this is the action that executes the JavaScript code
4. Extract the text from the PDF — for short output you can use Get Text from Input, but if the length of the output causes multiple PDF pages being made, you'll be asked to choose which page — If so, extract the text with Combine Text instead.
Empty lines aren't preserved with these actions though (probably a bug), but you can use Make Markdown from Rich Text as a workaround, which both preserves empty lines (not repeated ones) and combines the PDF pages.
To keep the output completely unmodified, you can URL encode or base64 encode it from the JavaScript side first. Base64 encoding also makes it possible to transfer non-text output like images. For larger output, you should split the encoded string by newlines every e.g. 64 or 76 characters, or the output from Make PDF will likely get corrupt. Then afterwards, combine and decode the output using the Combine Text, URL Encode and Base64 Encode actions.
Example shortcut — updated 2025-05-09 17:54 UTC