iOS 12 Shortcut Setup Question for "Run JavaScript on Web Page"
Asked Answered
A

4

11

I'm hoping this is the right place for this question since it centers around JavaScript. In the new iOS 12 Shortcuts app you can create workflows. I want to create one that simply launches a web page, fills in my username and password, then clicks the submit button, something like this...

document.myForm.username.value = 'myUsername';
document.myForm.password.value = 'myPassword';
document.getElementById('loginSubmit').submit();

But before even getting that far I just want to run an alert(1);. I can't get that to happen. I keep getting this error message below...

Run JavaScript on Web Page failed because Shortcuts couldn't convert from URL to Safari web page.

enter image description here

I'm not sure what that means. I haven't been able to find info or tutorials on how to use this. Does anyone know how to get JavaScript to run? Thanks!

Allantois answered 18/9, 2018 at 14:5 Comment(0)
H
7

the first thing you need to know is that docs are here: https://support.apple.com/guide/shortcuts/welcome/ios

Now, the problem is that "Run Javascript on web page" need an input called "Safari web pages" but Open urls only gives as output the result of opening the url that is showing the page in safari, in order to make it work you need Open url to give you a "Safari web page" item.

The only way that I found to do that is to use the shortcut as a "Share extension"

  1. In your workflow delete the URL item
  2. Go to the shortcut settings page by tapping the icon Settings icon
  3. Now tap on "Show in Share Sheet"
  4. In the "Accepted types" section, select only URLs that is at the very bottom of the list.
  5. Finally go to safari, open the url you want and tap the share button, if this is the first time you do this, you need to active the "Shortcuts" section, in the bottom list go to the end and tap in "More", the scroll down and select "Shortcuts" After doing that you will have the Shortcuts option, tap on it and select your Shortcut and it will run the java script in the page.

Is important to note that you will need to do something with the output of "Run javascript on web pages" like showing the result in one alert, because the way you have your workflow now, it may look like nothing is happening.

Hatchery answered 18/9, 2018 at 15:30 Comment(1)
In iOS 14, for Step #3, you have to go to the Share Sheet section at the top level of "My Shortcuts"Cameliacamella
C
7

There's a note in the Run JavaScript on Web Page action that says, "Safari Web Page item in shortcuts stating that they are only available when running your shortcut as an Action Extension in Safari."

So, sadly, it's not possible to chain up actions where it launches a URL and then runs JS. You must visit the url and run the action from the share sheet.

Candlepin answered 9/10, 2018 at 3:2 Comment(1)
That is insanely unfortunateBucentaur
B
2

The Safari actions require Safari to be open to the page you want when you launch the activity. That said, you can accomplish this easily using another element.

I don’t know your technical level, so I apologize if any of this is stuff you already know, but for you and for future reference to anyone looking, here is the howto.

First, go to the page you want on a Mac/PC browser and open the developer tools. I use Firefox Developer Edition, but Safari and Chrome have them also. If you’re using MacOS Safari, you will first need to open Safari’s Preferences menu and check “developer options” and relaunch.

Once the developer tools are open, switch to the Network tab / pane; find and turn off the option which clears the log on each page load. Keep this pane open, fill out the form, hit the trash can to clear the network log, and then submit the form. Now the HTTP request of the form submission should be either the first or the only thing in the list.

Select the line for the form submission and look at the request header. There should be a Request Method, Request Url, and all the key-value pairs that were submitted with the form.

Back in the Shortcuts app, add a URL element and give it the same URL as the Request Url. Chain this URL input element to a Get Contents of URL action element and toggle open the Advanced drawer. If there are any additional headers in the request log, you can tap Add new header to add more. Match the Request-Type to the Method field, choose “Form” as the Request Body, and then add in all the keys and values from the request.

Anyways, that’s it.

Chain this to Set Variable then use an IF block to test if the login succeeded or not. If it succeeded, make a Show Result that says “You are now logged in.” and if it failed, show the variable so that you can debug the response.

Bean answered 8/11, 2018 at 2:34 Comment(0)
C
0

For API requests in shortcuts, I typically use the Get Contents of URL shortcut, which allows you to use different HTTP methods (POST, GET, etc) and adjust the URL parameters.

In your case, once you authenticate, you could use the authentication token to make other requests as needed.

View Apple's documentation here for an example

Cameliacamella answered 2/7, 2021 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.