I’m trying to figure out how to convert some of my bookmarklets to Shortcuts. For example, this one converts a web page to an ePub. They provide code for a bookseller on their website, but I can’t figure out how to edit the code to make it run as a “Run JavaScript on Web Page” action in Shortcuts. Any ideas?
I set the Shortcut to be usable from the share sheet for Safari Web Pages, put the bookmarklet script into the JavaScript action,replaced some of the percent encoding to have spaces in the JavaScript and added the required completion function call at the end.
Script
Here’s the JavaScript, but reformatted for easier reading.
javascript: (function()
{
try
{
var d = document,
w = window;
if (!d.body || d.body.innerHTML == '') throw (0);
var s = d.createElement('link'),
h = d.getElementsByTagName('head')[0],
i = d.createElement('div'),
j = d.createElement('script');
s.rel = 'stylesheet';
s.href = '//dotepub.com/s/dotEPUB-favlet.css';
s.type = 'text/css';
s.media = 'screen';
h.appendChild(s);
i.setAttribute('id', 'dotepub');
i.innerHTML = '<div id=%22status%22><p>Conversion in progress...</p></div>';
d.body.appendChild(i);
j.type = 'text/javascript';
j.charset = 'utf-8';
j.src = '//dotepub.com/j/dotepub.js?v=1.2&s=0&t=epub&g=en';
h.appendChild(j);
}
catch (e)
{
w.alert('The page has no content or it is not fully loaded. Please, wait till the page is loaded.');
}
})();
completion();
In my quick test it did produce an ePub that contained at least a sub set of page content, so based on that I’m assuming it is working as expected.
I don’t know why it doesn’t work. There’s a lot that goes on in these libraries and I honestly don’t have the time to try and work out what it is doing in printfriendly.js and beyond (it isn’t trivial). Sometimes they work by dropping them into a Shortcut, sometimes they don’t. The reasons can vary.
But it does still work from a bookmark on iOS (and that’s from testing on an iPhone, not even an enhanced iPad Safari) - I would suggest that you utilise that approach for now.