Passing the HTML of a webpage to a shortcut

Hello. I am writing a script that uses a WebView to open up a webpage and then get its HTML. I would like to pass the HTML to a shortcut and was wondering what the best way to do this would be. The contents of the HTML may be a lot so I am hoping that there is a method that ensures that all the HTML is passed successfully to the shortcut. Would anyone have any suggestions? Thanks so much.

1 Like

Apart from directly passing it via a URL, or the clipboard, you could write it to a file and have Shortcuts read it in.

1 Like

If you have access to JavaScript this is pretty simple:

Get everything in the tag…

const result = document.body.innerHTML;

Get everything in the HTML tag ( too)…

const result = document.documentElement.innerHTML;

And with the Run JavaScript action in shortcuts, you can run this on any url passed into your shortcut. Just pass the result into the build-in completion() method and you can pass it on to the next action in shortcuts. Here’s an example.

https://www.icloud.com/shortcuts/92fc3ef066664ecf9aa1de3699e8debf

Using JavaScript for working with the DOM is advantageous. But, it is worth noting that the “Run JavaScript on Active Safari Tab” that you’re using relies on the content coming from Safari … but in this case it is a webView in Scriptable, so I’m not sure if that’s going to be applicable.

2022-05-15-14.30.56

If you were just getting it from a URL in Shortcuts rather than via Safari, you can, as an alternative, grab the content of the URL and get its HTML; the OP does say “all the HTML”, so that is an option…

…but I was inferring that the OP was doing something with the HTML from the webView prior to going on to Shortcuts - e.g. processing or manipulating the page first, or reducing the number of web accesses overall to imnprove performance.

I guess another alternative could even be running the Scriptable script inside of Shortcuts, but there’s no context for what else is going on in the end-to-end workflow as to whether that is viable.

1 Like

Ah, that’s true. But yeah, the get contents action should work. Well, hopefully something here is a help :slight_smile:

Thank you. All of this information is useful. I really appreciate every bit shared by you both. It helps me to know how to best solve what I am trying to accomplish. Thank you both.

1 Like

Glad it was a help! Good luck!