Turning Bookmarklets into Shortcuts

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?

javascript:(function(){try{var%20d=document,w=window;if(!d.body||d.body.innerHTML=='')throw(0);var%20s=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%20id=%22status%22><p>Conversion%20in%20progress...</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%20page%20has%20no%20content%20or%20it%20is%20not%20fully%20loaded.%20Please,%20wait%20till%20the%20page%20is%20loaded.');}})();
1 Like

Try this.

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.

Hope that helps.

Thank you so very much! I was trying to figure out how to clean up the code, and this did it perfectly. Much appreciated!

Hi Sylumer,

Sorry to resurrecting this thread, I am trying to make another bookmarklet from PrintFriendly to iOS shortcut, but I can’t get it to work.

bookmarklet in following page

https://www.printfriendly.com/extensions/safari

my modified shortcut javascript version as follow:

javascript:(function(){if(window['priFri']){window.print()}else{pfstyle='nbk';pfBkVersion='1';_pnicer_script=document.createElement('SCRIPT');_pnicer_script.type='text/javascript';_pnicer_script.src='//cdn.printfriendly.com/printfriendly.js';document.getElementsByTagName('head')[0].appendChild(_pnicer_script);}})();

completion();

I also tried putting my code into your Safari Page to ePub shortcut, but still not working as expected. Any suggestion?

1 Like

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.

Hi Sylumer,

Thanks for replying, I will use the bookmarklet instead.

1 Like