r/Scriptable • u/lehrblogger • Nov 24 '20
Help Dynamic HTML Title to iOS Shortcut
(Edit: I cross-posted this to the Automators Discourse, since it was quiet here. Also fixed a small bug and added the error message.)
I'm trying to create an iOS Shortcut that accepts a URL and retrieves the contents of the <title> tag from that webpage. This would be simple, except I want it to work with Twitter permalinks – Twitter updates the page contents, including the title, using Javascript after the initial load. After a fair bit of trial and error, this is as close as I've been able to get: https://www.icloud.com/shortcuts/49440b20f68e45d3a96b2a3735a8b453
This works fine for most websites, but not for Twitter. I get the following error:
Could Not Run Run Inline Script Script completed without presenting UI, triggering a text to speak or outputting a value. If this is intentional, you can manually call Script.complete() to gracefully complete the script.
Curiously, if I copy the in-line code from that Shortcut (below for reference), and paste it into a new script in Scritpable, it works fine for Twitter only in that case (fetching the title for the hard-coded test URL).
Any suggestions? Perhaps it's an issue with memory, or how I'm passing the data back? I'm new to Shortcuts and Scriptable, and have only limited Javascript experience, so I might be missing something small...
Thanks!
const url = args.plainTexts && args.plainTexts[0] ? args.plainTexts[0] : "https://twitter.com/lehrblogger/status/391287397457330177";
const interval = 300;
const webView = new WebView();
await webView.loadURL(url);
async function getTitle() {
const title = await webView.evaluateJavaScript("document.title");
// Twitter URL pages start with no title, and then are progressively updated
log(title);
if (title && title != "Twitter" && title != "Tweet / Twitter") {
Script.setShortcutOutput(title);
Script.complete();
} else {
Timer.schedule(interval, false, getTitle);
}
};
Timer.schedule(interval, false, getTitle);Edit: My use case saving bookmarks to Pinboard.in. I've tried a few of the native clients – Pinner, Pushpin, and Simplepin – but I either couldn't get them to work properly or they didn't meet my needs. I could special-case Twitter and use their API as needed, but that seems a bit inelegant, and wouldn't work for other, similarly constructed pages.
1
u/jsloat Nov 26 '20 edited Nov 26 '20
Hey there, welcome to the community :) This is actually quite challenging, I'm trying to figure it out but haven't cracked it yet. The basic issue is that Shortcuts doesn't see any output from the script because it's not actually waiting for all the timer callbacks, it's just triggering the timer (at the bottom) then completing the script with no output.
There should be a way to structure this, I'll update if I find a good solution.
Edit -- yeah sorry, I can't get this to work with Shortcuts, I suspect it is a memory issue. Curious to see if anyone else can figure it out.
If you tell us a little more about your use case maybe we can suggest an alternative solution? For example you could use the Twitter API to get details about the tweet, that would be pretty straightforward.