Some Random Nerd

View Original

Drafts and Safari bookmarklets

As I mentioned, I'm falling in love with the Drafts app on iPad/iPhone. One of the things I'm using its 'Actions' for is quickly dumping URLs into a single text file.

It occurred to me that an app that plays so nicely with URL schemes (ie. sending things to other apps via their URL schemes) would probably have a scheme of its own for pulling things in. A little googling later and I found that you can; like this bookmarklet

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(document.title+'\n')+encodeURIComponent(location.href)+'&action=URL%20to%20Scratch&x-success='+encodeURIComponent(location.href)

And a similar one from here

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(location.href+'%5Cn%5Cn')+encodeURIComponent(window.getSelection())+'&action=RunDue'

So, I figured a basic "send the URL of the current page to drafts" bookmarklet might look like;

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(document.title+'\n')+encodeURIComponent(location.href)

(Note - in Drafts, I have allowed URLs to trigger actions, but disabled the 'key' parameter. I should probably fix this at some point, but for now I'm going with "make it work" over "make it secure".)

And… yay - it works! From clicking a bookmarklet, I now have a Drafts document with the page title on one line and the URL on the next. Awesome...

What else can I do? Well, I have an action in Drafts called "LinksCollection" which sends things to a "links collection" Dropbox document, which I thought would be handy to store all of the links I obsessively collect (but rarely organise). That would be very handy to be able to trigger straight from Safari. Again, modifying the bookmarklets I found above a little, I get to;

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(document.title+'\n')+encodeURIComponent(location.href)+'&action=LinksCollection'

Success! (Again!) But this time, I get a "run this in Drafts?" pop up box. I didn't notice that before… Which is fine, but it might be nice to get rid of this.

But first, I want to play with this;

+encodeURIComponent(window.getSelection()

From my rudimentary Javascript, it looks like I can grab a selected piece of text from the web page (.getSelection) and send that chunk of text to Drafts as well.

Dropping the Action part for now (so that I can only break one thing at a time...)

javascript:window.location='drafts://x-callback-url/create?text='+encodeURIComponent(document.title+'\n')+encodeURIComponent(

And… it doesn't work. Or rather, it just does what the first version did – no extra text selection goodness.

Still, thats good enough for me for today - I can now do the clever things that I can do in Drafts straight from a Safari bookmarklet.

Result.