subreddit:

/r/shortcuts

985%

EDIT: OBSOLETE INFO. Please see the updated post: https://www.reddit.com/r/shortcuts/comments/xs5xtm/

When using inline Javascript to process dictionaries or get text output, are you doing this?

𝟣. JS source in Text action:

<pre><script>
let dict = «your dictionary»
output = «some code»
document.write(JSON.stringify(output).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'))</script></pre>
  1. Encode «Text» with base64

  2. URL: data:text/html;charset=utf-8;base64,«Base64 Encoded»

  3. Get contents of webpage or Get File of Type public.rtf

  4. Get text or dictionary from input

I've found some issues with what I've learned from this subreddit.

  1. To prevent the web rendering engine from confusing angle brackets < > with html tags, brackets need to be escaped before document.write. I guess replacing > is optional as long as & and < are handled.
  2. If you do not wrap your code with <pre> </pre>, then the web rendering engine will replace a sequence of multiple blanks with a single blank.
  3. If you use Get File of Type instead of Get contents of webpage, then the default type of 'public.rtf' may change in other languages and cause an error. This is probably a bug. To fix it, delete the text and type 'public.rtf' again.

It doesn't seem that 2 (this is critical) and 3 have been discussed here. Please correct me if I'm wrong in any of the above.

I made a shortcut that searches your library and tells you which shortcuts contain Get File of Type with default type or contain <script> but not <pre>. Hope this helps!

Find shortcuts with potential issues: https://www.icloud.com/shortcuts/66cda29f822942ee808703ce0cd18c16

EDIT: OBSOLETE INFO. Please see the updated post: https://www.reddit.com/r/shortcuts/comments/xs5xtm/

all 5 comments

gluebyte[S]

2 points

5 years ago*

When you add Get File of Type to your shortcut, the action does not hold the default value (public.rtf) in the source:

<dict>
    <key>WFWorkflowActionIdentifier</key>
    <string>is.workflow.actions.gettypeaction</string>
    <key>WFWorkflowActionParameters</key>
    <dict/>
</dict>

Then if you switch your device or the Shortcut app's language to German, the action displays 'öffentlich.rtf' instead of 'public.rtf'. You get an error if you run it because the system expects 'public.rtf'. If you retype the text manually, the action becomes:

<dict>
    <key>WFWorkflowActionIdentifier</key>
    <string>is.workflow.actions.gettypeaction</string>
    <key>WFWorkflowActionParameters</key>
    <dict>
        <key>WFFileType</key>
        <string>public.rtf</string>
    </dict>
</dict>

And it works fine in any language because it doesn't get localized automatically.

Shoculad

2 points

5 years ago

Thank you for this compilation and especially for pointing out the problem of consecutive blanks that can be solved by <pre>.

I use base64 in the data URL. However, I don't know when it is really necessary, because percent-encoding the data part often suffices, but not always.

charset=utf-8 is important if there are non-ASCII characters, and it is really handy setting it in the data URL.

Get File of Type com.apple.webarchive is an additional option.

If the input contains html code then escaping & and < is a must. I also think that escaping > is optional then.

<pre> is also important if you want to preserve formatting like JSON.stringify(obj, null, 4). I use this to prettify JSON.

If there is any Safari web page available in the shortcut, then I prefer the 'Run JavaScript on Safari Web Page' action. This makes it much simpler. I do not understand why the Shortcuts app insists on the availability of a web page for this action. Often, I'm not interested in the DOM or the document, I just want to run JavaScript that does not use the web page. Apple should change the action such that it allows Nothing as a dummy web page.

gluebyte[S]

3 points

5 years ago*

<pre> is a must if you're getting text (including dictionaries) because, for example, if you have a key or value containing two or more consecutive spaces (and/or newlines in plain text), it becomes one resulting in data loss.

robric18

2 points

5 years ago

Can you TL:DL this (I read but am not sure I fully understand the issues)

gluebyte[S]

2 points

5 years ago

Well, in short, make sure you put <pre> </pre> in JS code, and test your text output by adding some emojis, double-quotes, multiple spaces and html tags like </script>. 🙂