1. 程式人生 > >Hacking around with JavaScript and Shortcuts in iOS 12

Hacking around with JavaScript and Shortcuts in iOS 12

Let’s ramp it up a little more, and try to implement a shortcut that activates gyroscope scrolling on any webpage. If your phone is tilted back, it’ll scroll up, start bringing it upright, we’ll scroll down, and bring it up to 90 degrees, and we’ll scroll down even faster.

Don’t want to wait? I’ve already built and uploaded this Shortcut to GitHub, so you can download and use it straight away:

https://www.github.com/chrishutchinson/ios-shortcuts-demos.

Follow the same procedure as before to create a new Shortcut, but paste in the above JavaScript code. This code is a little more complex, but nothing we shouldn’t be able to follow:

  1. Create a function that handles device orientation change
  2. Inside that function, grab the “beta” direction (horizontal tilt) and check the value
  3. Based on the value, do nothing, scroll up, scroll down, or scroll down faster
  4. Assign our function to the deviceorientation event
  5. Call the completion() handler to tell Shortcuts we’re done

This is pretty crude, but will do the trick for now. Make sure you set up the share sheet and Safari Web Page triggers in the Shortcut configuration, save it, and give it a whirl. You should be able to tilt your phone back and forth, and scroll up and down the page!

So this is pretty cool, we can interact with the page, get device details, and make it do stuff!